1

My database data is stored in list view, when I click on one of the list view, how can I get each item according to parameter “position”?

lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
    }
});
Guillaume Barré
  • 4,168
  • 2
  • 27
  • 50
tts zhang
  • 17
  • 3
  • follow this url https://stackoverflow.com/questions/14891026/get-clicked-item-from-listview – androholic Dec 01 '17 at 08:23
  • 1
    if you want to get your adapter's item then see `Adapter#getItem()` method – pskink Dec 01 '17 at 08:28
  • Possible duplicate of [Android ListView and OnClickListener: How to get the selected item](https://stackoverflow.com/questions/9427797/android-listview-and-onclicklistener-how-to-get-the-selected-item) – Manoj Perumarath Dec 01 '17 at 08:58

3 Answers3

0

You can do it by-:

messagesContainer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position){
           case 0:
            break;
          case 1:
            break;

        }
    }
});

But this will be complex when we have to many items in listview. Thanks

Shivam Oberoi
  • 1,447
  • 1
  • 9
  • 18
  • Because the data in listview from the database ,the value of the position is changing.Then how can we use "switch" better?Thanks! – tts zhang Dec 01 '17 at 11:00
0

Try this...

this sample will help you...

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
      // When clicked, show a toast with the TextView text
      Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
          Toast.LENGTH_SHORT).show();
    }
  });

for more

Arnold Brown
  • 1,330
  • 13
  • 28
0

If you set your database data in listview like below

ArrayAdapter<Object> mDatabaseData = new ArrayAdapter<Object>(this,android.R.layout.simple_expandable_list_item_2, array);
lv.setAdapter(mDatabaseData);

then you can get each item according to parameter “position” as below

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
  // When clicked, get data for selected position
   Object selectedItem=mDatabaseData.get(position);
   }
});
Zealous System
  • 2,080
  • 11
  • 22
  • Yes,I add data to the database using the method you say.Yeah, thanks for help,a whole record can be displayed according to "String str = (lv.getItemAtPosition(position).toString());"But,I can't separate the value I want successfully.For my part, because the record from the database,so I may get the value of a database specific property column based on id.I don't have a very effective way to do it right now.Thank you! – tts zhang Dec 01 '17 at 15:13
  • Sorry,I misread your answer.I use " ArrayAdapter mDatabaseData = new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_2, array); lv.setAdapter(mDatabaseData);"to display data in listview. – tts zhang Dec 01 '17 at 15:28