2017-03-16 75 views
0

Github上:https://github.com/jjvang/PassIntentDemo傳遞的ArrayList <CustomObject>使用Parcelable,傳遞null

我一直在關注這個教程有關的意圖傳遞對象:https://www.javacodegeeks.com/2014/01/android-tutorial-two-methods-of-passing-object-by-intent-serializableparcelable.html

我從教程瞭解如何發送實現Parcelable一個ArrayList如果你只有1組值是這樣的:

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

請指教,謝謝!

回答

0

我相信你需要使用putParcelableArrayListExtra您的數組列表添加到意圖演員: intent.putParcelableArrayListExtra(CONTACT_LIST」字)

然後用getParcelableArrayListExtra

+0

沒有得到空!!謝謝你的接受它提示,現在我會看到是否可以使用ArrayList來設置適配器。 – ojboba