java - 如何在 Android 中使用 onListItemClick 在 listView 中打开带有联系人姓名的联系人详细信息卡?

标签 java android listview android-arrayadapter listactivity

我有一个联系人应用程序,它使用 ArrayAdapter 显示联系人的所有姓名。这工作正常,但我想每当我单击姓名时打开 native 联系人应用程序,其中包含联系人的详细信息。

提前谢谢您!

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;


}

最佳答案

我认为您需要将 IntentACTION_VIEW action 一起使用并将 Intent 数据设置为表单的联系人 URI

content://contacts/people/1

如第一个示例所示http://developer.android.com/reference/android/content/Intent.html#IntentStructure

关于java - 如何在 Android 中使用 onListItemClick 在 listView 中打开带有联系人姓名的联系人详细信息卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30189098/

相关文章:

java - String.valueOf() 和 new String() 的区别

java - ANDing,嵌套查询解析服务器android

android - 我希望 ListView 项目在新 Activity 中显示相关项目的数据

java - 用于针对用户输入进行快速查找服务的 REST 或 SOAP?

java - 如何使用 java 选择并单击 selenium Web 驱动程序中的单选按钮

java - 如何解析多线程写入的日志?

java - 我如何在 Android 中绑定(bind)此服务?

java - 线程中断替代方案?

android - 使用 CursorAdapter (CHOICE_MODE_MULTIPLE) 在 ListView 上设置选中状态

java - 来自 Parse 数据的带有 ListView 的 fragment 中的 RatingBar