1

My current problem is I have a ListView I'm using to display my contacts now I'd like to set the onItemClick to grab the name of the contact and set that to a TextView called p2 the code I currently have is:

protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setp2);

        cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);

        String[] from = new String[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME};
        int[] to = new int[]{R.id.id_text,R.id.name_text};

        final SimpleCursorAdapter adapter = new SimpleCursorAdapter(setp2.this,R.layout.setp2_layout,cursor,from,to);

        ListView list = (ListView)findViewById(R.id.contact_list);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {              
            }
        });


}

So basically I'm unsure of what I need to do in the onItemClick, any help appreciated!

Coombes
  • 203
  • 1
  • 10

3 Answers3

1

In your onItemClick, you will get the instance of the view associated with that list item.From that view you can get the text associated with it and set it to the p2 TextView. If my understanding of your requirement is correct, the code will be something like this:

 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) { 
               CharSequence name = ((TextView)view.findViewById(R.id.name_text)).getText();
               //Set this to your textview in P2
               tvP2.setText(name);

        }
    });
Karakuri
  • 38,365
  • 12
  • 84
  • 104
arjoan
  • 1,849
  • 2
  • 20
  • 39
0

Set up a SimpleCursorAdapter.CursorToStringConverter and set it on the adapter, then use convertToString(Cursor cursor) to get the text in on item click :

(Cursor) simple.getItem(position)
Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
0
@Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) { 
            cursor.moveToPosition(position);
            String name = cursor.getString(cursor.getColumnIndex(
                            ContactsContract.Contacts.DISPLAY_NAME));
             p2.setText(name);
        }
    });
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54