简体   繁体   English

在Bindview支架内单击时如何显示更新的RecycleView适配器?

[英]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 这是notifyRecyclerView

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 . 然后在ArrayListString 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.

相关问题 如何在Android中的游标适配器的bindview中进行日期解析和格式化 - How to do Date parsing and formating inside a bindview of cursor adapter in android RecycleView适配器内的ViewPager - ViewPager inside RecycleView Adapter 如何在RecycleView Adapter中从ImageView onClick显示对话框片段 - How to Show Dialog Fragment from ImageView onClick in RecycleView Adapter 单击从RecycleView Adapter启动活动 - Start Activity from RecycleView Adapter on click Firestore - RecycleView - 图像支架 - Firestore - RecycleView - Image holder 如何从片段内的适配器视图持有者 class 访问 UI 元素? - How do you access a UI element from an adapter view holder class inside a fragment? 如何使用视图持有者图案制作自定义适配器? - How to make custom adapter with view holder pattern? 差异Recycleview:在CreateView或BindView中设置颜色 - Difference Recycleview: set color in CreateView or BindView 将Firebase中的值添加到RecycleView适配器中不会显示在应用程序中 - Adding values from Firebase to RecycleView adapter won't show in app 如何在 recycleView 中创建 CardView? - How Create a CardView inside a recycleView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM