[英]Passing Arraylist<CustomObject> using Parcelable, passing null
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();
}
}
請指教,謝謝!
我相信您需要使用putParcelableArrayListExtra將數組列表添加到其他意圖中:intent.putParcelableArrayListExtra(Contact_list“,words)
然后使用getParcelableArrayListExtra接收它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.