Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * A simple {@link Fragment} subclass.
- */
- public class StoreListFragment extends Fragment {
- public static final String KEY_LAT = "lat";
- public static final String KEY_LNG = "lng";
- private StoreItemAdapter mAdapter;
- private List<Store> mStoreList;
- public StoreListFragment() {
- // Required empty public constructor
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- View view = inflater.inflate(R.layout.fragment_store_list, container, false);
- RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
- recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
- mStoreList = new ArrayList<>();
- mAdapter = new StoreItemAdapter(getActivity(), mStoreList);
- mAdapter.setListener(this);
- recyclerView.setAdapter(mAdapter);
- if(ConnectivityUtil.isConnected(getActivity())){
- loadStores();
- }
- else{
- PopupUtil.showMsg(getActivity(), "No Internet connection", PopupUtil.SHORT);
- }
- return view;
- }
- private void loadStores(){
- PopupUtil.showLoading(getActivity(), "", "Loading stores....");
- ApiEndPoint apiEndPoint = ApiClient.getClient().create(ApiEndPoint.class);
- Call<StoreResponse> call = apiEndPoint.getStore(Double.toString(mLat),Double.toString(mLng));
- call.enqueue(new Callback<StoreResponse>() {
- @Override
- public void onResponse(Call<StoreResponse> call, Response<StoreResponse> response) {
- PopupUtil.dismissDialog();
- StoreResponse storeResponse = response.body();
- if (storeResponse != null){
- if (storeResponse.getSuccess()){
- Log.d("StoreListFragment", "Jumlah store:" + storeResponse.getStore().size());
- mStoreList.addAll(storeResponse.getStore());
- getActivity().runOnUiThread(new Runnable() {
- @Override
- public void run() {
- mAdapter.notifyDataSetChanged();
- }
- });
- }
- }
- else {
- Log.d("StoreListFragment", "response is null");
- }
- }
- @Override
- public void onFailure(Call<StoreResponse> call, Throwable t) {
- PopupUtil.dismissDialog();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement