N
N
newdancer2016-03-27 18:41:55
Java
newdancer, 2016-03-27 18:41:55

How to access an imageview that is in a listview?

How to access an imageview that is in a listview?
I have 2 imageviews in my listview for each item. By clicking on the picture, I draw a rating on it. Do I need the imageview itself for this? i get Via tag
Get like this:

ImageView imgl= (ImageView) view.findViewById(R.id.imageLeft).findViewWithTag(pos);

since the view does not know the values ​​of the tagprevious item except the last one

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Meehalkoff, 2016-03-27
@Meehalkoff

public static View getViewByPosition(int pos, ListView listView) {
        final int firstListItemPosition = listView.getFirstVisiblePosition();
        final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

        if (pos < firstListItemPosition || pos > lastListItemPosition ) {
            return listView.getAdapter().getView(pos, null, listView);
        } else {
            final int childIndex = pos - firstListItemPosition;
            return listView.getChildAt(childIndex);
        }

D
Denis Zagaevsky, 2016-03-28
@zagayevskiy

For example, when in the getView() method:

View getView(...) {
    final ImageView image = ...;
    otherImage.setOnClickListener(new View.OnClickListener(){
            public void onClick(...){
                 //используем ссылку на image
            }
    }
    ...
}

Similarly, you can make the click listener a non-anonymous class by explicitly passing a link to the view into it.
If the click occurs on the same view with which you want to work, then everything is even simpler:
image.setOnClickListener(new View.OnClickListener(){
    public void onClick (View v){
        ImageView image = (ImageView) v;
        // работаем с этим же image
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question