SDK Version:
M3
So this tutorial shows you how to refresh imageviews’ contents periodically (let say by Handlers if you download the picture from web).
What we’re lookign for here is instead of create new Adapters and HashMaps (which contains ListView data), we just update it’s values, and Android will do the trick for us.
The most important thing is DO NOT AT ANY CIRCUMSTANCES CREATE A NEW ADAPTER (or a new data source that holds the Adapter’s data).
There’s an exception of course, you obviously have to create a new Adapter in OnCreate() { }.
Here is how it works:
1. Step
Create a new ArrayList of HashMaps that holds the data
2. Step
Create a new Adapter in your OnCreate() {}
R.layout.detailedview is my own Layout file, that includes 3 textviews called "R.id.EntityName, R.id.Category, R.id.Price, " and an ImageView called "R.id.ThumbImage".
As you can see, we tell the Adapter to use these UI elements.
3. Step
The refreshing
Since we have data in the List of HashMaps, we can overwrite the values, simply by calling
That’s all you have to do, enjoy!
M3
So this tutorial shows you how to refresh imageviews’ contents periodically (let say by Handlers if you download the picture from web).
What we’re lookign for here is instead of create new Adapters and HashMaps (which contains ListView data), we just update it’s values, and Android will do the trick for us.
The most important thing is DO NOT AT ANY CIRCUMSTANCES CREATE A NEW ADAPTER (or a new data source that holds the Adapter’s data).
There’s an exception of course, you obviously have to create a new Adapter in OnCreate() { }.
Here is how it works:
1. Step
Create a new ArrayList of HashMaps that holds the data
- hashMapListForListView = new ArrayList<HashMap<String,String>>();
- we can fill it up by calling:
- entitiesHashMap.put("name", "Ball");
- entitiesHashMap.put("category", "Sport");
- entitiesHashMap.put("price", "2.99");
- entitiesHashMap.put("imageUri", ball.Uri);
- hashMapListForListView.add(entitiesHashMap);
2. Step
Create a new Adapter in your OnCreate() {}
- adapterForList = new SimpleAdapter(Main.this,
- hashMapListForListView, R.layout.detailedview,
- new String[] {"name", "category", "price", "imageUri"},
- new int[] { R.id.EntityName, R.id.Category, R.id.Price, R.id.ThumbImage });
- listView.setAdapter(adapterForList);
R.layout.detailedview is my own Layout file, that includes 3 textviews called "R.id.EntityName, R.id.Category, R.id.Price, " and an ImageView called "R.id.ThumbImage".
As you can see, we tell the Adapter to use these UI elements.
3. Step
The refreshing
Since we have data in the List of HashMaps, we can overwrite the values, simply by calling
- hashMapListForListView.set(index, entitiesHashMap);
That’s all you have to do, enjoy!