2017-03-16 17 views
0

Githubに渡し、Parcelableを使用して渡す:私はParcelableを実装するのArrayListを送信する方法のチュートリアルから理解https://www.javacodegeeks.com/2014/01/android-tutorial-two-methods-of-passing-object-by-intent-serializableparcelable.htmlArrayListのは、<CustomObject>ヌル

:私は意図してオブジェクトを渡すことについては、このチュートリアルを追ってきたhttps://github.com/jjvang/PassIntentDemo

あなたはこのような値の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); 
    } 

を私は大きさ2以上にArrayListに追加していくが、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

をしてくれてありがとう!! nullを得ていないとそれを受け取りますtipを使用すると、ArrayListを使用してアダプタを設定できるかどうかがわかります。 – ojboba

関連する問題