0

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);
user1105975
  • 121
  • 2
  • 11

4 Answers4

2
ArrayList<String> mystringArray = getintent().getStringArrayListExtra("customers1");
Changwei Yao
  • 13,051
  • 3
  • 25
  • 22
0

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

Activity1.cutomers

Housefly
  • 4,324
  • 11
  • 43
  • 70
  • read above two answer..that will be helpful..Dont use static cause that's not reliable,creates memory leaks..also see http://stackoverflow.com/questions/2475978/using-static-variables-in-android – MKJParekh May 07 '12 at 05:58
  • hmm...yeah, otherwise its too obvious :P – Housefly May 07 '12 at 06:00
  • yes it it obvious :)..that developer use it..but going through documentation and good blogs..you will find that Static can make how much trouble for you.. – MKJParekh May 07 '12 at 06:04
  • hi,sory.in Avtivity2 i use Activity1.customers=customers1; but in customers1 null value is there. – user1105975 May 07 '12 at 06:48
0

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

In Activity2.class:

Activity1.customers;

Andras Balázs Lajtha
  • 2,576
  • 25
  • 32
0

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");  
    }
Khan
  • 7,585
  • 3
  • 27
  • 44