Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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);              
            }
        }
        });
like image 759
user676567 Avatar asked May 22 '11 09:05

user676567


2 Answers

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);
            }
        }
like image 75
ccheneson Avatar answered Oct 21 '22 07:10

ccheneson


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);              
        }
    }
});
like image 29
Houcine Avatar answered Oct 21 '22 08:10

Houcine