我想在android中显示带有多选复选框的联系人列表。我可以实现这一点。 可以任何人建议任何示例或代码,然后它会有所帮助。
答案 0 :(得分:0)
答案 1 :(得分:0)
最后我得到了答案......
public class ShowContactActivity extends Activity {
ArrayList<String> listname;
ArrayList<String> list_no;
Context context;
LayoutInflater inflater;
ListView lView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.show);
lView = (ListView)findViewById(R.id.listView1);
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); // Here I collect the name of contact person and phone number.
list_no.add(phoneNo);
}
pCur.close();
}
}
}
lView.setAdapter(new Contact(this));
}
class Contact extends BaseAdapter
{
Context myContext;
public Contact(ShowContactActivity 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.checkBox = (CheckBox) convertView
.findViewById(R.id.checkBox);
viewHolder.id = (TextView)convertView.findViewById(R.id.id);
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
// Here We can do our rest of stuff
Toast.makeText(myContext, "Selected item is :-" +buttonView.getId(), Toast.LENGTH_LONG).show();
}
});
}
return convertView;
}
}
class ViewHolder
{
TextView text_name,id;
CheckBox checkBox;
//EditText search;
} }