Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent.putExtra List [duplicate]

Tags:

android

People also ask

What is the putExtra () method used with intent for?

For this, Intent will start and the following methods will run: putExtra() method is used for send the data, data in key-value pair key is variable name and value can be Int, String, Float etc. getStringExtra() method is for getting the data(key) which is send by above method.

Is intent deprecated?

The Intent is not deprecated the problem is with your itemClickable. class because it is not recognized.

What is the importance of the putExtra () method in android how it is different from setData ()?

putExtra allows you to add primitive (or parcelable) key-value pairs. setData is limited to passing an Uri . setData is conventionally used for the case of requesting data from another source, such as in startActivityForResult. but an uri can be sent through putextra also.


Assuming that your List is a list of strings make data an ArrayList<String> and use intent.putStringArrayListExtra("data", data)

Here is a skeleton of the code you need:

  1. Declare List

    private List<String> test;
    
  2. Init List at appropriate place

    test = new ArrayList<String>();
    

    and add data as appropriate to test.

  3. Pass to intent as follows:

    Intent intent = getIntent();  
    intent.putStringArrayListExtra("test", (ArrayList<String>) test);
    
  4. Retrieve data as follows:

    ArrayList<String> test = getIntent().getStringArrayListExtra("test");
    

Hope that helps.


If you use ArrayList instead of list then also your problem wil be solved. In your code only modify List into ArrayList.

private List<Item> data;

you can do it in two ways using

  • Serializable

  • Parcelable.

This examle will show you how to implement it with serializable

class Customer implements Serializable
{
   // properties, getter setters & constructor
}

// This is your custom object
Customer customer = new Customer(name, address, zip);

Intent intent = new Intent();
intent.setClass(SourceActivity.this, TargetActivity.this);
intent.putExtra("customer", customer);
startActivity(intent);

// Now in your TargetActivity
Bundle extras = getIntent().getExtras();
if (extras != null)
{
    Customer customer = (Customer)extras.getSerializable("customer");
    // do something with the customer
}

Now have a look at this. This link will give you a brief overview of how to implement it with Parcelable.

Look at this.. This discussion will let you know which is much better way to implement it.

Thanks.