0

what i have is three activities, the first one called penddingorders which i make an array list inside it and i add strings to it like this :

 ArrayList prodlist=new ArrayList();
    for(int z=0;z<=mylist.size();z++){
    if(id.equals(mylist.get(arg2).getOrderId()))
    {
    product=mylist.get(arg2).getProductName();
    quantity=mylist.get(arg2).getQuantity();
    unit=mylist.get(arg2).getUnit();
    price=mylist.get(arg2).getPrice();
    totalprice=mylist.get(arg2).getTotalPrice();
                       totalpriceAfterDiscount=mylist.get(arg2).getPriceAfterDiscount();
                                 note=mylist.get(arg2).getNote();
                                prodlist.add(product);
                                prodlist.add(quantity);
                                prodlist.add(unit);
                                prodlist.add(totalprice);
                                prodlist.add(price);
                                prodlist.add(totalpriceAfterDiscount);
                                prodlist.add(note);
                                arg2++;
                            }

and i have print it in the logcat and it shows correctly:

 Log.e("product list",prodlist+""); 

and i have send it from the first activity to the third activity like this , though i should pass through the second one before i go to the third one :

Intent intent = new Intent(PenddingOrders.this, ProductInfoDetails.class);
intent.putStringArrayListExtra("prodlist", prodlist);
startActivity(intent);

and in the third activity i get the array list through this code :

try{
prodlist = getIntent().getStringArrayListExtra("prodlist");                     
}
catch(Exception e){
e.printStackTrace();
}
    Log.e("prodlist",prodlist+"");

but in the logcat i get null value .. so what i am doing wrong please help ??

user3534834
  • 85
  • 1
  • 2
  • 7

3 Answers3

0

you can set your property using getter setter so you can manage your value very well. and make it parceable. so you can use it Extras ArrayList of it into next Activity.

Please follow this https://stackoverflow.com/a/15731120/942224

Community
  • 1
  • 1
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
0

This is my ans on stackoverflow please checked and hope it's help alot:

Java / Android ArrayList in different Activity

Community
  • 1
  • 1
Farhan Shah
  • 2,344
  • 7
  • 28
  • 54
0

Better to use Android Application class to persist data. For reference link

public class MyApplication extends Application {
    private ArrayList<String> list;

    public void setValue(ArrayList<String> list) {
        this.list = list;
    }

    public ArrayList<String> getValue() {
        return list;
    }

}

Step-1 First set the value from your Activity

MyApplication globalVariable = (MyApplication) getApplicationContext();
globalVariable.setValue(prodlist)// Set your ArraList that you want to store

Step-2 Retrieve the value in other activity like

    MyApplication globalVariable = (MyApplication) getApplicationContext();

    // Get ArrayList from global/application context
    ArrayList<String> list = globalVariable.getValue();

and don't forget to register in Manifest.xml

<application
        android:name="YOUR_PACKAGE_NAME.MyApplication "
        -----------------------
Community
  • 1
  • 1
Amit Gupta
  • 8,914
  • 1
  • 25
  • 33