Mazher Khan
Mazher Khan

Reputation: 17

How to send and receive ArrayList<String> using Intent?

I am new in Android. I need your help. My Problem is - I have 2 classes Nplist.java and Addcustomer.java. In my AddCustomer class,One TextView and one Button is there to go Nplist class. In my Nplist class there is checklist and this checklist is coming from database and all the checked values are stored in ArrayList<String> and one Button is used to go back to AddCustomer class. I want that ArrayList<String> which is in Nplist to by display in my AddCustomer class Textview . I haved tried these but my Addcustomer class crashed.

1.Nplist.class

add.setOnClickListener(new View.OnClickListener() {<br>
   @Override<br>
            public void onClick(View view) {<br>
                      Bundle extra=new Bundle();<br>
                         extra.putSerializable("objects",checkedList);<br>
                    Intent intent = new Intent(Nplist.this, AddCustomer.class);<br>
                        intent.putExtra("extra",extra);<br>
                        startActivity(intent);<br>
});

2.AddCustomer.class

onCrete()...{
 Bundle extra = getIntent().getBundleExtra("extra");<br>
    ArrayList<String> object = (ArrayList<String>)extra.getSerializable("objects");<br>
    for (String str : object) {<br>
        getnp.append(str + "\n");<br>
}
    }

What do you expect the result to be? - What is the actual result you get? (Please include any errors.) When i go like this Nplist-->AddCustomer its working well but crash on ( AddCustomer-->Nplist-->AddCustomer)

Upvotes: 0

Views: 306

Answers (2)

frogatto
frogatto

Reputation: 29285

Using putStringArrayListExtra method you can send an array list of strings along with the intent.

Sender side:

Intent intent = ...
intent.putStringArrayListExtra("THE_KEY", theList);

Receiver side:

ArrayList<String> theList = intent.getStringArrayListExtra("THE_KEY");

Upvotes: 0

Piash Sarker
Piash Sarker

Reputation: 466

It's because when you are coming back to your AddCustomer Activity the list is null . You can solve this problem by making a global class which will store the list in a Static Field , And You can access that list from any class or Activity you want to. Try out below solution .

Global.java Class is as below :

public class Global {

    private static ArrayList<String> object = new ArrayList<>();

    public static ArrayList<String> getObject() {
        return object;
    }

    public static void setObject(ArrayList<String> object) {
        Global.object = object;
    }
}

From NpList.java class set the value of the list as below :

add.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
      Global.setObject(checkedList); 
      Intent intent = new Intent(Nplist.this, AddCustomer.class);
      startActivity(intent);
   }
});

Now Access in AdCustomer.java as below :

onCreate()...{
   Bundle extra = getIntent().getBundleExtra("extra");
   ArrayList<String> object = Global.getObject(); 
   for (String str : object) {
        getnp.append(str + "\n");
   }
}

This maybe helpful for you.

Upvotes: 1

Related Questions