2

How to send an ArrayList to Another Activity and display there ? I Want to get data from another activity and send it to another activity and get there printed. I am only able to pass single string but not an array of strings

Code On Java File,

Main Activity:

EditText et1 , et2 , et3 , et4 ;
public final static String MESSAGE_KEY = "com.example.prabhav.myapplication.message";
ArrayList<String> ar , tr = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_on);
}

public void sm (View v)
{
    et1= (EditText) findViewById(R.id.name);
    String msg1 = et1.getText().toString();
    et2= (EditText) findViewById(R.id.dob);
    String msg2 = et2.getText().toString();
    et3= (EditText) findViewById(R.id.emailinput);
    String msg3 = et3.getText().toString();
    et4= (EditText) findViewById(R.id.clgi);
    String msg4 = et4.getText().toString();
    ar.add(msg1);
    ar.add(msg2);
    ar.add(msg3);
    ar.add(msg4);
    tr=ar;
    Intent intent = new Intent(this,SecAct.class);
    intent.putExtra("myarray",tr);
    startActivity(intent);

}

Another Activity:

Spinner s;
public final static String MESSAGE_KEY = "com.example.prabhav.myapplication.message";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent= getIntent();
    ArrayList<String> ls = (ArrayList<String>) getIntent().getSerializableExtra("myarray");
    ArrayAdapter<String> adptr=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ls);
    s= (Spinner) findViewById(R.id.sp);
    s.setAdapter(adptr);

    //  setContentView(R.layout.sec_lay);

}

2 Answers2

3

Once try as follows

MainActivity

Intent i = new Intent(this,SecAct.class);
i.putStringArrayListExtra("myarray", tr);
startActivity(i);

2nd Activity

ArrayList<String> list=(ArrayList<String>)getIntent().getStringArrayListExtra("myarray");
//use the list as you wish

Hope this will helps you.

Nagaraju V
  • 2,739
  • 1
  • 14
  • 19
0

try sending the arrayList using putParcelableArrayList like this:

Intent intent = new Intent(this,SecAct.class);
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("myArray", (ArrayList<? extends android.os.Parcelable>) tr);
        intent.putExtras(bundle);
        startActivity(intent);

And you can get the arrayList in the second activity like this:

Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        tr= bundle.getParcelableArrayList("myArray");}

i hope this will help

Hawraa Khalil
  • 261
  • 2
  • 12