Advertisement
abuzuhair

Display store in Recycler View

Dec 15th, 2017
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. /**
  2.  * A simple {@link Fragment} subclass.
  3.  */
  4. public class StoreListFragment extends Fragment {
  5.     public static  final  String KEY_LAT = "lat";
  6.     public static  final  String KEY_LNG = "lng";
  7.     private StoreItemAdapter mAdapter;
  8.     private List<Store> mStoreList;
  9.  
  10.     public StoreListFragment() {
  11.         // Required empty public constructor
  12.     }
  13.  
  14.  
  15.     @Override
  16.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  17.                              Bundle savedInstanceState) {
  18.         // Inflate the layout for this fragment
  19.         View view = inflater.inflate(R.layout.fragment_store_list, container, false);
  20.  
  21.         RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
  22.         recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
  23.  
  24.         mStoreList = new ArrayList<>();
  25.         mAdapter = new StoreItemAdapter(getActivity(), mStoreList);
  26.         mAdapter.setListener(this);
  27.         recyclerView.setAdapter(mAdapter);
  28.  
  29.         if(ConnectivityUtil.isConnected(getActivity())){
  30.             loadStores();
  31.         }
  32.         else{
  33.             PopupUtil.showMsg(getActivity(), "No Internet connection", PopupUtil.SHORT);
  34.         }
  35.  
  36.         return view;
  37.     }
  38.  
  39.     private void loadStores(){
  40.         PopupUtil.showLoading(getActivity(), "", "Loading stores....");
  41.  
  42.         ApiEndPoint apiEndPoint = ApiClient.getClient().create(ApiEndPoint.class);
  43.         Call<StoreResponse> call = apiEndPoint.getStore(Double.toString(mLat),Double.toString(mLng));
  44.  
  45.         call.enqueue(new Callback<StoreResponse>() {
  46.             @Override
  47.             public void onResponse(Call<StoreResponse> call, Response<StoreResponse> response) {
  48.                 PopupUtil.dismissDialog();
  49.                 StoreResponse storeResponse = response.body();
  50.  
  51.                 if (storeResponse != null){
  52.                     if (storeResponse.getSuccess()){
  53.                         Log.d("StoreListFragment", "Jumlah store:" + storeResponse.getStore().size());
  54.                         mStoreList.addAll(storeResponse.getStore());
  55.  
  56.                         getActivity().runOnUiThread(new Runnable() {
  57.                             @Override
  58.                             public void run() {
  59.                                 mAdapter.notifyDataSetChanged();
  60.                             }
  61.                         });
  62.                     }
  63.                 }
  64.                 else {
  65.                     Log.d("StoreListFragment", "response is null");
  66.                 }
  67.             }
  68.  
  69.             @Override
  70.             public void onFailure(Call<StoreResponse> call, Throwable t) {
  71.                 PopupUtil.dismissDialog();
  72.  
  73.             }
  74.         });
  75.  
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement