在alertDialog中单击按钮时如何更新recyclerView

问题描述 投票:0回答:2

// 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();

结果是清除了回收站视图中的cardView并显示了回收的cardView的原始布局。

是否有一种方法可以将recycler.adapter的“ MyViewHolder”重新加载到一个位置,以便Cardview更新?

// Organisation_Fragment

       Initiates the adapter

[单击AlertDialog中的按钮并解除警报后,recyclerView中特定项目(位置)的复选框应显示为选中状态。

AlertDialog displayed over recyclerView

android android-recyclerview android-alertdialog recycler-adapter
2个回答
0
投票

使用适配器方法

adapter.notifyItemChanged(position, your Object);

0
投票

在这种情况下,我通常在我的适配器中添加一个对数据更改做出反应的方法

适配器:

    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);

        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.