使用Parcelable传递Arraylist ,传递null(Passing Arraylist 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(); } }

请指教,谢谢!

Github: https://github.com/jjvang/PassIntentDemo

I've been following this tutorial about passing object by intent: https://www.javacodegeeks.com/2014/01/android-tutorial-two-methods-of-passing-object-by-intent-serializableparcelable.html

I understand from the tutorial how to send an Arraylist implementing Parcelable if you have only 1 set of values like this:

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); }

I have arranged the code so I can continue to add to the ArrayList to size 2 or greater but notice that the ArrayList I pass to the next activity is null.

I would like to understand if I would have to add to the ArrayList differently or if I am just sending/catching the Arraylist incorrectly.

Trying code change like this:

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(); } }

Please advise, thank you!

最满意答案

我相信你需要使用putParcelableArrayListExtra将你的数组列表添加到intent extras中:intent.putParcelableArrayListExtra(Contact_list“,words)

然后用getParcelableArrayListExtra接收它

I believe you need to add your array list to the intent extras using putParcelableArrayListExtra: intent.putParcelableArrayListExtra(Contact_list", words)

then receive it with getParcelableArrayListExtra

更多推荐