10

I've created a list and would like to pass the list to another activity but i'm getting an error on the putExtra statement when i create the intent. Just wondering is there any easy way to pass a List of Strings rather than a single String?

Thanks

private List<String> selItemList;
private ListView mainListView = null;       

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recipes);
        Button searchBtn = (Button) findViewById(R.id.searchButton);
        searchBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (selItemList == null) {
                Toast.makeText(getApplicationContext()," Please Make A Selection ", Toast.LENGTH_SHORT).show();
            } else {
                Intent intent = new Intent(Recipes2.this, XMLParser.class);
                intent.putExtra("items_to_parse", selItemList);
                startActivityForResult(intent, 0);              
            }
        }
        });
user676567
  • 1,119
  • 9
  • 20
  • 39
  • can you add the Error you got on the Logcat to you question ? – Houcine May 22 '11 at 10:06
  • Hi Houcine, Eclipse wont let me compile the above. The error is "The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, List)"? – user676567 May 22 '11 at 10:16

3 Answers3

20

You can use putStringArrayListExtra from Intent

public Intent putStringArrayListExtra (String name, ArrayList value)

  private final List<String> selItemList;
  private ListView mainListView = null;       

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recipes);
        Button searchBtn = (Button) findViewById(R.id.searchButton);
        searchBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (selItemList == null) {
                Toast.makeText(Recipes2.this," Please Make A Selection ", Toast.LENGTH_SHORT).show();
            } else {
                Intent intent = new Intent(Recipes2.this, XMLParser.class);
                intent.putStringArrayListExtra("items_to_parse", (ArrayList<String>) selItemList);
                startActivityForResult(intent, 0);              
            }
        }
        });

And in your XMLParser.class:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getIntent().getExtras() != null) {
            for(String a : getIntent().getExtras().getStringArrayList("items_to_parse")) {
                Log.d("=======","Data " + a);
            }
        }
ccheneson
  • 49,072
  • 8
  • 63
  • 68
  • 1
    Hi Ccheneson, I tried your solution and it failed on the putStringArrayListExtra line with a class cast exception: 05-22 12:11:09.267: ERROR/AndroidRuntime(2279): java.lang.ClassCastException: java.util.Arrays$ArrayList – user676567 May 22 '11 at 12:15
  • TypeCasting to `(ArrayList)` is really classy. I never knew we could TypeCast while passing through intents, all these days. – vss Jun 20 '17 at 14:18
9

You can't pass a List in Intent.putExtras(String name, List<?> list);. I think you can use an Array of String and pass it in putExtras like this:

private List<String> selItemList;
private ListView mainListView = null; 

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recipes);

    Button searchBtn = (Button) findViewById(R.id.searchButton);
    searchBtn.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        if (selItemList == null) {
            Toast.makeText(getApplicationContext(), "Please Make A Selection", Toast.LENGTH_SHORT).show();
        } else {
            String[] selItemArray = new String[selItemList.size()];
            // Copy your List of Strings into the Array, and then pass it in your intent
            // ....
            Intent intent = new Intent(Recipes2.this, XMLParser.class);
            intent.putExtra("items_to_parse", selItemArray);
            startActivityForResult(intent, 0);              
        }
    }
});
Jeankowkow
  • 814
  • 13
  • 33
Houcine
  • 24,001
  • 13
  • 56
  • 83
4

I know it is a little late and this question has an answer already but here is another way.

simply create another object define it as Serializable and give it a list variable and send that using putExtra on your intent like this:

public class Category implements Serializable {
private List<YourObject> objects;

public Category() {
}

public List<YourObject> getObjects() {
    return objects;
}

public void setObjects(List<YourObject> objects) {
    this.objects = objects;
}

and then for sending it do this:

Category cat = new Category();
cat.setObjects(objects);
intent.putExtra("listOfObjects",cat);
startActivity(intent);

and to get the object you have created do this:

Category cat = (Category) extras.get("listOfObjects");
cat.getObjects;
Pouya Danesh
  • 1,557
  • 2
  • 19
  • 36
  • java.lang.RuntimeException: Parcelable encountered IOException writing serializable object – Yaroslav Dukal Jul 13 '17 at 00:33
  • some objects are not Serializable . please check documentations to see which one are or are not. https://developer.android.com/reference/java/io/Serializable.html – Pouya Danesh Jul 13 '17 at 03:56