Android: Identify individual items in row of 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 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.

That's exactly what I was looking for. Thank you so much! What a great community.

– spryan Feb 24 at 23:32.

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

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions