ListView Checkbox取值问题,贴代码,求前辈解决

lcmgood 2011-03-29 11:35:40
贴一下代码:
望各位前辈指点啊,代码实现了获取手机联系人的功能,但现在面临的问题是如何防止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)
{

//如何取值?
}



});

}

}
...全文
228 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rikoraul 2011-04-24
  • 打赏
  • 举报
回复
时不时的扔给大家仨瓜俩枣的,中国劳苦大众就好这一口。
TaiJi1985 2011-03-29
  • 打赏
  • 举报
回复
你在getContactsList中保存一个值得数组,假设为 val_arr (定义为成员变量)
然后再onItemClick中, 用哪个position

这个position就是第几个item被调用.

var_arr[position]就是你要的值. ok?
foley_liao 2011-03-29
  • 打赏
  • 举报
回复
试试

@Override
public void onItemClick(AdapterView<?>parent, View view, int position, long id)
{

HashMap<String, Object> phonernumber= contactsListItem.get(position);
}
yongyuandeni 2011-03-29
  • 打赏
  • 举报
回复
HashMap<String, Object> phonernumber= contactsListItem.get(position);

80,475

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧