简体   繁体   中英

How to pass a list from a class to an activity?

I declare a List object in a method of a java class, which gets filled with data inside this method. Now I want to pass the filled list to another activity. How should I do that?

if you are in a JAVA class and want to call the list in an Activity, them simply put the method return type as list and call it in the required Activity . Demo code is as follows

class ReturnList{
public List method(){
  List list=new ArrayList();
  return list;}
}
class ActivityDemo extends Activity{
  onCreate(){
 ///activity code
 ReturnList returnListObj=new ReturnList();
 List listData= returnListObj.method();
 //deal with list here 
 }}

First make your object class parcelable .

Pass data as a bundle

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

Retrieving the data:

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

Hope it helps :)

Define your class as below

public class CategoryEntity implements Parcelable{
public int id;
public String name;
public String imageOriginal;
public String imageThumb;
public String description;
public String status;
public String createdAt;
public String updatedAt;
public int imageDummy;

protected CategoryEntity(Parcel in) {
    id = in.readInt();
    name = in.readString();
    imageOriginal = in.readString();
    imageThumb = in.readString();
    description = in.readString();
    status = in.readString();
    createdAt = in.readString();
    updatedAt = in.readString();
    imageDummy = in.readInt();
}

public static final Creator<CategoryEntity> CREATOR = new Creator<CategoryEntity>() {
    @Override
    public CategoryEntity createFromParcel(Parcel in) {
        return new CategoryEntity(in);
    }

    @Override
    public CategoryEntity[] newArray(int size) {
        return new CategoryEntity[size];
    }
};

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(id);
    dest.writeString(name);
    dest.writeString(imageOriginal);
    dest.writeString(imageThumb);
    dest.writeString(description);
    dest.writeString(status);
    dest.writeString(createdAt);
    dest.writeString(updatedAt);
    dest.writeInt(imageDummy);
}
}

Pass your data using Bundle

startActivity(new Intent(CategoriesListingActivity.this, StoryDetailActivity.class)
                        .putExtra("List", your list with model class));

Handle your list in another activity

Bundle extra = getIntent().getExtras();
    if (extra != null) {
        storyList = (ArrayList<CategoryEntity>) extra.getParcelable("StoryListing");

    }

Hope this will helps you.

Pass your list in intent.putExtra(), for example

Intent intent = new Intent(getApplicationContext() , YourNextActivity.class); intent.putExtra("list" , (Serializable) yourList); startActivity(intent);

and for retrieve list in NextActivity

Intent intent = getIntent(); intent.getSerializableExtra("list");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM