Shoogarh
Shoogarh

Reputation: 9

get string value from item clicked in a listView

Hi i am new to android programming, i have make an Http post request to get json data from an external sql database and displayed my result in a lisView. i want to be able to retrieve the string value from a clicked item in the listView. Please any help with this will be much appreciated

Upvotes: 1

Views: 4693

Answers (3)

Mr. Concolato
Mr. Concolato

Reputation: 2230

I would try something like this, which has worked for me in the past:

String  itemValue    = (String) listView.getItemAtPosition(position);

This is from within the listView.setOnItemClickListener(new OnItemClickListener() portion of your code.

If your strings are aggregated into one string, then try this:

//Let itemValue = "item1 item2 item3" for example:
String[] parts = itemValue.split(" ");
String part1 = parts[0]; // item1
String part2 = parts[1]; // item2

Upvotes: 2

balaji koduri
balaji koduri

Reputation: 1321

try this:

listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String itemString=listView.getSelectedItem().toString();
            }

        });

Upvotes: 0

Ganesh Katikar
Ganesh Katikar

Reputation: 2690

Set OnItemClickListener on listview. See on below.

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                String itemString=listView.getSelectedItem().toString();
            }
        });

Enjoy!!!...

Upvotes: 1

Related Questions