问题
I kind of have a 2 part question here.
1) How can I populate my ListView so that strings are what is displayed, but when items are selected, a non-visible id value (contact id from phone contacts) is the value that is actually used?
2) I have a ListView that's using multipleChoice mode for item selections. It's popluated with names from my contacts list. When I select an item in the ListView, I want that selected item to trigger a call to my SqLite routine to store the value into a database record. How do I make this event fire when an item is checked in the listview?
This is my layout XML;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/lvContacts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
/>
</LinearLayout>
And here is the code I am using to populate my ListView;
private void fillData() {
final ArrayList<String> contacts = new ArrayList<String>();
// Let's set our local variable to a reference to our listview control
// in the view.
lvContacts = (ListView) findViewById(R.id.lvContacts);
String[] proj_2 = new String[] {Data._ID, Phone.DISPLAY_NAME, CommonDataKinds.Phone.TYPE};
cursor = managedQuery(Phone.CONTENT_URI, proj_2, null, null, null);
while(cursor.moveToNext()) {
// Only add contacts that have mobile number entries
if ( cursor.getInt(2) == Phone.TYPE_MOBILE ) {
String name = cursor.getString(1);
contacts.add(name);
}
}
// Make the array adapter for the listview.
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
contacts);
// Let's sort our resulting data alphabetically.
aa.sort(new Comparator<String>() {
public int compare(String object1, String object2) {
return object1.compareTo(object2);
};
});
// Give the list of contacts over to the list view now.
lvContacts.setAdapter(aa);
}
I had hoped to be able to use something like the onClick event for each checked item, but have made not one bit of progress.
Any help would be so very appreciated. Thanks.
回答1:
I think solution for you problem is using custom list adapter witch each item include contact name and contact ID, here is detail:
1) Try to create custom contact item bean include 2 properties: contactID, contactName
public class contactItem{
private long contactID;
private String contactName;
//...
}
Create CustomContactAdapter:
public class CustomContactAdapter extends ArrayAdapter<contactItem>{
ArrayList<contactItem> itemList = null;
//Constructor
public CustomContactAdapter (Context context, int MessagewResourceId,
ArrayList<contactItem> objects, Handler handler) {
//Save objects and get LayoutInflater
itemList = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final ReceiveMailStruct contact= items.get(position);
if (contact!= null) {
view = inflater.inflate(R.layout.layout_display_contact_item, null);
//Set view for contact here
}
}
}
Search custom adapter for more information
2)To handler click event at ListView you must register an handler for listitem: Step1: Register handler to List item:
lvContacts.setOnItemClickListener(new HandlerListClickEvent());
Step2: Implement proccess when item click (check/uncheck)
class HandlerListClickEvent implements OnItemClickListener {
public void onItemClick( AdapterView<?> adapter, View view, int position, long id ) {
//Get contact ID here base on item position
}
Hope it help, Regards,
来源:https://stackoverflow.com/questions/4141124/android-listview-checkbox-selection