Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract the text from the selected item on the listView

I have a listview with some items. I would like to get the text from the selected item.

Here is my list adapter and the onItemClickListener:

ListView lv = (ListView)findViewById(R.id.listView1);
    lv.setAdapter(new ArrayAdapter<Country>(
            this,R.layout.list_black_text,R.id.list_content, values));


    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
??????
    }});
        }

Could you tell me please how to get the String from the selected item.

the method ((TextView) view).getText() does not work, i have a

ClassCastException: android.widget.LinearLayout

I have found the solution, maybe somebody will need it:

ListView lv = (ListView)findViewById(R.id.listView1);
    lv.setAdapter(new ArrayAdapter<Country>(
            this,R.layout.list_black_text,R.id.list_content, values));

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
            TextView textView = (TextView) view.findViewById(R.id.list_content);
            String text = textView.getText().toString(); 
            System.out.println("Choosen Country = : " + text);

    }});
like image 322
Milos Cuculovic Avatar asked Feb 09 '12 10:02

Milos Cuculovic


People also ask

Which method is used to obtain the selected option from a ListView?

To get which item was selected, there is a method of the ListView called getItemAtPosition.

What is the use of ListView explain list view with example?

Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.

What does ListView uses to place each item?

ListView uses Adapter classes which add the content from data source (such as string array, array, database etc) to ListView.

What is a ListView?

A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.


3 Answers

Use this:

String selectedFromList = (String) (lv.getItemAtPosition(position));

Whatever the datatype you are having in your list, cast accordingly.

Hope it will help. :)

like image 162
Android Killer Avatar answered Sep 27 '22 08:09

Android Killer


For this you need to write the following:

lv.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        TextView textView = (TextView) view.findViewById(R.id.list_content);
        String text = lv.get(position).toString().trim();
        System.out.println("Chosen Country = : " + text);

}});
like image 27
Rishi Avatar answered Sep 24 '22 08:09

Rishi


The other answers look good, but I thought I'd wrap everything up into one complete answer.

There are multiple ways to achieve this and it also depends on whether you are getting text from simple listView or from Custom ListView(with custom_list_item.xml).

For Simple ListView

lv.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        String text = lv.get(position).tostring().trim();//first method 
        final String text = ((TextView)view).getText();// second method
}});

For Custom ListView

lv.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        TextView textView = (TextView) view.findViewById(R.id.list_content);
//where list_content is the id of TextView in listview_item.xml

}});

Problem with others Answers

@Android Killer string Casting is missing.

@Rishi doesn't give detail about using R.id.list_content

like image 5
Zar E Ahmer Avatar answered Sep 27 '22 08:09

Zar E Ahmer