android:识别ListView行中的单个项目?

发布于 2024-10-19 02:53:29 字数 365 浏览 8 评论 0原文

我在 ListActivity 中有一个由数据库表填充的 ListView。 ListView 的每一行都是一个relativelayout,其中包含三个依次命名为 rowid、date 和 name 的 TextView。我可以使用 ListView 的 .setSelection(intposition) 方法以编程方式选择单独的行。

这就是我想要做的:当我按下界面上的按钮时,从当前选定的列表行中获取 rowid,并使用 rowid 执行数据库查询。我不知道如何从列表本身获取 rowid 。 rowid 可能与列表中的 ID 或位置不同,因为它是数据库中的 rowid。

我怀疑这需要使用适配器,但我已经尝试/搜索网络一周了,但无法弄清楚这一点。感谢任何人都可以提供的帮助。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零崎曲识 2024-10-26 02:53:29

您知道当前所选项目的列表位置,ListView 外部有一个按钮,该按钮应该触发该项目的某些操作,并且您不仅仅是使 ListView 行(或每行中的某些子视图)可单击。正确的?

您可以从列表的适配器获取信息。 getItem(intposition) 返回由位置处的列表项表示的对象,因此如果该信息存储在该对象中,您可以直接检索所需的信息。 getView(intposition) 返回行的视图,允许您使用 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 行,而不是选择该行然后按另一个按钮。雷诺的答案可能会更好。

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?

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.

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);

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.

悟红尘 2024-10-26 02:53:29

我最终使用了最后一种方法和以下代码。

int dbRowId;
Adapter adapter = myListView.getAdapter();

View listViewRow = adapter.getView(selectedRow);
TextView dbRowView = (TextView) listViewRow.findViewById(R.id.rowid, null, null);
String dbRowAsString = dbRowView.getText().toString();
dbRowId = Integer.parseInt(dbRowAsString);

我必须做的唯一更改是向 .findViewByID 中的参数添加 null, null

TextView dbRowView = (TextView) listViewRow.findViewById(R.id.rowid, null, null);

I ended up using the last method with the following code.

int dbRowId;
Adapter adapter = myListView.getAdapter();

View listViewRow = adapter.getView(selectedRow);
TextView dbRowView = (TextView) listViewRow.findViewById(R.id.rowid, null, null);
String dbRowAsString = dbRowView.getText().toString();
dbRowId = Integer.parseInt(dbRowAsString);

The only change I had to make was adding null, null to the parameters in .findViewByID

TextView dbRowView = (TextView) listViewRow.findViewById(R.id.rowid, null, null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文