Java – what is notifyitemrangechanged (0, this. Data. Size()); How does it work in this example?
I understand how the onbindviewholder of viewholder works, but I don't know how notifyitemrangechanged (0, this. Data. Size()); Applies to this example and its exact functionality
The data provided to this adapter is in JSON format
Adapters are as follows:
public class AdapterQuestion extends RecyclerView.Adapter<AdapterQuestion.ViewQuestion>{
private LayoutInflater mLayoutInflater;
//this is an arrayList of questionData objects
private ArrayList<QuestionData> data =new ArrayList<>();
//Created the layoutInflator
public AdapterQuestion(Context context){
//get from context
mLayoutInflater=LayoutInflater.from(context);
}
public void setBloglist(ArrayList<QuestionData> data){
this.data =data;
notifyItemRangeChanged(0,this.data.size());
}
@Override
public ViewQuestion onCreateViewHolder(ViewGroup parent,int viewType) {
//inflates the customQuestion view or converts it to java code
View view= mLayoutInflater.inflate(R.layout.customquestion,null);
//We Now want to convert the View into a ViewQuestion,view Question takes
//a view so we pass the view into view question and then return it.
ViewQuestion holder=new ViewQuestion(view);
return holder;
}
//ViewGroup parent and ViewType are not being assigned.
@Override
public void onBindViewHolder(ViewQuestion holder,int position) {
//here we need to bind the data to our view,there is currently no Data!
//We need to get the data from our JSON
//Parameters is a ViewHolder and a Position
//This gives us the current information object from the whole arraylist
//data.get(position) data is the arraylist and we are getting the current position or index;
//That current obj is of Type QuestionData
QuestionData currentObj= data.get(position);
//we are accessing the Inflated view,or saved view with holder
//holder.answerText is the textView in holder. We are then taking that current object
//We are getting the text of the current object and setting it to the AnswerText view
holder.answerText.setText(currentObj.getMtext());
holder.answerId.setText(currentObj.getId());
holder.mVotes.setText(currentObj.getVotes());
holder.mLikeButton.setTag(currentObj);
}
@Override
public int getItemCount() {
return data.size();
}
public class ViewQuestion extends RecyclerView.ViewHolder{
//once we create it once the reclycer view will automatically recycle it
private TextView answerText;
private TextView answerId;
private TextView mVotes;
private LikeButton mLikeButton;
public ViewQuestion (View itemView){
super(itemView);
//here we are finding the views by their ID
answerText=(TextView)itemView.findViewById(R.id.answerText);
answerId=(TextView)itemView.findViewById(R.id.answerId);
mVotes=(TextView)itemView.findViewById(R.id.VoteTextView);
mLikeButton= (LikeButton)itemView.findViewById(R.id.heart_buttons);
mLikeButton.setOnLikeListener(new OnLikeListener() {
@Override
public void liked(LikeButton likeButton) {
Voting Vote = new Voting();
Vote.onUpVote(convertToString(),getAdapterPosition(),ViewQuestion.this);
System.out.print("Adapter Position"+getAdapterPosition());
}
@Override
public void unLiked(LikeButton likeButton) {
Voting onDown=new Voting();
onDown.onDownVote(convertToString(),ViewQuestion.this);
}
});
}
public String getVoteView(){
String VoteView=mVotes.getText().toString();
return VoteView;
}
public String convertToString(){
String converted=answerId.getText().toString();
return converted;
}
public int convertToInt(){
String converted=answerId.getText().toString();
int ConvertedInt=Integer.parseInt(converted);
return ConvertedInt;
}
}
}
Solution
When the data to be set in the recyclerview changes, the adapter needs to be notified of the data change so that it can change the data in the recyclerview
method
notifyItemRangedChanged(fromIndex,toIndex);
It is used to inform the adapter that a set of data has been changed in the whole data. It tells the adapter that the adapter should refresh the data and reload it into the recyclerview from fromindex to toindex of the incoming method
If you have more than one data that has changed, but not all of it, use this method, and the changed data is also in the cluster, so that you can say that the index data from the 5th to the 10th has changed
If you have changed all the data, call:
notifyDataSetChanged();
If only one dataitem is changed, call:
notifyItemChanged(dataPosition);
