How to retrieve the clicked string from a listview using OnItemClick?
Asked Answered
S

5

15

I've got some problem here. It looks simple and i keep searching for its solution. Unfortunately, i cant find anything. This is my problem.... What i'm trying to do is to get the string showed in the listview from an On item click method.

This is my listview :

- lol
- hi
- waw

When i click "lol" i want to get the "lol" string.....

What should i put in my code here? :

lv = (ListView) findViewById(R.id.list_view);
lv.setOnItemClickListener(new OnItemClickListener()
{
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
 {
    // Intent newI = new Intent(this,PDetail.class); 
     Intent newI = new Intent (Create.this, PDetail.class);
     //String sd = ((() arg1).getText()).toString();
     //newI.putExtra("x", arg2);
     startActivity (newI);
    // db.getList(arg3);

 }});
Sulphuric answered 1/8, 2013 at 9:15 Comment(4)
You should post the code that actually populates the ListViewMaria
What type of adapters are you using? is it a custom adapterGehlenite
i already know how to do it..... @ user2012,njzk2,karan23 already give me the answer what i need. But, thank for your attention.Sulphuric
multiple ways to achieve ListView Itemtext https://mcmap.net/q/367222/-how-to-extract-the-text-from-the-selected-item-on-the-listviewKlimesh
G
30
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
     {
         String data=(String)arg0.getItemAtPosition(arg2);


     }});

data contains your clicked position's data. Do what ever you want to do with that.

Garth answered 1/8, 2013 at 9:19 Comment(2)
i'll have many input.... not just that three. i cant define them like that one by one..... i need something more flexibel and universalSulphuric
what's with the 3 items ?Nonconductor
N
7

arg0 is your AdapterView, typically a ListView. arg2 is the position in the ListView. You can get the items from your Adapter :

Object item = arg0.getItemAtPosition(arg2);

Depending on the type of your object in your adapter, a trivial solution is:

String value = item.toString();
Nonconductor answered 1/8, 2013 at 9:21 Comment(1)
Thanks, i appreciated your reply.Sulphuric
M
2

The arg1 parameter of your listener is the clicked item's view.

Assuming that the items of your ListView are TextViews you can try:

String itemText = ((TextView) arg1).getText();
Maria answered 1/8, 2013 at 9:17 Comment(0)
A
1

use String val = (String)arg0.getItemAtPosition(arg2)

Amuse answered 1/8, 2013 at 9:20 Comment(0)
R
0

For Kotlin use this:

mListView.setOnItemClickListener{ parent, view, position, id ->
        println("Item : " +  (view as TextView).text)
    }

This only works if you inflated your adapter with just a TextView otherwise instead of casting it to TextView you should get your TextView by doing something like

view.findViewById(R.id.your_textview_id)
Radley answered 29/11, 2021 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.