Passing List<T> through a bundle
I am currently working on an Android assignment for uni and i have a bit of a problem with lists.
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.
on my main Activity i have a List (contacts is a class to hold 2 strings, type and number)
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.
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)
then on my listview activity i do a bundle b = getintent().getextras();
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.
if i post the link to tutorial and my source code it might make more sense. As allways i appreacte any input at all
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开发者_JAVA技巧.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 should stress that i got it working with the following, but i can 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 to see if it is exceptable (it works):
Created a 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 on my ContactPicker Activity i did this:
Serializable l = nBundle.getSerializable("PHONENUMBERS");
ContactsList cl = (ContactsList)l;
List<Contacts> myContacts = cl.getList();
Just wanted to make sure that doing it this way was acceptable thanks
精彩评论