This is one of my fragment I am using in the navigation drawer. I populating the data from api and displaying in recyclerview. I have checked in the log and it displays the data in log but data is not getting populated in recycler view. I tried with breakpoints and logs. The data is coming in arraylist and I also tried with static data which is getting displayed. I am not able to understand where I am missing. And due to this fragment the other fragments are also not getting loaded in main frame. It appears blank. Thanks in advance.
public class HomeFragment extends Fragment {
private Context mContext;
private RecyclerView rvMain;
ArrayList<DataModel> arrayList;
private ProgressBar pb;
private ApiInterface apiInterface;
public HomeFragment(Context mContext) {
this.mContext = mContext;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d("Fragment", "onViewCreated: ");
rvMain = view.findViewById(R.id.main_category_rv);
pb = view.findViewById(R.id.main_pb);
arrayList = new ArrayList<>();
prepareData();
pb.setVisibility(View.GONE);
}
private void prepareData() {
pb.setVisibility(View.VISIBLE);
apiInterface = ApiRequest.createService(ApiInterface.class);
Call<MainCategoryModel> call = apiInterface.readJson();
call.enqueue(new Callback<MainCategoryModel>() {
@Override
public void onResponse(Call<MainCategoryModel> call, Response<MainCategoryModel> response) {
if (response.isSuccessful()) {
for (int i = 0; i < response.body().getData().getCategory().size(); i++) {
arrayList.add(new DataModel(response.body().getData().getCategory().get(i).getTitle(), response.body().getData().getCategory().get(i).getImage()));
}
} else {
Log.d("Response", "onResponse: " + response.errorBody());
}
}
@Override
public void onFailure(Call<MainCategoryModel> call, Throwable t) {
}
});
DataAdapter adapter = new DataAdapter(mContext, arrayList, new DataAdapter.RecyclerViewClickListener() {
@Override
public void onClick(View view, int position) {
}
});
rvMain.setAdapter(adapter);
AutoFitGridLayoutManager layoutManager = new AutoFitGridLayoutManager(mContext, 500);
rvMain.setLayoutManager(layoutManager);
}
}
First, make your adapter global in that activity, outside any method
DataAdapter adapter;
then you need to notify your adapter that you have added data with
yourAdapter.notifyDataSetChanged();
your prepareData method should be like this
private void prepareData() {
pb.setVisibility(View.VISIBLE);
apiInterface = ApiRequest.createService(ApiInterface.class);
adapter = new DataAdapter(mContext, arrayList, new DataAdapter.RecyclerViewClickListener() {
@Override
public void onClick(View view, int position) {
}
});
AutoFitGridLayoutManager layoutManager = new AutoFitGridLayoutManager(mContext, 500);
Call<MainCategoryModel> call = apiInterface.readJson();
call.enqueue(new Callback<MainCategoryModel>() {
@Override
public void onResponse(Call<MainCategoryModel> call, Response<MainCategoryModel> response) {
if (response.isSuccessful()) {
for (int i = 0; i < response.body().getData().getCategory().size(); i++) {
arrayList.add(new DataModel(response.body().getData().getCategory().get(i).getTitle(), response.body().getData().getCategory().get(i).getImage()));
}
adapter.notifyDataSetChanged(); // notify your adapter here
} else {
Log.d("Response", "onResponse: " + response.errorBody());
}
}
@Override
public void onFailure(Call<MainCategoryModel> call, Throwable t) {
}
});
rvMain.setAdapter(adapter);
rvMain.setLayoutManager(layoutManager);
}
}
why static data show you the list, because its sync, but retrofit enqueue is using async call, it may need few time to get the data