如何使用Android中的onListItemClick在联系人名称列表视图中打开联系人详细信息卡?

时间:2015-05-12 11:09:57

标签: java android listview android-arrayadapter listactivity

我有一个联系人应用程序,它使用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;


}

1 个答案:

答案 0 :(得分:0)

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

content://contacts/people/1

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