Answer the question
In order to leave comments, you need to log in
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
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);
ViewHolder viewHolder = (ViewHolder)convertView.getTag();
...
viewHolder.description.getText();
...
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
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 questionAsk a Question
731 491 924 answers to any question