【问题标题】:Passing Arraylist<CustomObject> using Parcelable, passing null使用 Parcelable 传递 Arraylist<CustomObject>,传递 null
【发布时间】:2017-08-07 05:21:16
【问题描述】:

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 为空。

我想了解我是否需要以不同的方式添加到 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();
    }
}

请指教,谢谢!

【问题讨论】:

    标签: java android


    【解决方案1】:

    我相信您需要使用putParcelableArrayListExtra 将您的数组列表添加到 Intent Extras: intent.putParcelableArrayListExtra(Contact_list", words)

    然后用getParcelableArrayListExtra接收它

    【讨论】:

    • 不会为空!!感谢您的提示,我现在看看是否可以使用 ArrayList 设置适配器。
    猜你喜欢
    • 2013-07-30
    • 2012-06-12
    • 2019-07-30
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    相关资源
    最近更新 更多