MiloŇ° MiloŇ° - 5 months ago 12
Android Question

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);

}});

Answer

Use this:

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

Hope it will help. :)

Comments