How to update custom listview images simply
Fri, 08/06/2010 - 02:53 | by bence
SDK Version:
M3 | So in this tutorial I'm going to show you how to refresh imageviews' contents periodically (let say by Handlers if you download the picture from web). I had painful 2 days figuring out what's a good solution here, I tried to give IDs to imageviews and that sort of sick things, but believe me, it wasn't worth it. The solution is so simply that I hardly can believe. 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() { }. |
![]() |
Let me show you 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);
and then, we add it to the List:
- hashMapListForListView.add(entitiesHashMap);
We can do this over n over, as many times as we want.
2. Step
Create a new Adapter in your OnCreate() {}
- adapterForList = new SimpleAdapter(Main.this,
- hashMapListForListView, R.layout.detailedview,
- new int[] { R.id.EntityName, R.id.Category, R.id.Price, R.id.ThumbImage });
- listView.setAdapter(adapterForList);
What we're doing here is that we create a new SimpleAdapter, and tell it to use hashMapForListView as data source, and also tell the listView to use adapterForList as Adapter.
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);
Don't forget to create a new instance of entitiesHashMap, and set it's values just as we did in 1. Step. to get the result you'd expect.
That's all you have to do, really!
Feel free to leave comments and to share your ideas!
See you next week!
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@StephanieNich10 (Stephanie Nichols)#android I laughed so hard at ochocinco 's avi. Hahahaha http://t.co/MPuhhi3m
25 weeks 4 days ago -
@CarlaAtkins8 (Carla Atkins)#android Omg! This is actually f'n interesting http://t.co/JodMOehr
25 weeks 4 days ago -
@MarianMcleod12 (Marian Mcleod)#android Precisely what song is? http://t.co/YmJXU0rB
25 weeks 4 days ago -
@JoBeach15 (Jo Beach)#android haha this made me laugh, i love ted:-) http://t.co/gtcWQ79C
25 weeks 4 days ago -
@aochart3 (青ちゃ)Start playing Paradise Island on Android http://t.co/DEID0Ao5 #Android #Androidgames #Gameinsight http://t.co/e1bifSeL
25 weeks 4 days ago
Poll
Useful resources
Android Development Projects
- create and publish an app on Google Play by duberpain
- Skout similar for experts IOS and Android Developers only please let your bid be final, social app - repost 5 by expemployer
- News Android APP by orionpr
- Nonpublic project #4541416 by eas1975
- Cocos 2d - X Game developer for Android Required by SanketJain
- Android app with opencv by nimahosseini10
- Need a mobile app for iOS & Android by rowanhall
- Bar Drink Credit Mobile App System and Platform by codebarron
- An application for Android (and maybe ipad/iphone) by SdesignNoW
- Andriod IPTV Player by aissa2




