Github:https://github.com/jjvang/PassIntentDemo
我一直在按照本教程关于按意图传递对象:https://www.javacodegeeks.com/2014/01/android-tutorial-two-methods-of-passing-object-by-intent-serializableparcelable.html
我从教程中了解如果只有一组这样的值,如何发送一个实现Parcelable的Arraylist:
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是null。
我想了解是否必须以不同方式添加到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();
}
}
请指教,谢谢!
答案 0 :(得分:0)
我相信你需要使用putParcelableArrayListExtra将你的数组列表添加到intent extras: intent.putParcelableArrayListExtra(Contact_list“,words)
接收它