试图将所有联系人都放入列表视图?

时间:2017-10-29 15:48:17

标签: java android android-layout android-contacts

如果我不止一次使用常见的数据类型,它无法识别它,我遇到了问题。当输入名为from。

的字符串时
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;

import java.util.List;

public class ContactSelect extends ListActivity {
// google and find out what this stuff means later
@Override
public long getSelectedItemId() {
    return super.getSelectedItemId();
}

@Override
public int getSelectedItemPosition() {
    return super.getSelectedItemPosition();
}
// creating the list view Object
ListView Contacts;
Cursor cursor1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_select);
          cursor1 =  getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, null, null, null);
          startManagingCursor(cursor1);

          String[] from = (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone._ID);

        }

}

1 个答案:

答案 0 :(得分:4)

public void getContacts() {
    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()) {
                    String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    contactList.add(new Contact(name, number));
                }
                pCur.close();
            }
        }
    }
    cur.close();