0

我需要一些帮助才能将 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);

}    
}
4

1 回答 1

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 .
于 2013-02-23T09:17:29.777 回答