I understand this question has been asked several times. You would think at least one of the solutions would have helped me but no such luck.
I have the following code that populates a list view correctly (also found here):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
// Create and populate a List of planet names.
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(planets) );
// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
}
However, unsuccessful at getting the text of the item selected.
For example, the user selects “Mars”, what function returns the text “Mars” in the function:
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
// Retrieve selected text here?
}
1 Answer