M
M
max_vr2016-06-09 11:57:43
Java
max_vr, 2016-06-09 11:57:43

How to access an image in a ListView?

There is a ListView, the layout is specified in the adapter with the following structure:
35eee1308728498cbd631a2c2f10fa7f.bmp
I'm trying to access the imageView of a specific row:

LinearLayout temp = (LinearLayout) list.getAdapter().getView(position, null, list);
 ImageView img = (ImageView) temp.getChildAt(0);
 img.setImageResource(R.drawable.noava);

It compiles without errors, does not crash during operation, but the picture in the treasured line still does not change. What am I doing wrong?
PS do not judge strictly, I'm new

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
itdroid, 2016-06-09
@itdroid

An approach like yours is wrong.
The content of the list is the responsibility of the adapter, and changes to that content from elsewhere will result in undefined rendering of the elements.
You need to define a method in the adapter:
In this method, you need to remember a new picture for the corresponding position and call the notifyDatasetChanged() method to update the contents of the list.
After that, the adapter will start updating, and you will be able to show the new picture that you previously saved.
It is clear from your code that you have not figured out how the ListView / RecyclerView works, read some tutorial on these components and then it will become clear to you what the problem was.

S
Sergey Semenko, 2016-06-09
@abler98

<ImageView android:id="@+id/imageView"
        .../>

View view = list.getAdapter().getView(position, null, list);
ImageView imageView = view.findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.noava);

Only this needs to be done in the adapter itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question