Reputation: 292
I have a QuestionActivity
were I show the list of questions in the RecyclerView
.
I have two action in toolbar
Go to downloadedFile activity.
Go to favorite activity.
Every thing is OK but when I hit one of that actions and return to QuestionActivity
the RecyclerView
couldn't get updated.
So I need to override OnResume
Method. My question is, How can I update RecyclerView
in onResume
Method .
AdapterRecyclerQuestion
code :
public class AdapterRecyclerQuestion extends RecyclerView.Adapter<AdapterRecyclerQuestion.ViewHolder> {
private Context context;
private ArrayList<ModelQuestion> questionha;
private int lastd;
////////////////////////////////////////////////////////////
//================== Constructor =========================
////////////////////////////////////////////////////////////
public AdapterRecyclerQuestion(Context context, ArrayList<ModelQuestion> questionha) {
this.context = context;
this.questionha = questionha;
}
////////////////////////////////////////////////////////////
//================== view holder =========================
////////////////////////////////////////////////////////////
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView txtTitle;
private TextView txtDesc;
private TextView txtCntDown;
private Button btnDownload;
private ImageView imgAddFav;
private ProgressBar prgDownload;
////////////////////////////////////////////////////////////
//================== View holder constructor ==============
////////////////////////////////////////////////////////////
public ViewHolder(View itemView) {
super(itemView);
txtTitle = (TextView) itemView.findViewById(R.id.txt_title_question);
txtDesc = (TextView) itemView.findViewById(R.id.txt_desc_question);
txtCntDown = (TextView) itemView.findViewById(R.id.txt_cnt_down_question_dy);
btnDownload = (Button) itemView.findViewById(R.id.btn_down_question);
imgAddFav = (ImageView) itemView.findViewById(R.id.img_add_to_fav);
prgDownload = (ProgressBar) itemView.findViewById(R.id.prgDownload);
}
}
////////////////////////////////////////////////////////////
//================== on create view holder ================
////////////////////////////////////////////////////////////
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.question_row, parent, false);
return new ViewHolder(view);
}
////////////////////////////////////////////////////////////
//================== on bind view holder =================
////////////////////////////////////////////////////////////
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
QuestionDatabaseAdapter questionDatabaseAdapter = new QuestionDatabaseAdapter(holder.itemView.getContext());
holder.txtTitle.setText(questionha.get(position).getQuestionTitle());
holder.txtDesc.setText(questionha.get(position).getQuestionDesc());
holder.txtCntDown.setText(questionha.get(position).getQuestionDownCnt());
////////////////////////////////////////////////////////////
//================== Favorite checking ====================
////////////////////////////////////////////////////////////
boolean isFavorite = questionDatabaseAdapter.isQuestionFavorite(questionha.get(position).getQuestionId());
if (isFavorite)
{
holder.imgAddFav.setImageResource(R.drawable.ic_favorite_red_700_24dp);
} else
{
holder.imgAddFav.setImageResource(R.drawable.ic_favorite_border_red_a700_24dp);
}
////////////////////////////////////////////////////////////
//================== img add fav listener =================
////////////////////////////////////////////////////////////
holder.imgAddFav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
QuestionDatabaseAdapter databaseAdapter = new QuestionDatabaseAdapter(v.getContext());
boolean isFav = databaseAdapter.isQuestionFavorite(questionha.get(position).getQuestionId());
if (!isFav) {
Toast.makeText(v.getContext(), "به لیست علاقه مندی ها اضافه شد", Toast.LENGTH_SHORT).show();
holder.imgAddFav.setImageResource(R.drawable.ic_favorite_red_700_24dp);
ModelQuestion question = new ModelQuestion();
question.setQuestionId(questionha.get(position).getQuestionId());
question.setQuestionTitle(questionha.get(position).getQuestionTitle());
question.setQuestionDesc(questionha.get(position).getQuestionDesc());
question.setQuestionDownLink(questionha.get(position).getQuestionDownLink());
question.setQuestionDownFileName(questionha.get(position).getQuestionDownFileName());
databaseAdapter.saveQuestion(question);
} else {
Toast.makeText(v.getContext(), "از لیست علاقه مندی ها پاک شد", Toast.LENGTH_SHORT).show();
holder.imgAddFav.setImageResource(R.drawable.ic_favorite_border_red_a700_24dp);
databaseAdapter.deleteQuestion(questionha.get(position).getQuestionId());
}
}
}
);
}
////////////////////////////////////////////////////////////
//================== get item count method ================
////////////////////////////////////////////////////////////
@Override
public int getItemCount() {
if (questionha.size() == 0) {
return 0;
} else {
return questionha.size();
}
}
}
Thanks.
Upvotes: 5
Views: 10997
Reputation: 292
Thanks to Anoop M , I just write the solution work for me . I write This code in QuestionActivity .
////////////////////////////////////////////////////////////
//================== on Resume ===========
////////////////////////////////////////////////////////////
@Override
protected void onResume() {
super.onResume();
adapterRecyclerQuestion.notifyDataSetChanged();
recyclerQuestion.setAdapter(adapterRecyclerQuestion);
}
Upvotes: 1
Reputation: 1002
Loaders
are best suited for such purposes where you want all database changes to be notified and reflected on the UI.
If your are making some changes in database from other two activities and wanted the UI to be updated on QuestionActivity, just make a loader and start it in onResume().
Go through this link to implement a loader: http://www.grokkingandroid.com/using-loaders-in-android/
Upvotes: 1
Reputation: 633
You can try this.
create a setter for your ArrayList<ModelQuestion> questionha;
in the Adapter.
public void setData(ArrayList<ModelQuestion> data){
questionha=data;
notifyDataSetChanged();
}
And call this in your onResume() with the new Data
Remember to call the notifyDataSetChanged();
or specific notifiyItemChangedAt(pos)
etc if you know Item at which position has changed an so on. You can choose from - https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html
Upvotes: 0