您知道当前选定项目的列表位置,您在ListView外部有一个按钮,该按钮应触发该项目的某个操作,而且您不仅仅制作ListView行(或每行内的某个子视图)点击。对?
您可以从列表的适配器中获取信息。 getItem(int position)返回位置处列表项表示的对象,因此如果存储在对象中,则可以直接检索所需的信息。 getView(int position)返回行的视图,允许您使用findViewById(int id)来检索您的TextView。
如果您还没有适配器,可以使用getAdapter()从ListView中获取它。
// ListView myListView = the ListView in question
// int selectedRow = the currently selected row in the ListView
// Each row in the ListView is backed by an object of type MyCustomDataClass
int dbRowId;
Adapter adapter = myListView.getAdapter();
MyCustomDataClass data = (MyCustomDataClass) adapter.getItem(selectedRow);
dbRowId = data.getDatabaseRowId();
// OR
dbRowId = data.rowId;
// OR whatever method the object has for getting the ID.
// OR
View listViewRow = adapter.getView(selectedRow);
TextView dbRowView = (TextView) listViewRow.findViewById(R.id.rowid);
String dbRowAsString = dbRowView.getText().toString();
dbRowId = Integer.parseInt(dbRowAsString);
您可能还会考虑用户点击ListView行,而不是选择行然后再按下另一个按钮是否更自然。里诺的答案可能会更好。
这正是我一直在寻找。非常感谢!多么伟大的社区。 – spryan 2011-02-24 23:32:27