在Bindview支架内单击时如何显示更新的RecycleView适配器? [英] How to show updated RecycleView adapter on click inside Bindview holder?
问题描述
我正在使用Recycleview适配器显示Firebase实时数据库中的项目列表.我有一个按钮,当单击该按钮时,它最初会显示Firebase的文本值,这会更改Firebase数据库中的相同键值,我也希望它也能在Button中显示更新的文本.
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.
但是,除非退出活动并重新打开活动,否则它不会.然后,它将在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.
如何在不退出适配器的情况下更新和显示值?
How to update and show the value without exiting the Adapter?
我使用了 notifyItemChanged(position)
,但是它什么也没做.
I used notifyItemChanged(position)
but it doesn't do anything.
这是我的代码
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代码是
<?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>
谢谢.
推荐答案
无论您想在适配器中更改什么值,都可以使用 notifyItemChanged(position)
进行. NotifyItemChange
仅在您的模型类发生更改时有效.
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.
让我们在这里说,您正在使用此代码在 TextView
上设置书名"的值.
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());
现在,如果您想更改 Button
上的Book name的值,则必须更改 ArrayList
模型中的值,然后通知它.
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");
此后,您的 notifyItemChanged(position)
将显示新值.这是 notify
在 RecyclerView
After this, your notifyItemChanged(position)
will display the new value. This is how notify
works in RecyclerView
为什么必须这样做?
每当在 RecylerView
中调用notify时,它都会再次从 ArrayList
中设置值.但对于您而言,该值不会在列表中本地更新,而是在 FireBase
中更新.因此,每当您再次打开应用程序时,都会获得更新的值.
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.
注意
要使用文本Remove更改 Button
的值,请在 FireBase
的数据更改中获取 Markid
.然后从 ArrayList
的 String
Markid
的相应模型(该位置)中设置该值.
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)
}
这篇关于在Bindview支架内单击时如何显示更新的RecycleView适配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!