1

I have RecyclerView in cart activity. I want to clear this RecyclerView on pressing checkout activity's complete order button.

Here I tried to describe the scenario: enter image description here

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Sehrish Fiaz
  • 57
  • 2
  • 7
  • I don't think you required to go back to Cart Page once you complete your order. You need to direct user to HomePage after Complete Order page. So handle the data source for Cart page when you click on Complete Order button. – nitinkumarp Jun 19 '17 at 07:11

4 Answers4

3

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. 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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
3

Just check start activity for result how it work refrance -https://stackoverflow.com/a/10407371/4741746

Than in onActivityResult method you can update your adapter by using notifyDataSetChanged() or refresh method

@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

Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55
  • i put this code but it does not cleared the recyclerview after checkout is proceeded. – Sehrish Fiaz Jun 19 '17 at 06:10
  • try with this one ArrayList yourNewList= new ArrayList (); adapter.Refresh(yourNewList); does this code execute ? – Sushant Gosavi Jun 19 '17 at 06:24
  • did you get callback of activity means ...put debugger in list activity onActivityResult method check if ArrayList yourNewList= new ArrayList (); adapter.Refresh(yourNewList); executed properly – Sushant Gosavi Jun 19 '17 at 06:26
1

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. Then your recyclerView will be cleared.

Devil10
  • 1,853
  • 1
  • 18
  • 22
0

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.

Another option would be:

1.create a singleton data class to save all data.

2.declare getter,setter ,allClear and getInstance(static) methods.

3.Get a instance of data class in the cart activity and populate list.

4.Get a instance of data class and call allClear to delete data in Checkout activity.

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.

G Singh
  • 156
  • 4