I am trying to update myadapter using notifyDataSetchanged() in updateList().But not working for recyclerview. I can able to see list size in updateList() and its displaying actual result but only reclerview is not updating.This class was designed in MVVM Design Pattern.
public class TasksAdapter extends RecyclerView.Adapter<TasksAdapter.TasksAdapterViewHolder> {
private static final String TAG = TasksAdapter.class.getSimpleName();
static ArrayList<ItemModel> list;
static Context mContext;
private Fragment fragment;
private int row_index = -1;
private SessionManager session;
// SelectColorVM colorVM;
public TasksAdapter(Fragment fragment, Context context, ArrayList<ItemModel> itemList) {
this.mContext = context;
this.list = itemList;
this.fragment = fragment;
session = new SessionManager(fragment.getContext());
}
public void updateList(ArrayList<ItemModel> itemList){
list.clear();
this.list = itemList;
notifyDataSetChanged();
}
@Override
public TasksAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
TasksItemBinding binding =
DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.tasks_item,
parent, false);
return new TasksAdapter.TasksAdapterViewHolder(binding);
}
@Override
public void onBindViewHolder(TasksAdapterViewHolder holder, final int position) {
holder.binding.setViewModel(list.get(position));
if(position == 0 && list.get(position).btnText.get().toString().length() > 0)
holder.binding.btnUpdate.setVisibility(View.VISIBLE);
else
holder.binding.btnUpdate.setVisibility(View.INVISIBLE);
holder.binding.layoutItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// row_index = position;
// notifyDataSetChanged();
Fragment fragment = new DetailFragment();
FragmentManager manager = ((FragmentActivity) mContext).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
Bundle bundle = new Bundle();
ItemModel item = list.get(position);
bundle.putSerializable("item", item);
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.id_frame, fragment, "details");
fragmentTransaction.addToBackStack("details");
fragmentTransaction.commit();
}
});
holder.binding.btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
HashMap<String, String> user = session.getUserDetails();
if(list.get(position).btnText.get().equals("Task Start")){
LandingEngine.updateStatusAndSetStartTimeForRequest(fragment, user.get(SessionManager.KEY_CLEANER_ID), list.get(position).sid.get().toString());
} else if(list.get(position).btnText.get().equals("Task Complete")){
LandingEngine.updateStatusAndSetCompleteTimeForRequest(fragment, user.get(SessionManager.KEY_CLEANER_ID), list.get(position).sid.get().toString());
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public static class TasksAdapterViewHolder extends RecyclerView.ViewHolder {
TasksItemBinding binding;
public TasksAdapterViewHolder(TasksItemBinding rowbinding) {
super(rowbinding.getRoot());
this.binding = rowbinding;
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
}
I am calling adapter from my fragment like below
if(list.size() > 0) {
if(adapter == null) {
view.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(fragment.getContext());
view.setLayoutManager(mLayoutManager);
adapter = new TasksAdapter(fragment, fragment.getContext(), list);
view.setAdapter(adapter);
view.addItemDecoration(new DividerItemDecoration(fragment.getContext()));
} else {
adapter.updateList(list);
// view.getAdapter().notifyDataSetChanged();
// view.invalidate();
}
}
Please check this and let me know where i am doing wrong here.Thanks in advance..
Finally its working by calling this method from fragment class once getting API response from the volley library.
I have written following method in ViewModel class.
public void updateList(ArrayList<Class> list){
if(adapter != null) {
adapter.updateList(list);
}
}
And calling this method from fragment class like following.
ArrayList<StatisticsModel> list = (ArrayList) data;
viewModel.updateList(list);
So in the adapter class update method is following.
public void updateList(ArrayList<StatisticsModel> itemList){
list.clear();
// this.list = itemList;
// list.addAll(itemList);
// notifyItemInserted(list.size());
// notifyDataSetChanged();
this.list.addAll(itemList);
// notifyItemInserted(list.size());
notifyDataSetChanged();
}