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
Use this:
String selectedFromList = (lv.getItemAtPosition(position));
Hope it will help. :)
This worked for me. I had tried some solutions here but they hadn't worked. Hope this helps.
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myFamily);
listViewDemo.setAdapter(arrayAdapter);
listViewDemo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String nameSelectedfromList = arrayAdapter.getItem(position).toString();
String nameSelectedfromList = listViewDemo.getItemAtPosition(position).toString();
Log.i("Clicked Item", nameSelectedfromList);
}
});
If you are using a List to populate the ListView in your onItemClick() method.
String s = list.get(position);
You need to be able to access this list within the inner class
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av,
View view, int position, long l) {
String text = String.valueOf(myListView.getItemAtPosition(position));
}
}
});
It is as simple as that
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView text1 = (TextView) findViewById(R.id.Text1);
TextView text2 = (TextView) findViewById(R.id.Text2);
String txt_1 = ""+text1.getText().toString().trim();
String txt_2 = ""+text2.getText().toString().trim();
//Other_Related_Work
}
});
try doing it by this, insert it into onItemClickListener, i am not sure what does your Country class look like:
String s = values.get(position).getCountryName();
Log.e("LISTVIEW", "selected item text = "+s);
or
String s = values.get(position).toString();
Log.e("LISTVIEW", "selected item text = "+s);