Skip to content Skip to sidebar Skip to footer

How To Get The Arraylist Out Android Studio

I am unable to get my ArrayList out when it's on a different page. I would like to get the ArrayList out Android Studio. This is CartActivity class: public class CartActivity exten

Solution 1:

You should impliment Serializable in CartItem class and then pass from ProductActivity to CartActivity . Like this :

ProductActivity class.

Intent productListIntent= newIntent(this, CartActivity .class);
  productListIntent.putExtra("cartlist", ArrayList<CartItem>mcartItem);
startActivity(productListIntent);

and in CartActivity class

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);
      ArrayList<CartItem> mycart= newArrayList<CartItem>();
          mycart= (ArrayList<CartItem>)getIntent().getSerializableExtra("cartlist");
     listView = (ListView) findViewById(R.id.listView);
        CartAdapteradapter=null;
        adapter = newCartAdapter(this, R.layout.adapter_cart_item, mycart);
        listView.setAdapter(adapter);
    }

Solution 2:

It's not the best practice, but you can make your arrayList

publicstatic ArrayList list;

Then you can access it from ather activity using {class_name}.list

Solution 3:

You have to pass your list of your data via intent(as Intent is a message passing Object) From ! activity to another activity. For that you have to make that class Parcable or Serializable . How to make a class Parcable in andriod studio read this answer. Then put thah class into the intent that you are passing to startActivity() method like

intent.putParcelableArrayListExtra("key",your_list);

or

 intent.putExtra("key",your_class)// incase of single object

then get it to the onCreate() method of second activity like

getIntent().getParcelableArrayListExtra("your_key")

or

getIntent().getParcelableExtra()//  incase of single object

Hope it will help your

Post a Comment for "How To Get The Arraylist Out Android Studio"