简体   繁体   中英

How to show updated RecycleView adapter on click inside Bindview holder?

I am using Recycleview Adapter to show my list of items from Firebase Real-time Database. I have a button which shows text value from Firebase initially when clicked will change the same key value in Firebase Database and I want it to show the updated text in Button too.

But it doesn't unless exit activity and reopen activity. Then it displays the updated value in Button text instead of the initial one.

How to update and show the value without exiting the Adapter?

I used notifyItemChanged(position) but it doesn't do anything.

Here is my Code

public class SubjectBooksAdapter extends RecyclerView.Adapter<SubjectBooksAdapter.MyViewHolder> {
    ArrayList<Books> bookslist;
    CardView cv;
    FirebaseAuth fauth;
    FirebaseDatabase database;
    DatabaseReference dbreference;
    Books g;
    SubjectBooksAdapter adapter;



    public SubjectBooksAdapter(ArrayList<Books> bookslist){
        this.bookslist = bookslist;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout,parent,false);
        return new MyViewHolder(v);

    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        Button mSolved;
        MyViewHolder(final View itemView) {
            super(itemView);
            database = FirebaseDatabase.getInstance();
            dbreference = database.getReference("books");
            dbreference.keepSynced(true);
            mSolved = (Button) itemView.findViewById(R.id.book_solved);

        }
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {

        database = FirebaseDatabase.getInstance();
        dbreference = database.getReference("books");

        g = bookslist.get(position);


        holder.mSolved.setText(g.getMarkid());
        holder.bookName.setText(g.getBname());
        holder.bookprice.setText("Rs. "+g.getPrice());
        holder.sellername.setText(g.getSellername());



        holder.mSolved.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
            DatabaseReference classicalMechanicsRef = rootRef.child("books").child(g.getCondition()).child(g.getBookid());
            ValueEventListener valueEventListener = new ValueEventListener() {

                @Override
             public void onDataChange(DataSnapshot dataSnapshot) {
            dataSnapshot.child("bmark").getRef().setValue("Solved");
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
};
classicalMechanicsRef.addListenerForSingleValueEvent(valueEventListener);


notifyItemChanged(position);
            }

        });



    @Override
    public int getItemCount() {
        return bookslist.size();
    }
}

xml code is

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" > <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/my_card_view" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="3dp" android:layout_marginTop="4dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="2dp" > <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".9" android:layout_marginBottom="5dp"> <ImageView android:id="@+id/imageView" android:layout_width="75dp" android:layout_height="75dp" android:scaleType="fitXY" android:padding="2dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingLeft="8dp" android:paddingRight="7dp" android:gravity="left"> <TextView android:id="@+id/book_name" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".4" android:text="Book Name" android:textStyle="bold" android:fontFamily="sans-serif-condensed" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_margin="1dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/seller_name" android:layout_width="match_parent" android:fontFamily="sans-serif" android:layout_height="35dp" android:layout_weight=".3" android:text="Seller" android:layout_margin="2dp" android:textAppearance="?android:attr/textAppearanceMedium" /> <Button android:fontFamily="sans-serif-condensed" android:gravity="center" android:textColor="#ffffff" android:text="Remove" android:textStyle="bold" android:layout_marginEnd="15dp" android:textAppearance="?android:attr/textAppearanceSmall" android:layout_marginRight="15dp" android:layout_width="wrap_content" android:id="@+id/book_solved" android:textAllCaps="false" android:layout_height="35dp" android:background="@drawable/buttonshape1" android:padding="8dp" android:layout_toRightOf="@id/seller_name"/> </LinearLayout> </LinearLayout> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_marginTop="4dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/profile_details" android:layout_width="match_parent" android:layout_height="35dp" android:fontFamily="sans-serif-condensed" android:text="View Details" android:background="@drawable/buttonshape1" android:gravity="center" android:textColor="#ffffff" android:textStyle="bold" android:layout_marginEnd="15dp" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_marginRight="15dp" /> </LinearLayout> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout> 

Thanks in advance.

Whatever the value you want to change in your adapter you can do using notifyItemChanged(position) . NotifyItemChange only works when there is a change in your Model Class.

Let say here you are setting the value your Book Name on a TextView by using this code.

Books g = bookslist.get(position);    
holder.bookName.setText(g.getBname());

Now if you want to change the value of Book name on Button click you have to change the value in your ArrayList Model also then notify it.

Books g = bookslist.get(position);    
g.setBname("your new value");

After this, your notifyItemChanged(position) will display the new value. This is how notify works in RecyclerView

Why you have to do so?

Whenever the notify is called in RecylerView it again set the value from the ArrayList . But in your case, the value is not updated locally in your list but is updated in the FireBase . That's why you get the updated value whenever you again open your app.

NOTE

To change the value of Button with text Remove, get the Markid in the Datachange of the FireBase . Then set that value in the respective model(of that position) from the ArrayList for String Markid .

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    dataSnapshot.child("bmark").getRef().setValue("Solved");
    // Here you need to save the value in the arraylist.   
    bookslist.get(position).setMardid("Here what ever the value you will pass will get updated on the button after notify"); //Try adding the static string that you want after button click.
    notifyItemChanged(position)
}

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