[英]Contact list shows empty
I want to show the custom list of contacts from the phone contacts. 我想显示电话联系人中的联系人自定义列表。 I want to get the list of contacts and I have to select multiple contacts from the list. 我想获取联系人列表,并且必须从列表中选择多个联系人。
For this I have created a custom view. 为此,我创建了一个自定义视图。 An adapter for the custom view. 自定义视图的适配器。
Also I have a function to get the contacts from the phone contact. 另外,我还具有从电话联系人中获取联系人的功能。 It's using a cursor to get the contacts. 它使用光标来获取联系人。 But the cursor shows null and list also shows empty. 但是光标显示为空,列表也显示为空。
As I debug the code it shows the cursor null and dose not move ahead. 当我调试代码时,它显示光标为null,并且不会向前移动。
I have added a red permission for contacts in manifest as well. 我还为清单中的联系人添加了红色权限。
But I am unable to get the list. 但是我无法得到名单。
ContactList activity: ContactList活动:
public class ContactList extends ListActivity {
/** Called when the activity is first created. */
private ArrayList<contact> contact_list = null;
private contactAdapter mContactAdapter = null;
private ArrayList<contact> items;
boolean[] isChecked;
Cursor mCursor;
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
contact_list = new ArrayList<contact>();
lv = getListView();
getContacts();
}
@SuppressWarnings("unused")
private void getContacts() {
try {
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts._ID };
mCursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
projection, ContactsContract.Contacts.HAS_PHONE_NUMBER + "=?", new String[] { "1" },
ContactsContract.Contacts.DISPLAY_NAME);
while (mCursor.moveToNext()) {
contact contact = new contact();
String contactId = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));
contact.setContactName(mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
contact_list.add(contact);
}
isChecked = new boolean[mCursor.getCount()];
for (int i = 0; i < isChecked.length; i++) {
isChecked[i] = false;
}
this.mContactAdapter = new contactAdapter(this, R.layout.listview, contact_list);
lv.setAdapter(this.mContactAdapter);
mCursor.close();
} catch (Exception e) {
Log.d("getContacts", e.getMessage());
}
}
public class contactAdapter extends ArrayAdapter<contact> {
public contactAdapter(Context context, int textViewResourceId, ArrayList<contact> items1) {
super(context, textViewResourceId, items1);
items = items1;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder;
mViewHolder = new ViewHolder();
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.listview, parent, false);
mViewHolder.cb = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(mViewHolder);
if (isChecked[position] == true)
mViewHolder.cb.setChecked(true);
else
mViewHolder.cb.setChecked(false);
mViewHolder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean ischecked) {
if (buttonView.isChecked())
isChecked[position] = true;
else
isChecked[position] = false;
}
});
contact contacts = items.get(position);
if (contacts != null) {
if (mViewHolder.cb != null) {
mViewHolder.cb.setText(contacts.getContactName());
}
}
return convertView;
}
}
public class ViewHolder {
CheckBox cb;
}
}
custom list view 自定义列表视图
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight">
<ImageView
android:id="@+id/contactimage"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginRight="8dp"
android:contentDescription="@string/app_name"
android:scaleType="centerInside" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toLeftOf="@+id/contactcheck"
android:layout_toRightOf="@+id/contactimage" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Contact Name"
android:textColor="#000"
android:textIsSelectable="false"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:singleLine="true"
android:text="09876543210"
android:textColor="#2689e0"
android:textIsSelectable="false"
android:textSize="14dp" />
</RelativeLayout>
<CheckBox
android:id="@+id/contactcheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginLeft="8dp" />
</RelativeLayout>
And a contact class: 和一个联系人类:
public class contact {
private String contactName;
public String getContactName() {
return contactName;
}
public void setContactName(String contactName) {
this.contactName = contactName;
}
public int getContactid() {
return contactid;
}
public void setContactid(int contactid) {
this.contactid = contactid;
}
private int contactid;
}
What's going wrong? 怎么了
Can anyone help here please? 有人可以帮忙吗? Thank you.. 谢谢..
在读取表之前,首先将光标移到头部,在contactlist活动中的while语句编写之前添加以下行
mCursor.movetoFirst();//Add this line
try to this:- 尝试这样做:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
String phone = null;
String id = null;
String name = null;
if (cur.getCount() > 0)
{
while (cur.moveToNext())
{
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
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())
{
phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
pCur.close();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.