如何使用复选框创建自定义联系人列表以从 android 列表中选择多个联系人
public class AddFromContacts extends Activity {
ArrayList<String> listname;
// ArrayList<String> list_no;
Context context;
LayoutInflater inflater;
ListView contactlistView;
String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.show);
contactlistView = (ListView) findViewById(R.id.ContactlistView);
listname = new ArrayList<String>();
// list_no = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
// Do something with phones
String phoneNo = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
listname.add(name + "\n" + phoneNo);
// list_no.add(phoneNo);
}
pCur.close();
}
}
}
contactlistView.setAdapter(new Contact(this));
}
class Contact extends BaseAdapter {
Context myContext;
public Contact(AddFromContacts contactActivity) {
// TODO Auto-generated constructor stub
this.myContext = contactActivity;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listname.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.checkbox, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text_name = (TextView) convertView
.findViewById(R.id.name);
viewHolder.id = (TextView) convertView.findViewById(R.id.id);
viewHolder.checkBox = (CheckBox) convertView
.findViewById(R.id.checkBox);
convertView.setTag(viewHolder);
}
final ViewHolder holder = (ViewHolder) convertView.getTag();
// holder.text_name.setText(list_no.get(position));
holder.id.setText(listname.get(position));
if (holder != null) {
holder.checkBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(
CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
// Toast.makeText(myContext, name + " Selected",
// Toast.LENGTH_LONG).show();
}
});
}
return convertView;
}
}
class ViewHolder {
TextView text_name, id;
CheckBox checkBox;
// EditText search;
}
}
但是我在从列表中选择联系人时遇到了这个问题,它从列表中选择了我没有选择的那些联系人