添加侦听器到ListView的联系人?

时间:2013-02-23 08:59:18

标签: android

我需要一些帮助才能将onItemClickListener添加到联系人的ListView中。我想从选定的联系人中捕获一些信息,如姓名,电话号码和其他信息,并将其传递给另一个活动。

我不确定如何在下面的代码中添加onListItemListener以及如何从所选联系人中捕获信息?我也想知道如何使用Intent传递多个值?我用它来表示一个值:

Intent i = new Intent(Activity_1.this, Activity_2.class);
startActivityForResult(i, 1);

Intent intent = new Intent();
intent.putExtra("imageId", imagePath);
setResult(RESULT_OK, intent);
finish();

这是我在ListView活动中的代码。昨天我在这里得到了一些帮助,我想知道代码的while(cursor.moveToNext()){}部分是否对ListView很重要,因为没有它可以工作吗?

提供一些帮助!谢谢!

public class Activity_3 extends Activity {

ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_3);
    listView=(ListView)findViewById(R.id.contactList);

    String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
                           ContactsContract.CommonDataKinds.Phone.NUMBER, 
                           ContactsContract.CommonDataKinds.Phone._ID};

    // Get a cursor with all people
    Cursor cursor = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,null,null, null);
    //Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,null,null, null);


    String[] fromColumn = {ContactsContract.Contacts.DISPLAY_NAME};
    int[] toView = {R.id.contactItem };

    while (cursor.moveToNext())
    {
         String Name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
         String Number=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

    }
    startManagingCursor(cursor);

    ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.activity_3, cursor, fromColumn, toView );

    listView.setAdapter(adapter);

}    
}

1 个答案:

答案 0 :(得分:0)

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapter, View view , int position,
                    long id) {
                // TODO Auto-generated method stub
                        do your operations here...

            }
        });


use this onitemclicklistener to listen to your listview clicks .