Java 使用Parcelable传递Arraylist,传递null
Github: 我一直在遵循本教程关于通过意图传递对象的内容: 我从教程中了解到,如果只有1组值,则如何发送实现Parcelable的Arraylist:Java 使用Parcelable传递Arraylist,传递null,java,android,Java,Android,Github: 我一直在遵循本教程关于通过意图传递对象的内容: 我从教程中了解到,如果只有1组值,则如何发送实现Parcelable的Arraylist: public void PacelableMethod(){ Book mBook = new Book(); mBook.setBookName("Android Developer Guide"); mBook.setAuthor("Leon"); mBo
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);
}
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添加大小为2或更大的内容,但请注意,传递给下一个活动的ArrayList为null
我想了解是否必须以不同的方式添加到ArrayList,或者是否只是错误地发送/捕获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);
}
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();
}
}
请告知,谢谢 我认为您需要使用以下方法将阵列列表添加到意向附加中: intent.putparcelablearraylistracontact\u列表,单词
然后以不为空的方式接收它!!谢谢你的提示,我现在看看是否可以使用ArrayList设置适配器。