如何在android中使用onListItemClick打开ListView中的联系人详细信息卡



我有联系应用程序,该应用显示了与ArrayAdapter一起显示联系人的所有名称。这可以正常工作,但是我想在我单击名称时打开本机联系应用程序,并使用联系人的详细信息。

预先感谢您!

public class MainActivity extends ListActivity {

private CustomAdapter customAdapter;
List<String> contacts = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView lv = (ListView) findViewById(android.R.id.list);
    customAdapter = new CustomAdapter(this);
    lv.setAdapter(customAdapter);
}
@Override
protected void onResume() {
    super.onResume();
    Log.d("TAG","onresume");
    loadApps();
}


public void loadApps(){
    //Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    Cursor cursor;
    ContentResolver contentResolver = getContentResolver();
    cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,
            ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1",
            null,
            "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");


    customAdapter.clear();
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
        do {
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            contacts.add(name);
        } while (cursor.moveToNext());
    }
    cursor.close();
    for (String names : contacts) {
        AppInfoItem appInfoItem = new AppInfoItem();
        appInfoItem.name = names;
        customAdapter.add(appInfoItem);
    }
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent intent = new Intent(Intent.ACTION_VIEW,
            Uri.parse("content://contacts/people/"));
    if (intent != null) {
        startActivity(intent);
    }
}
}

customArrayadapter.class

public class CustomAdapter extends ArrayAdapter<String> {
LayoutInflater layoutInflater;

public CustomAdapter(Context context) {
    super(context, 0);
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // Create cell and populate with data of the array
    if (convertView == null)
        convertView = layoutInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
    AppInfoItem item = (AppInfoItem) getItem(position);
    //String name = names[position];
    TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
    textView.setText(item.name);


    return convertView;
}

}

appinfoitem.class

public class AppInfoItem {
String name;
String id;

}

我认为您需要将Intent与Action_View Action一起使用,并将意图数据集与表单的联系人的URI

content://contacts/people/1

如第一个示例所示,此处http://developer.android.com/reference/android/content/content/intent.html#intentsstructure

最新更新