D
D
dllweb2014-07-30 16:00:15
Android
dllweb, 2014-07-30 16:00:15

What did the user select from the Android ListView?

Friends, how to determine what the user has selected from the list in the ListView. I understand that you can pull the position or identifier from there, but my sheet consists of a ListView and two TextViews in it. That is, its appearance looks something like
TITLE
description
TITLE
description
TITLE
description
The list also stores the record id
(I will make a reservation, the list is formed based on the parsed json package obtained from the Web. That is, the list is dynamic). So, the question is, how can I get this, for example, description in the listener
public void onItemClick(AdapterView<?> parent, View v , int position, long id)
Tried in different ways, the application crashes. I am not an android development guru. I ask for advice.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Copperfield, 2014-07-30
@Copperfield

You can use the ViewHolder pattern .
Do this in the adapter:

ViewHolder holder = new ViewHolder();
holder.description= (TextView) convertView.findViewById(R.id.description);
convertView.setTag(holder);

Then, in the listener, you extract the viewHolder from the View and get the description from the TextView:
ViewHolder viewHolder = (ViewHolder)convertView.getTag();
...
viewHolder.description.getText();
...

The option, of course, is not the best. If you show your adapter, you can come up with a better one.

K
Kirill Zakharov, 2014-07-30
@yTko

if memory serves, then choose adapter from parent via getAdapter() and getItem(position) for it,
and in general, the view should just be the clicked view

D
dllweb, 2014-08-01
@dllweb

The question was solved so, none of the answers here, alas, did not fit.

public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {

Toast.makeText(getApplicationContext(), ((TextView) v.findViewById(R.id.title)).getText().toString() +" Его ID"+ ((TextView) v.findViewById(R.id.desc)).getText().toString(),
Toast.LENGTH_SHORT).show();

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question