[英]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. 我正在使用Recycleview适配器显示Firebase实时数据库中的项目列表。 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.
我有一个按钮,当单击该按钮时,它最初会显示Firebase的文本值,它将更改Firebase数据库中的相同键值,我也希望它也显示Button中的更新文本。
But it doesn't unless exit activity and reopen activity. 除非退出活动并重新打开活动,否则它不会。 Then it displays the updated value in Button text instead of the initial one.
然后,它在Button文本中显示更新的值,而不是初始值。
How to update and show the value without exiting the Adapter? 如何在不退出适配器的情况下更新和显示值?
I used notifyItemChanged(position)
but it doesn't do anything. 我使用了
notifyItemChanged(position)
但是它什么也没做。
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代码是
<?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)
. 无论您要在适配器中更改什么值,都可以使用
notifyItemChanged(position)
。 NotifyItemChange
only works when there is a change in your Model Class. NotifyItemChange
仅在Model类发生更改时才起作用。
Let say here you are setting the value your Book Name on a TextView
by using this code. 让我们在这里说,您正在使用此代码在
TextView
上设置您的Book Name的值。
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. 现在,如果您要更改
Button
上的Book name的值,则必须在ArrayList
Model中更改该值,然后通知它。
Books g = bookslist.get(position);
g.setBname("your new value");
After this, your notifyItemChanged(position)
will display the new value. 之后,您的
notifyItemChanged(position)
将显示新值。 This is how notify
works in RecyclerView
这是
notify
在RecyclerView
Why you have to do so? 为什么要这样做?
Whenever the notify is called in RecylerView
it again set the value from the ArrayList
. 每当在
RecylerView
调用RecylerView
它都会再次从ArrayList
设置值。 But in your case, the value is not updated locally in your list but is updated in the FireBase
. 但对于您而言,该值不会在列表中本地更新,而是在
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
. 要使用文本Remove更改
Button
的值,请在Markid
的Datachange中获取FireBase
。 Then set that value in the respective model(of that position) from the ArrayList
for String
Markid
. 然后在
ArrayList
为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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.