Intent.Putextra List

Intent.putExtra List

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.

intent.putExtra a list of Parcelable objects and enums

You don't need to make List parcelable. To put it, just use existing method.

final Intent i = new Intent(this,SecondActivity.class);
final ArrayList<Product> list = new ArrayList< Product >();
i.putParcelableArrayListExtra(PRODUCT_KEY, list);
startActivity(i);

To retrieve almost in the same way.

final ArrayList<Product> list = this.getIntent()
.getParcelableArrayListExtra(PRODUCT_KEY);

UPDATE

To answer you newer question. You cannot pass generic List inside. Because by List you could have different collection. To know system, which type to use you need to have explicit type.

// This will work. 
final ArrayList<Department> departments = getSpecificDepartments();
intent.putParcelableArrayListExtra(DEPARTMENTS_KEY, departments);

Passing a List from one Activity to another

You can pass an ArrayList<E> the same way, if the E type is Serializable.

You would call the putExtra (String name, Serializable value) of Intent to store, and getSerializableExtra (String name) for retrieval.

Example:

ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList);

In the other Activity:

ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");

Please note that serialization can cause performance issues: it takes time, and a lot of objects will be allocated (and thus, have to be garbage collected).

put list of objects in `Intent`

It is not recommended to pass a list of objects between your activities. As @IlyaMaier said, a more robust way is to initialize your ViewModel with your application context and then reuse the same LiveData in your MainActivity.

if you want to continue the same way, there are two reasons why your MainActivity is not being called.

  1. The observer is not being able to receive new data in your ViewModel Class.
  2. The data class should extend Pacelable & also add annotation @Parcelize.

Like this:

@Parcelize
data class Channels (
var Id: Int?,
var Title: String?,
var CallLetter: String?,
var ChannelPosition: Int?
) : Parcelable

How to put a List in intent

You have to instantiate the List to a concrete type first. List itself is an interface.

If you implement the Parcelable interface in your object then you can use the putParcelableArrayListExtra() method to add it to the Intent.

How can I pass ListString as an Intent extra?

This is used to send using intent from one activity.

            Intent intent = getIntent();
intent.putStringArrayListExtra("votedBy",new ArrayList<>(votedBy));

To receive this can be used;

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

Pass list of objects from one activity to other activity in android

Use parcelable. Here is how you will do it:

public class SharedBooking implements Parcelable{

public int account_id;
public Double betrag;
public Double betrag_effected;
public int taxType;
public int tax;
public String postingText;

public SharedBooking() {
account_id = 0;
betrag = 0.0;
betrag_effected = 0.0;
taxType = 0;
tax = 0;
postingText = "";
}

public SharedBooking(Parcel in) {
account_id = in.readInt();
betrag = in.readDouble();
betrag_effected = in.readDouble();
taxType = in.readInt();
tax = in.readInt();
postingText = in.readString();
}

public int getAccount_id() {
return account_id;
}
public void setAccount_id(int account_id) {
this.account_id = account_id;
}
public Double getBetrag() {
return betrag;
}
public void setBetrag(Double betrag) {
this.betrag = betrag;
}
public Double getBetrag_effected() {
return betrag_effected;
}
public void setBetrag_effected(Double betrag_effected) {
this.betrag_effected = betrag_effected;
}
public int getTaxType() {
return taxType;
}
public void setTaxType(int taxType) {
this.taxType = taxType;
}
public int getTax() {
return tax;
}
public void setTax(int tax) {
this.tax = tax;
}
public String getPostingText() {
return postingText;
}
public void setPostingText(String postingText) {
this.postingText = postingText;
}
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(account_id);
dest.writeDouble(betrag);
dest.writeDouble(betrag_effected);
dest.writeInt(taxType);
dest.writeInt(tax);
dest.writeString(postingText);

}

public static final Parcelable.Creator<SharedBooking> CREATOR = new Parcelable.Creator<SharedBooking>()
{
public SharedBooking createFromParcel(Parcel in)
{
return new SharedBooking(in);
}
public SharedBooking[] newArray(int size)
{
return new SharedBooking[size];
}
};

}

Passing the data:

Intent intent = new Intent(getApplicationContext(),YourActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("data", sharedBookingObject);
intent.putExtras(bundle);
startActivity(intent);

Retrieving the data:

Bundle bundle = getIntent().getExtras();
sharedBookingObject = bundle.getParcelable("data");

How to putextra arraylistobject with if statement and show it in other activity

I don't quite understand with your question, I think you probably need to pass the criteria with the list. Something like this:

intent.putExtra("listbarang", goodsList);
// Criteria for at least one item in the list
intent.putExtra("criteria", 1);

Then process the extra:

int criteria;

Intent intent = getIntent();
goodsList = (List<Goods>) intent.getSerializableExtra("listbarang");
criteria = intent.getIntExtra("criteria", 0);

// do something based on criteria
for(Goods good: goodsList) {
if(good.getStock >= criteria) {
// process the good.
}
}

Array List of objects via intent

You can make your objects implement Parcelable and use putParcelableArrayListExtra. Alternatively, you can serialize your objects in some way and put the byte array of your serialized objects.



Related Topics



Leave a reply



Submit