在Android中传递ArrayList

时间:2011-12-28 08:19:00

标签: android android-activity arraylist android-intent

我正在尝试在Android中的活动之间传递数组列表。列表包含字符串。我读过很多关于Parcelable的文章。我是否需要创建一个Parcelable来传递 String 数组列表?截至目前,我使用putStringArrayListExtra()和getSringArrayListExtra()通过意图传递列表。

以下是我的一些代码。

Intent load = new Intent(getApplicationContext(), HelloTabWidget.class);
        load.putStringArrayListExtra("albums", albums);  
        load.putStringArrayListExtra("songs", songs);  
        load.putStringArrayListExtra("artists", artists); 
        load.putStringArrayListExtra("fileName", fileName);  

这是我的onCreate方法,用于获取数组列表的acticity。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.songlist); 

    Bundle extras = getIntent().getExtras();
    isArtists = extras.getBoolean("artists");   
    isAlbums = extras.getBoolean("albums"); 
    isSongs = extras.getBoolean("songs");   

        Intent get = getIntent();  
        songs = get.getStringArrayListExtra("songs"); 
        artists = get.getStringArrayListExtra("artists");
        albums = get.getStringArrayListExtra("albums");
        fileName = get.getStringArrayListExtra("fileName"); 



    if(isArtists == true)       
        updateArtistsList(); 
    else if(isAlbums == true) 
        updateAlbumsList();  
    else if(isSongs == true) 
        updateSongList();    
}

检索列表的类应该从列表中的数据创建listView。每当我运行代码时,我都会在尝试制作列表时获得nullPointerExceptions。我知道我的listView代码工作,所以我已经将问题缩小到通过数组列表的意图。

提前致谢。

编辑: 以下是logcat的前几行。

12-28 03:03:42.313: E/AndroidRuntime(873): FATAL EXCEPTION: main
12-28 03:03:42.313: E/AndroidRuntime(873): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adam.mediaplayer/com.adam.mediaplayer.HelloTabWidget}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adam.mediaplayer/com.adam.mediaplayer.MakeListActivity}: java.lang.NullPointerException

2 个答案:

答案 0 :(得分:4)

以下是传递ArrayList的方法:

MyListClass.java - 自定义类

public class MyListClass implements Parcelable{

private int test;

public MyListClass()
{}

public MyListClass(Parcel read){
    test = read.readInt();
}

public int getTest() {
    return test;
}

public void setTest(int test) {
    this.test = test;
}

public static final Parcelable.Creator<MyListClass> CREATOR = 
    new Parcelable.Creator<MyListClass>() {

        @Override
        public MyListClass createFromParcel(Parcel source) {
            return new MyListClass(source);
        }

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

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

@Override
public void writeToParcel(Parcel arg0, int arg1) {
    arg0.writeInt(test);
}

}

<强> MyParcelable.java

public class MyParcelable implements Parcelable {

private List<MyListClass> arrList = new ArrayList<MyListClass>();
private int myInt = 0;
private String str = null;

public String getStr() {
    return str;
}

public void setStr(String str) {
    this.str = str;
}

public List<MyListClass> getArrList() {
    return arrList;
}

public void setArrList(List<MyListClass> arrList) {
    this.arrList = arrList;
}

public int getMyInt() {
    return myInt;
}

public void setMyInt(int myInt) {
    this.myInt = myInt;
}

MyParcelable() {
    // initialization
    arrList = new ArrayList<MyListClass>();
}

public MyParcelable(Parcel in) {
    myInt = in.readInt();
    str = in.readString();
    in.readTypedList(arrList, MyListClass.CREATOR);
}

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

@Override
public void writeToParcel(Parcel outParcel, int flags) {
    outParcel.writeInt(myInt);
    outParcel.writeString(str);
    outParcel.writeTypedList(arrList);
}

public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {

    @Override
    public MyParcelable createFromParcel(Parcel in) {
        return new MyParcelable(in);
    }

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

}

<强> MainAcitivty.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    arrList.add(new MyListClass());
    arrList.get(0).setTest(200);

    MyParcelable object = new MyParcelable();
    object.setMyInt(100);
    object.setArrList(arrList);

    Intent intent = new Intent(MainActivity.this,ReceiverParcel.class);
    intent.putExtra("parcel", object);
    startActivity(intent);
}

<强> ReceiverParcel.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle b = getIntent().getExtras();
    MyParcelable object = b.getParcelable("parcel");

    System.out.println(object.getArrList().get(0).getTest());
    System.out.println(object.getMyInt());

}

答案 1 :(得分:4)

这取决于arraylist的类型

  • putIntegerArrayListExtra(String name, ArrayList<Integer> value)

  • putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)

  • putStringArrayListExtra(String name, ArrayList<String> value)

  • putCharSequenceArrayListExtra(String name, ArrayList<CharSequence> value)

然后,您可以通过将put替换为get并使用键字符串作为参数来阅读下一个活动,例如

myIntent.getStringArrayListExtra("arrayListName");