Github上:https://github.com/jjvang/PassIntentDemo传递的ArrayList <CustomObject>使用Parcelable,传递null
我一直在关注这个教程有关的意图传递对象:https://www.javacodegeeks.com/2014/01/android-tutorial-two-methods-of-passing-object-by-intent-serializableparcelable.html
我从教程了解如何发送实现Parcelable一个ArrayList如果你只有1组值是这样的:
public void PacelableMethod(){
Book mBook = new Book();
mBook.setBookName("Android Developer Guide");
mBook.setAuthor("Leon");
mBook.setPublishTime(2014);
Intent mIntent = new Intent(this,ObjectTranDemo2.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable(PAR_KEY, mBook);
mIntent.putExtras(mBundle);
startActivity(mIntent);
}
我已经安排了代码,所以我可以继续添加到ArrayList尺寸大于或等于2,但注意到我的ArrayList传递给下一个活动一片空白。
我想了解,如果我不得不以不同的方式添加到ArrayList或如果我只是不正确地发送/捕获Arraylist。
尝试更改代码如下:
public void PacelableMethod(){
ArrayList<Book> words = new ArrayList<Book>();
words.add(new Book("red", "yes", 1));
words.add(new Book("mustard", "yes", 1));
Toast.makeText(this, "" + words, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,ObjectTranDemo2.class);
intent.putExtra("Contact_list", words);
Bundle mBundle = new Bundle();
intent.putExtras(mBundle);
startActivity(intent);
}
public class ObjectTranDemo2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<Book> myList = getIntent().getParcelableExtra("Contact_list");
Toast.makeText(this, "" + myList, Toast.LENGTH_SHORT).show();
}
}
请指教,谢谢!
没有得到空!!谢谢你的接受它提示,现在我会看到是否可以使用ArrayList来设置适配器。 – ojboba