简体   繁体   English

在另一个活动的按钮上清除recyclerview

[英]clear recyclerview on pressing button of another activity

I have RecyclerView in cart activity. 我在购物车活动中有RecyclerView。 I want to clear this RecyclerView on pressing checkout activity's complete order button. 我想在按结帐活动的完成订单按钮时​​清除此RecyclerView。

Here I tried to describe the scenario: 在这里,我试图描述这种情况: 在此处输入图片说明

I can think of two possible solutions: 我可以想到两种可能的解决方案:

  1. Start the checkout activity with startActivityForResult() and then return a value which indicates whether or not to clear the RecyclerView. 使用startActivityForResult()启动检出活动,然后返回一个值,该值指示是否清除RecyclerView。 See Getting a Result from an Activity for details. 有关详细信息,请参见从活动获取结果

  2. Store the cart contents in a on disk in a file or database. 将购物车内容存储在文件或数据库的磁盘上。 The data can include a flag which indicates if the purchase has been completed. 该数据可以包括指示购买是否已经完成的标志。 The cart activity then only loads data for items which are in the cart but not yet paid for. 然后,购物车活动仅加载购物车中但尚未付款的商品的数据。

Just check start activity for result how it work refrance - https://stackoverflow.com/a/10407371/4741746 只要检查结果它是如何工作的refrance开始活动- https://stackoverflow.com/a/10407371/4741746

Than in onActivityResult method you can update your adapter by using notifyDataSetChanged() or refresh method onActivityResult方法相比,您可以使用notifyDataSetChanged()或refresh方法更新适配器。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            String result=data.getStringExtra("result");
            ArrayList<String> yourNewList= new ArrayList<String> (); 
            adapter.Refresh(yourNewList);
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}
public void Refresh(ArrayList<String> datas) {    //your bean
        this.mStrings.clear();          //mStrings is your bean ArrayList
        this.mStrings.addAll(datas);
        notifyDataSetChanged();
    }

put this Refresh method in your adapter if you want totally refresh adapter 如果要完全刷新适配器,请在适配器中放入此Refresh方法

on complete order button click send a local broadcast to the previous recyclerView activity where on receiving the broadcast clear the list and update the recyclerView. 单击完成订单按钮后,将本地广播发送到上一个recyclerView活动,在接收到广播后,清除列表并更新recyclerView。 Then your recyclerView will be cleared. 然后,您的recyclerView将被清除。

As mentioned above it would be best to go with startActivityForResult This will prevent your cart activity from being destroyed when you start checkout activity and cart activity can handle the end result. 如上所述,最好与startActivityForResult一起使用。这将防止您在开始结帐活动时购物车活动被破坏,并且购物车活动可以处理最终结果。

Another option would be: 另一种选择是:

1.create a singleton data class to save all data. 1.创建一个单例数据类以保存所有数据。

2.declare getter,setter ,allClear and getInstance(static) methods. 2.声明getter,setter,allClear和getInstance(static)方法。

3.Get a instance of data class in the cart activity and populate list. 3.在购物车活动中获取数据类的实例并填充列表。

4.Get a instance of data class and call allClear to delete data in Checkout activity. 4.获取数据类的实例,然后调用allClear在Checkout活动中删除数据。

This approach would keep cart list data independent from different activities. 这种方法可以使购物车列表数据独立于不同的活动。 So all you need is call static getInstance method of data class and call allClear no matter which activity you are in. 因此,无论您处于哪个活动中,您都需要调用数据类的静态getInstance方法并调用allClear。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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