Passing list <T> through the bundle
I am currently working on assigning Android to uni, and I have a little problem with lists.
Basically I followed the tutorial on using a list of type classes (in my case its contacts) to create a custom listAdapter to represent the list.
in my main activity I have a List (contacts is a class that contains 2 lines, type and number)
i then get all the numbers and their type from the selected contact in the contact list. then for each add create an instance of the class and add it to the list.
as soon as this is done, I will make a new intention (which basically is an Activity that has a list view, but in the subject of the dialogue), as soon as the intention is announced, I do i.putExtra and put the name and name of my list (I should emphasize that I had this work with the arraist, and not with the list).
then in my listview activity i am making b = getintent () package. getextras ();
then try to do List myContacts = b.get .... thers nothing that matches the list, theres arraylist and everything I try ruined the material in the tutorial that I read.
If I write a link to the tutorial and my source code, this may make more sense. As always, I generally access any input
thanks vade
Link: http://dustinbreese.blogspot.com/2009/12/creating-listview-with-alternating.html
main source code:
protected void getContactInfo(Intent intent) { ArrayList<String> contacts = new ArrayList<String>() ; 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); } List View Source: oh I have to emphasize that I got it working with the following, but I see some bad coding practice here!
public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); Bundle nBundle = getIntent().getExtras(); ArrayList<String> myArray = nBundle.getStringArrayList("PHONENUMBERS"); List<Contacts> myContacts = getData(myArray); ListView lv = (ListView)findViewById(R.id.contactPicker); //lv_arr = (String[])myArray.toArray(new String[0]); //setListAdapter(new ArrayAdapter<String>(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<Contacts> getData(ArrayList<String> array){ List<Contacts> myContacts = new ArrayList<Contacts>(); int myArrayLength = array.size(); for(int i = 0; i < myArrayLength;) { String type = array.get(i); ++i; String number = array.get(i); ++i; myContacts.add(new Contacts(type,number)); } return myContacts; } } EDIT:
After reading the comments below this post, I tried this and wanted to check if it is an exception (it works):
The created class called ContactsList looks like this:
import java.io.Serializable; import java.util.List; public class ContactsList implements Serializable { private List<Contacts> innerContacts; public ContactsList(List<Contacts> contacts){ this.innerContacts = contacts; } public List<Contacts> getList() { return this.innerContacts; } } then on my main i did
ContactsList l = new ContactsList(contacts); Intent i = new Intent(this,ContactPicker.class); i.putExtra("PHONENUMBERS", l); then in my ContactPicker job I did the following:
Serializable l = nBundle.getSerializable("PHONENUMBERS"); ContactsList cl = (ContactsList)l; List<Contacts> myContacts = cl.getList(); Just wanted to make sure it was, thanks
No one has answered this question yet.
See related questions: