【问题标题】:How to pass a list from a class to an activity?如何将列表从类传递到活动?
【发布时间】:2019-01-26 13:44:19
【问题描述】:

我在 java 类的方法中声明了一个 List 对象,该对象在该方法中填充了数据。现在我想将填充的列表传递给另一个活动。我该怎么做?

【问题讨论】:

  • 您必须将其放入Bundle,然后您可以将其与Intent 一起传递。

标签: java android list class android-activity


【解决方案1】:

如果您在 JAVA 类中并想在 Activity 中调用列表,他们只需将方法返回类型设置为列表并在所需的 Activity 中调用它。演示代码如下

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 
 }}

【讨论】:

    【解决方案2】:

    首先将您的对象类设为parcelable

    以包的形式传递数据

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

    检索数据:

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

    希望对你有帮助:)

    【讨论】:

      【解决方案3】:

      如下定义你的类

      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);
      }
      }
      

      使用 Bundle 传递您的数据

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

      在其他活动中处理您的列表

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

      希望对你有帮助。

      【讨论】:

        【解决方案4】:

        在intent.putExtra()中传递你的列表,例如

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

        在 NextActivity 中检索列表

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

        【讨论】:

          猜你喜欢
          • 2017-11-13
          • 1970-01-01
          • 1970-01-01
          • 2016-03-01
          • 2012-11-12
          • 1970-01-01
          • 1970-01-01
          • 2012-07-05
          • 1970-01-01
          相关资源
          最近更新 更多