将ArrayList <item>的数据传递给Android中的另一个Activity?</item>

时间:2012-12-31 07:22:30

标签: android list listview android-intent arraylist

  

可能重复:
  Help with passing ArrayList and parcelable Activity

我已经尝试使用putExtra(),但无济于事。

当我使用putExtra()时,会出现语法错误。并且唯一的方法(似乎)是将所选的整个数据转换为字符串。但问题是,我只需要显示部分数据而不是全部数据。因此,我认为这不是方法。

我要做的是发送ArrayList数据。第一个Activity是程序在Array中搜索数据,第二个Activity用于显示部分数据。

我需要发送onClick()的数据ListView。我需要数据在ArrayList<Item>

在有人回答或评论此问题之前,请先了解一下我的声誉。因此,凭借这么多声誉,我想你知道我是新人。只需在低估之前考虑这一点。

1 个答案:

答案 0 :(得分:1)

假设您有一个这样的类,它实现了Parcelable

public class CustomParcelableClass implements Parcelable {
    public String string;
    public int i;

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(i);
        dest.writeString(string);
    }

            private Item(Parcel in) {
             string= in.readString();
            }
}

你可以像这样Intent

ArrayList<CustomParcelableClass> myParcelableList = 
          new ArrayList<CustomParcelableClass>();
myParcelableList.add(...);
...
yourIntent.putParcelableArrayListExtra(LIST, myParcelableList);

其中LIST类似于:

public static final String LIST = "LIST";

在第二节课中,在像onNewIntent(Intent i)这样的方法中,通过像

这样的行检索ArrayList
ArrayList<CustomParcelableClass> myParcelableList = 
        i.getExtras().getParcelableArrayList(LIST);

我不知道为什么这不适合你。