Skip to content Skip to sidebar Skip to footer

Passing A List To Another Activity In Android

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 eas

Solution 1:

You can use putStringArrayListExtra from Intent

public Intent putStringArrayListExtra (String name, ArrayList value)

privatefinal List<String> selItemList;
  privateListViewmainListView=null;       

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

And in your XMLParser.class:

publicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getIntent().getExtras() != null) {
            for(String a : getIntent().getExtras().getStringArrayList("items_to_parse")) {
                Log.d("=======","Data " + a);
            }
        }

Solution 2:

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:

privateList<String> selItemList;
privateListView mainListView = null; 

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

    Button searchBtn = (Button) findViewById(R.id.searchButton);
    searchBtn.setOnClickListener(newOnClickListener() {
    publicvoidonClick(View v) {
        if (selItemList == null) {
            Toast.makeText(getApplicationContext(), "Please Make A Selection", Toast.LENGTH_SHORT).show();
        } else {
            String[] selItemArray = newString[selItemList.size()];
            // Copy your List of Strings into the Array, and then pass it in your intent// ....Intent intent = newIntent(Recipes2.this, XMLParser.class);
            intent.putExtra("items_to_parse", selItemArray);
            startActivityForResult(intent, 0);              
        }
    }
});

Solution 3:

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:

publicclassCategoryimplementsSerializable {
private List<YourObject> objects;

publicCategory() {
}

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

publicvoidsetObjects(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;

Post a Comment for "Passing A List To Another Activity In Android"