I implemented ArrayAdapter for ListView, where each row contains TextView and ImageButton.
The background image of ImageButton depends on some condition, two options are available:
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item_with_button, null);
File f = new File(PATH_TO_FILE);
if(f.exists()){
imbutton =(ImageButton)vi.findViewById(R.id.btn_image);
imbutton.setBackgroundResource(R.drawable.done);
}else
{
imbutton =(ImageButton)vi.findViewById(R.id.btn_image);
imbutton.setBackgroundResource(R.drawable.download);
imbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String url = URL;
new DownloadFileAsync(pos).execute(url);
}
});
}
...
return vi;
}
If file exists, then I set “done” image on ImageButton. If file doesn’t exist, I set “download” image.
After user clicks on ImageButton, the file downloads and I need change respective image.
The problem is that getView is called only if user scrolls ListView down until this ImageButton dissapear from the screen and then scroll back. After that button appears with new background image.
However, I can’t invalidate button to display new image at the moment.
I used this class for async downloading: http://www.java-samples.com/showtutorial.php?tutorialid=1521
I tried make something like currentbutton.invalidate() in function
@Override
protected void onPostExecute(String unused) {
activity.dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
but without success.
How can I invalidate ImageButton to display its new background image?
You could call notifyDataSetChanged in the onClick listener to force a refresh on the list.
I haven’t tried it though, so I’m not sure if it’ll work without making any explicit changes on the adapter(i.e. adding, removing or updating items on it).
I’ll check it later and write any further suggestions if it doesn’t work out of the box.
EDIT: Actually you need to call notifyDataSetChanged in the onPostExecute method of the AsyncTask you are using, not in the onClick listener.