简体   繁体   中英

How to update recyclerView when button is clicked in an alertDialog

// RecyclerAdapter (On click listener for the image)

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

// Main Activity

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

What I've tried is Fragment.adapter.notifyItemChanged(p); and Fragment.adapter.notifyDataSetChanged();

The outcome is that the cardView in the recyclerview gets cleared and the original layout of the recycled cardView is displayed.

Is there a way to reload the "MyViewHolder" of the recycler.adapter for one position so that the cardview updates?

// Organisation_Fragment

       Initiates the adapter

When the button in the AlertDialog is clicked and the alert is dismissed the checkbox of a specific item(position) in the recyclerView should appear selected.

显示在 recyclerView 上的 AlertDialog

In this case, I usually add a method in my Adapter which reacts to the data change

Adapter:

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

Your Activity:

private AutoCompleteAdapter adapter;

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

your click listener

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

        }
    });

Use the adapter method

adapter.notifyItemChanged(position, your Object);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM