SHARE
TWEET

StoreListFragment

a guest Sep 17th, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. class StoreListFragment : Fragment() {
  3.     private var mAdapter: StoreItemAdapter? = null
  4.     private var mStoreList: MutableList<Store>? = null
  5.  
  6.  
  7.     fun onCreateView(inflater: LayoutInflater, container: ViewGroup,
  8.                      savedInstanceState: Bundle): View {
  9.         // Inflate the layout for this fragment
  10.         val view = inflater.inflate(R.layout.fragment_store_list, container, false)
  11.  
  12.         val recyclerView = view.findViewById(R.id.recycler_view)
  13.         recyclerView.setLayoutManager(LinearLayoutManager(getActivity()))
  14.  
  15.         mStoreList = ArrayList()
  16.         mAdapter = StoreItemAdapter(getActivity(), mStoreList!!)
  17.         mAdapter!!.setListener(this)
  18.         recyclerView.setAdapter(mAdapter)
  19.  
  20.         if (ConnectivityUtil.isConnected(getActivity())) {
  21.             loadStores()
  22.         } else {
  23.             PopupUtil.showMsg(getActivity(), "No Internet connection", PopupUtil.SHORT)
  24.         }
  25.  
  26.         return view
  27.     }
  28.  
  29.     private fun loadStores() {
  30.         PopupUtil.showLoading(getActivity(), "", "Loading stores....")
  31.  
  32.         val apiEndPoint = ApiClient.getClient().create(ApiEndPoint::class.java)
  33.         val call = apiEndPoint.getStore(java.lang.Double.toString(mLat).toDouble(), java.lang.Double.toString(mLng).toDouble())
  34.  
  35.         call.enqueue(object : Callback<StoreResponse>() {
  36.             fun onResponse(call: Call<StoreResponse>, response: Response<StoreResponse>) {
  37.                 PopupUtil.dismissDialog()
  38.                 val storeResponse = response.body()
  39.  
  40.                 if (storeResponse != null) {
  41.                     if (storeResponse!!.success!!) {
  42.                         Log.d("StoreListFragment", "Jumlah store:" + storeResponse!!.store!!.size)
  43.                         mStoreList!!.addAll(storeResponse!!.store!!)
  44.  
  45.                         getActivity().runOnUiThread(Runnable { mAdapter!!.notifyDataSetChanged() })
  46.                     }
  47.                 } else {
  48.                     Log.d("StoreListFragment", "response is null")
  49.                 }
  50.             }
  51.  
  52.             fun onFailure(call: Call<StoreResponse>, t: Throwable) {
  53.                 PopupUtil.dismissDialog()
  54.  
  55.             }
  56.         })
  57.  
  58.     }
  59.  
  60.     companion object {
  61.         val KEY_LAT = "lat"
  62.         val KEY_LNG = "lng"
  63.     }
  64. }// Required empty public constructor
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top