Android 如何使用复选框从listview中提取详细信息?
我想保留使用Android 如何使用复选框从listview中提取详细信息?,android,listview,checkbox,contacts,Android,Listview,Checkbox,Contacts,我想保留使用复选框一次选择多个联系人号码的功能。我使用了一个带有两个文本视图和一个复选框的自定义布局来为列表创建一行。我已成功地将联系人填入列表,但无法配置在用户单击“完成”按钮时如何检索已选中(勾选)联系人的电话号码。请引导我 custcontactview.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
复选框一次选择多个联系人号码的功能。我使用了一个带有两个文本视图
和一个复选框
的自定义布局来为列表创建一行。我已成功地将联系人填入列表,但无法配置在用户单击“完成”按钮时如何检索已选中(勾选)联系人的电话号码。请引导我
custcontactview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5.0px"
android:paddingLeft="5.0px"
android:paddingTop="5.0px" >
<TextView
android:id="@+id/txtContactName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15.0dip"
android:layout_toLeftOf="@+id/checkBox1"
android:layout_alignParentLeft="true"
android:text="Medium Text"
android:textAppearance="?android:textAppearanceMedium" />
<TextView
android:id="@+id/txtContactNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtContactName"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15.0dip"
android:layout_toLeftOf="@+id/checkBox1"
android:text="Small Text"
android:textAppearance="?android:textAppearanceSmall" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:clickable="true"
android:layout_below="@+id/btnShow" >
</ListView>
</RelativeLayout>
活动\u联系人\u picker.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5.0px"
android:paddingLeft="5.0px"
android:paddingTop="5.0px" >
<TextView
android:id="@+id/txtContactName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15.0dip"
android:layout_toLeftOf="@+id/checkBox1"
android:layout_alignParentLeft="true"
android:text="Medium Text"
android:textAppearance="?android:textAppearanceMedium" />
<TextView
android:id="@+id/txtContactNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtContactName"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15.0dip"
android:layout_toLeftOf="@+id/checkBox1"
android:text="Small Text"
android:textAppearance="?android:textAppearanceSmall" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:clickable="true"
android:layout_below="@+id/btnShow" >
</ListView>
</RelativeLayout>
ContacsPicker.java
public class ContactsPicker extends ListActivity implements OnItemClickListener {
protected Object mActionMode;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts_picker);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "Name", "Phone" };
int[] to = { R.id.txtContactName, R.id.txtContactNumber };
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.custcontactview, from, to);
setListAdapter(adapter);
}
private ArrayList<Map<String, String>> buildData() {
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.clear();
Cursor people = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null,
"UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
while (people.moveToNext()) {
String contactName = people.getString(people
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String contactId = people.getString(people
.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = people
.getString(people
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ((Integer.parseInt(hasPhone) > 0)) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId,
null,
"UPPER(" + ContactsContract.Contacts.DISPLAY_NAME
+ ") ASC");
while (phones.moveToNext()) {
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Map<String, String> NamePhoneType = new HashMap<String, String>();
NamePhoneType.put("Name", contactName);
NamePhoneType.put("Phone", phoneNumber);
list.add(NamePhoneType);
}
phones.close();
}
}
people.close();
return list;
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { }
}
公共类ContactsPicker扩展ListActivity实现了McClickListener{
保护对象模式;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u contacts\u picker);
ArrayList=buildData();
字符串[]from={“Name”,“Phone”};
int[]to={R.id.txtContactName,R.id.txtContactNumber};
SimpleAdapter=新SimpleAdapter(此,列表,
R.layout.view,from,to);
setListAdapter(适配器);
}
私有ArrayList buildData(){
ArrayList=新建ArrayList();
list.clear();
Cursor people=getContentResolver().query(
Contacts contract.Contacts.CONTENT\u URI,null,null,null,
“上部(“+Contacts.Contacts.DISPLAY_NAME+”)ASC”);
while(people.moveToNext()){
String contactName=people.getString(人
.getColumnIndex(Contacts contract.Contacts.DISPLAY_NAME));
String contactId=people.getString(人)
.getColumnIndex(Contacts contract.Contacts._ID));
字符串hasPhone=people
.getString(人)
.getColumnIndex(Contacts contract.Contacts.HAS_PHONE_NUMBER));
if((Integer.parseInt(hasPhone)>0)){
Cursor=getContentResolver().query(
ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,
无效的
Contacts contract.CommonDataTypes.Phone.CONTACT\u ID
+“=”+联系人ID,
无效的
上方(“+Contacts contract.Contacts.DISPLAY_NAME
+(“ASC”);
while(phones.moveToNext()){
字符串phoneNumber=电话
.getString(电话)
.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER));
Map NamePhoneType=newhashmap();
NamePhoneType.put(“Name”,contactName);
NamePhoneType.put(“Phone”,phoneNumber);
list.add(NamePhoneType);
}
电话。关闭();
}
}
人;
退货清单;
}
public void onItemClick(AdapterView arg0,视图arg1,int arg2,long arg3){}
}
您应该为每个复选框以及onCheckChanged侦听器或类似的内容设置一个标记。当状态更改为“已选中”时,只需获取与该复选框关联的标记(这样您就可以知道列表中哪些元素已选中)。然后,您必须重新查询以获取该列索引处的电话号码。你的电话号码已经存储在这里了
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
您需要重写getView()方法,以便在创建项时可以存储信息(例如在ArrayList中)checked@ShashankKadne你能填写我的密码吗。我必须在星期一提交我的项目。还有很多事情要做。