Android: Define individual items in a ListView string?

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 can select individual rows programmatically using the .setSelection (int position) ListView method.

Here is what I am trying to do: when I click a button on an interface, I get rowid from the currently selected list row and execute a db query using rowid. I cannot figure out how to get rowid from the list itself. The rowid may not match the identifier or position in the list, as it is the rowid in the database.

I suspect that this will require working with the adapter, but I tried / searched the Internet for a week and was unable to figure it out. Thanks for the help anyone can provide.

+3
source share
2 answers

You know the position of the list of the currently selected item, you have a button outside the ListView that should initiate some actions on this item, and you do not just create ListView strings (or some child views in each row), right?

. getItem (int position) , , , . getView (int position) , findViewById (int id) TextView.

, ListView 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, , . .

+3

.

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

, , null, null .findViewByID

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

Source: https://habr.com/ru/post/1794317/


All Articles