user1105975
user1105975

Reputation: 121

how to pass stringarraylist values from one class to another class in android

hi i would like to send string arraylist values from one class another class.i tried using bundle concept but in second class in arraylist showing null value.plaese any one suggest me wherre did mistake..

  Activity1.class:
 public static ArrayList<String> customers = new ArrayList<String>();
        customers.add("radha");
        customers.add("aswini");
           Intent i=new Intent(Activity1 .this,Activity2.class);
         i.putExtra("customers1", customers);
        Log.i("arrayvalues1",""+ customers);
        startActivity(i);

     Activity2.class:
     String[] mystringArray = getIntent().getStringArrayExtra("customers1");
    Log.i("arrayvalues2",""+ mystringArray);

Upvotes: 0

Views: 153

Answers (4)

Khan
Khan

Reputation: 7605

For Activity 1:

 ArrayList<String> Customers = new ArrayList<String>();
 Intent i=new Intent(Activity1 .this,Activity2.class);
 i.putStringArrayListExtra("customers1",(ArrayList<String>) Customers);         
 startActivity(i);

For Activity 2:

 ArrayList<String> Customers = new ArrayList<String>();
 Bundle extra=getIntent().getExtras();
    if(extra!=null){
        Customers =extra.getStringArrayList("customers1");  
    }

Upvotes: 0

If you create a public static variable, you can access it in a static way:

In Activity2.class:

Activity1.customers;

Upvotes: 0

Housefly
Housefly

Reputation: 4422

when the arraylist is public static, you can directly access it using classname.arraylist na.

Activity1.cutomers

Upvotes: 0

Changwei Yao
Changwei Yao

Reputation: 13101

ArrayList<String> mystringArray = getintent().getStringArrayListExtra("customers1");

Upvotes: 2

Related Questions