簡體   English   中英

在alertDialog中單擊按鈕時如何更新recyclerView

[英]How to update recyclerView when button is clicked in an alertDialog

// RecyclerAdapter(圖像的點擊監聽器)

itemView.setOnClickListener(new View.OnClickListener() {
      @Override
         public void onClick(View v) {
          Integer p = getAdapterPosition()
                Drawable d = imageView.getDrawable();
                ((MainActivity) itemView.getContext()).showOrganisation(p,d);
            }
});

// 主要活動

 public void showOrganisation(Integer p, Drawable d) {

    myDialogOrg.setContentView(R.layout.popup_organisation);
    TextView selectButton = myDialogOrg.findViewById(R.id.popOrgClose);
    ImageView imageView = myDialogOrg.findViewById(R.id.popUpLogo);
    imageView.setImageDrawable(d);

    myDialogOrg.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    selectButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences pref = v.getContext().getSharedPreferences("CheckBoxStateOrganisationer", MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();

           // Send mysql query to database that a charity is selected
           // Code
                            response{
           // Store State of checkbox
               editor.putBoolean("CheckBoxOrganisationer"+p, true);
               editor.commit();
             }
            ////////////////////////////////
            // update state of checkbox in//
            //////////////////////////////// 

          -> I've added code here <-  

            // Dismiss popup window
            myDialogOrg.dismiss();
        }
    });
    myDialogOrg.show();
}

我試過的是 Fragment.adapter.notifyItemChanged(p); 和 Fragment.adapter.notifyDataSetChanged();

結果是recyclerview中的cardView被清空,顯示回收cardView的原始布局。

有沒有辦法為一個 position 重新加載 recycler.adapter 的“MyViewHolder”,以便卡片視圖更新?

// Organisation_Fragment

       Initiates the adapter

當單擊 AlertDialog 中的按鈕並解除警報時,recyclerView 中特定項目(位置)的復選框應顯示為選中狀態。

顯示在 recyclerView 上的 AlertDialog

在這種情況下,我通常在我的適配器中添加一個對數據更改做出反應的方法

適配器:

    public void updateData(List<String> newList){
        suggestions.clear();
        if(newList!=null) suggestions.addAll(newList);
        notifyDataSetChanged();
    }

您的活動:

private AutoCompleteAdapter adapter;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
adapter = new AutoCompleteAdapter
                            (context, suggestions, etAnswer);
recyclerView.setAdapter(adapter);
}

你的點擊監聽器

selectButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         // your other code
            ////////////////////////////////
            // update state of checkbox in//
            //////////////////////////////// 

          -> I've added code here <-  
           adapter.updateData(yourNewList);

        }
    });

使用適配器方法

adapter.notifyItemChanged(position, your Object);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM