Passing List<T> through a bundle
<p>I am currently working on an Android assignment for uni and i have a bit of a problem with lists.</p>

<p>Basically i have been following a guide on using a list of type class (in my case its contacts) to make a custom listAdapter for a list view.</p>

<p>on my main Activity i have a List (contacts is a class to hold 2 strings, type and number)</p>

<p>i then get all the numbers and their type from a selected contact in the contacts list. then for each one add create an instance of the class and add that to the list.</p>

<p>once that is done i make a new intent (which is basically an Activity which has a list view but in theme of a dialog) once declared the intent i do a i.putExtra and put a name and the name of my list ( i must stress that i had this working fine with an arraylist not a list.. confusing) </p>

<p>then on my listview activity i do a bundle b = getintent().getextras();</p>

<p>then try to do List myContacts = b.get.... thers nothing which matches a list, theres an arraylist and everything i try kinds of messes up the stuff on the tutorial i read.</p>

<p>if i post the link to tutorial and my source code it might make more sense.
As allways i appreacte any input at all</p>

<p>thanks Vade</p>

<p>Link: <a href="http://dustinbreese.blogspot.com/2009/12/creating-listview-with-alternating.html" rel="nofollow">http://dustinbreese.blogspot.com/2009/12/creating-listview-with-alternating.html</a></p>

<p>main source code:</p>

<pre><code> protected void getContactInfo(Intent intent)
{
     ArrayList&lt;String&gt; contacts = new ArrayList&lt;String&gt;() ;
     String name ="";
   Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);      
   while (cursor.moveToNext()) 
   {           
       String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
       name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

       String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

       if ( hasPhone.equalsIgnoreCase("1"))
           hasPhone = "true";
       else
           hasPhone = "false" ;

       if (Boolean.parseBoolean(hasPhone)) 
       {
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);

        while (phones.moveToNext()) 
        {
         String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
         String type = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
         contacts.add(type);
         contacts.add(number);

            //phoneNumber.add(new String("Hello"));





        }
        phones.close();
       }
   }
   cursor.close();


  Intent i = new Intent(this,ContactPicker.class);
  i.putExtra("PHONENUMBERS", contacts);

  startActivity(i);

}
</code></pre>

<p>List View Source: oh i should stress that i got it working with the following, but i can see some bad coding practice here!</p>

<pre><code>public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    Bundle nBundle  = getIntent().getExtras();
    ArrayList&lt;String&gt; myArray = nBundle.getStringArrayList("PHONENUMBERS");
    List&lt;Contacts&gt; myContacts = getData(myArray);

    ListView lv = (ListView)findViewById(R.id.contactPicker);
    //lv_arr = (String[])myArray.toArray(new String[0]);
    //setListAdapter(new ArrayAdapter&lt;String&gt;(ContactPicker.this, android.R.layout.simple_list_item_1,lv_arr));

    ListAdapter adapter = new ContactsAdapter(this,myContacts,
            android.R.layout.simple_expandable_list_item_2, new String[] {
            Contacts.KEY_TYPE, Contacts.KEY_NUMBER}, new int[]{
            android.R.id.text1,android.R.id.text2});
    this.setListAdapter(adapter);
    }


    //
    private List&lt;Contacts&gt; getData(ArrayList&lt;String&gt; array){
        List&lt;Contacts&gt; myContacts = new ArrayList&lt;Contacts&gt;();
        int myArrayLength = array.size();
        for(int i = 0; i &lt; myArrayLength;)
        {
            String type = array.get(i);
            ++i;
            String number = array.get(i);
            ++i;
            myContacts.add(new Contacts(type,number));

        }
        return myContacts;

    }

}
</code></pre>

<p>EDIT:</p>

<p>After reading the comments below this post i tried this and wanted to check to see if it is exceptable (it works):</p>

<p>Created a class called ContactsList looks like this:</p>

<pre><code>import java.io.Serializable;
</code></pre>

<p>import java.util.List;</p>

<p>public class ContactsList implements Serializable   {</p>

<pre><code>private List&lt;Contacts&gt; innerContacts;

public ContactsList(List&lt;Contacts&gt; contacts){
    this.innerContacts = contacts;
}
public List&lt;Contacts&gt; getList()
{
    return this.innerContacts;
}
</code></pre>

<p>}</p>

<p>then on my main i did ContactsList l = new ContactsList(contacts);
 Intent i = new Intent(this,ContactPicker.class);
      i.putExtra("PHONENUMBERS", l);</p>

<p>then on my ContactPicker Activity i did this:</p>

<pre><code>Serializable l = nBundle.getSerializable("PHONENUMBERS");
    ContactsList cl = (ContactsList)l;
    List&lt;Contacts&gt; myContacts = cl.getList();
</code></pre>

<p>Just wanted to make sure that doing it this way was acceptable
thanks</p>
