我知道这个问题已经被问过好几次了。你会认为至少有一个解决方案会帮助我,但没有这样的运气。
我有以下代码可以正确填充列表视图(也可以在此处找到):
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 );
}
但是,无法成功获取所选项目的文本。
比如用户选择“Mars”,什么函数在函数中返回文本“Mars”:
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
// Retrieve selected text here?
}