贴一下代码:
望各位前辈指点啊,代码实现了获取手机联系人的功能,但现在面临的问题是如何防止list滑动时混乱的发生,以及如何取listview值?public class Getcontacts extends Activity {
private ListView contactList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getcontacts);
contactList = (ListView) findViewById(R.id.contactList);
contactList.setItemsCanFocus(false);
contactList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//cb=(CheckBox)findViewById(R.id.contactsCheckBox);
getContactsList();
}
private void getContactsList() {
ArrayList<HashMap<String, Object>> contactsListItem = new ArrayList<HashMap<String, Object>>();
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext())
{
HashMap<String, Object> map = new HashMap<String, Object>();
String phoneName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
map.put("nameTxt", phoneName);
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.compareTo("1") == 0)
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{
String strphoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String phoneNumber="";
phoneNumber+=strphoneNumber+" ";
map.put("numberTxt", phoneNumber);
}
phones.close();
}
contactsListItem.add(map);
}
SimpleAdapter contactListAdapter = new SimpleAdapter(this, contactsListItem,R.layout.contactslistitem,new String[] {"nameTxt", "numberTxt"}, new int[] {R.id.nameTxt,R.id.numberTxt});
//ArrayAdapter<HashMap<String, Object>> contactListAdapter = new ArrayAdapter<HashMap<String, Object>>(this, android.R.layout.simple_list_item_multiple_choice, contactsListItem); contactList.setAdapter(contactListAdapter);
cursor.close();
contactList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?>parent, View view, int position, long id)
{
//如何取值?
}
});
}}
望各位前辈指点啊,代码实现了获取手机联系人的功能,但现在面临的问题是如何防止list滑动时混乱的发生,以及如何取listview值?public class Getcontacts extends Activity {
private ListView contactList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getcontacts);
contactList = (ListView) findViewById(R.id.contactList);
contactList.setItemsCanFocus(false);
contactList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//cb=(CheckBox)findViewById(R.id.contactsCheckBox);
getContactsList();
}
private void getContactsList() {
ArrayList<HashMap<String, Object>> contactsListItem = new ArrayList<HashMap<String, Object>>();
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext())
{
HashMap<String, Object> map = new HashMap<String, Object>();
String phoneName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
map.put("nameTxt", phoneName);
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.compareTo("1") == 0)
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{
String strphoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String phoneNumber="";
phoneNumber+=strphoneNumber+" ";
map.put("numberTxt", phoneNumber);
}
phones.close();
}
contactsListItem.add(map);
}
SimpleAdapter contactListAdapter = new SimpleAdapter(this, contactsListItem,R.layout.contactslistitem,new String[] {"nameTxt", "numberTxt"}, new int[] {R.id.nameTxt,R.id.numberTxt});
//ArrayAdapter<HashMap<String, Object>> contactListAdapter = new ArrayAdapter<HashMap<String, Object>>(this, android.R.layout.simple_list_item_multiple_choice, contactsListItem); contactList.setAdapter(contactListAdapter);
cursor.close();
contactList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?>parent, View view, int position, long id)
{
//如何取值?
}
});
}}
public void onItemClick(AdapterView<?>parent, View view, int position, long id)
{
HashMap<String, Object> phonernumber= contactsListItem.get(position);
}
然后再onItemClick中, 用哪个position这个position就是第几个item被调用.var_arr[position]就是你要的值. ok?