gpt4 book ai didi

android - Activity 恢复时更新 RecyclerView?

转载 作者:行者123 更新时间:2023-12-03 19:49:04 24 4
gpt4 key购买 nike

我有一个 QuestionActivity我是否在 RecyclerView 中显示问题列表? .

我在工具栏中有两个 Action

  • 转到下载的文件 Activity 。
  • 去最喜欢的 Activity 。

  • 一切正常,但是当我点击其中一个操作并返回 QuestionActivityRecyclerView无法更新。

    所以我需要覆盖 OnResume方法。我的问题是,如何更新 RecyclerViewonResume方法 。
    AdapterRecyclerQuestion代码 :
    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();
    }
    }
    }

    谢谢。

    最佳答案

    Loaders最适合您希望通知所有数据库更改并反射(reflect)在 UI 上的目的。

    如果您正在从其他两个 Activity 对数据库进行一些更改并希望在 QuestionActivity 上更新 UI,只需制作一个加载器并在 onResume() 中启动它。

    通过这个链接来实现一个加载器:
    http://www.grokkingandroid.com/using-loaders-in-android/

    关于android - Activity 恢复时更新 RecyclerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38182868/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com