机器人:在ListView中的行识别单个项目?机器人、项目、ListView

2023-09-06 04:36:59 作者:倾她城做她王脱她衣上她床

我在填充的数据库表中的ListActivity一个ListView。 ListView中的每一行是与名为rowid的,日期和按顺序说出三种TextViews一个RelativeLayout的。我可以选择单个行编程方式使用ListView控件的.setSelection(INT位置)的方法。

I have a ListView in a ListActivity populated by a database table. Each row of the ListView is a RelativeLayout with three TextViews named rowid, date, and name in that order. I am able to select individual rows programmatically using the .setSelection(int position) method of the ListView.

下面是我想要做的事:当我推界面上的按钮,您可以通过当前选择列表行的ROWID,并使用ROWID进行数据库查询。我无法弄清楚如何从列表本身的ROWID。因为它是在数据库中的rowid rowid的可能不一样的列表上的ID或位置。

Here is what I'm trying to do: When I push a button on the interface, get the rowid from the currently selected list row, and perform a db query using the rowid. I can't figure out how to get the rowid from the list itself. rowid may not be the same as the ID or position on the list as it is the rowid in the database.

我怀疑这将需要一个适配器的工作,但我一直在试图/在网上搜索了一个星期,一直没能想出解决办法。感谢您的帮助,任何人都可以提供。

I suspect this will require working with an adapter, but I've been trying/searching the web for a week and haven't been able to figure this out. Thanks for the help anyone can provide.

推荐答案

您知道当前所选项目的列表中的位置,你必须在ListView之外的按钮,应该触发该项目的一些行动,而你不只是使ListView的行(或每行中的某些子视图)点击。对吧?

You know the list position of the currently selected item, you have a button outside the ListView that should trigger some action on that item, and you're not just making the ListView rows (or some child view within each row) clickable. Right?

您可以从列表的适配器信息。的getItem(INT位置)返回重新通过在位置列表项psented $ P $的对象,这样你就可以获取你需要直接,如果它存储在对象中的信息。 getView(INT位置)返回视图行,让您在使用findViewById(INT ID)来检索您的TextView。

You can get information from the list's adapter. getItem(int position) returns the object that is represented by the list item at position, so you can retrieve the information you need directly if it's stored in the object. getView(int position) returns the view for the row, allowing you to use findViewById(int id) to retrieve your TextView.

如果您还没有该适配器,您可以使用getAdapter从ListView中得到它()。

If you don't already have the adapter, you can get it from the ListView using getAdapter().

// 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行,而不是选择该行,然后pressing另一个按钮。雷诺的答案可能更好地工作。

You might also consider whether it would be more natural for the user to just tap the ListView row, rather than selecting the row and then pressing another button. Reno's answer might work better.