我想在一个联系人姓名下显示多个联系号码。每个号码都应该有复选框。我已经完成了这个代码,它向我显示了一个带有一个数字的联系人姓名。如果该联系人姓名有另一个联系号码,则显示另一个同名的号码。
DisplayContacts.java
//import statements
public class DisplayContacts extends Activity implements OnItemClickListener{
List<String> name = new ArrayList<String>();
List<String> phone = new ArrayList<String>();
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
getAllContacts(this.getContentResolver());
ListView list = (ListView)findViewById(R.id.contactListView);
adapter = new MyAdapter();
list.setAdapter(adapter);
list.setOnItemClickListener(this);
list.setItemsCanFocus(false);
list.setTextFilterEnabled(true);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
adapter.toggle(position);
}
public void getAllContacts(ContentResolver cr) {
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null,null);
while (phones.moveToNext())
{
String name1=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
name.add(name1);
phone.add(phoneNumber);
}
phones.close();
}
class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener
{ private SparseBooleanArray mCheckStates;
LayoutInflater mInflater;
TextView textView1,textView2;
CheckBox cb;
MyAdapter()
{
mCheckStates = new SparseBooleanArray(name.size());
mInflater = (LayoutInflater)DisplayContacts.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return name.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = mInflater.inflate(R.layout.contact_list_item, null);
textView2= (TextView) vi.findViewById(R.id.textView1);
textView1= (TextView) vi.findViewById(R.id.textView2);
cb = (CheckBox) vi.findViewById(R.id.checkBox1);
textView2.setText(""+ name.get(position));
textView1.setText(""+ phone.get(position));
cb.setTag(position);
cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener(this);
return vi;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
}
}
activity_contacts.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ashu.mycontactapp.Contacts" >
<ListView
android:id="@+id/contactListView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
contact_list_item.xml
\\for each row in list
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginRight="26dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_alignLeft="@+id/textView1"
android:layout_marginLeft="21dp"
android:layout_toLeftOf="@+id/checkBox1"
android:text="TextView" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/checkBox1"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
我的输出屏幕截图
请参阅上图中的avinash联系人姓名。它显示两次名称。我希望这两个号码都在一个联系人名下。