• Announcements

    • Reminder - MoDaCo position on illegal content   07/30/15

      ILLEGAL CONTENT I'd like to just reaffirm MoDaCo's position regarding piracy and illegal content in the light of some recent questions / postings. Posts will be censored by myself or my moderation team if the contain or link to: Illegal / pirated / cracked software or sites that host such softwareNintendo emulators / ROMs or sites hosting them (in light of Nintendo's legal stance)CUSTOM ROMS You may discuss and post links to custom device ROMs on MoDaCo, provided the following rules are adhered to: ROMs must not contain any illegal 3rd party software (this includes trial versions included without permission)ROMs must give full credit to the original authorISSUES If you have any issues with this policy, please contact PaulOBrien directly via PM.
    • Reminder: Selling items on the forum directly is not allowed   07/30/15

      Please note that selling items on the forum directly is not allowed by the forum rules. There is a forum for eBay auctions whereby you can list the items on eBay and link to them there. This is the ONLY forum for this type of activity. You may also advertise links to the eBay forum in your signature. Please note that selling directly in contravention of these rules will result in a warning / suspension / ban.
Sign in to follow this  
Followers 0

How to Update Custom Listview Images

3 posts in this topic

Posted (edited)

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() { }.

saupload_latitude_list_view.jpg

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);

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 String[] {"name", "category", "price", "imageUri"},

*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, enjoy!

Edited by afolery
0

Share this post


Link to post
Share on other sites

Posted (edited)

Might also want to add a call to BaseAdapter.notifyDataSetChanged() in step 3 otherwise the images already on the screen won't refresh until they get invalidated by scrolling off-screen and new entries won't get picked up.

Edited by Uxian
0

Share this post


Link to post
Share on other sites

Posted

This is exactly what I've been looking for! Quick question for you? I am new to all of this but by tutorials such as yours I'm gaining the experiance I need to create wonderful apps like the one you have listed. My question is, can you implement something like this to read from a database? So anytime the user inputs their information it would automaticly get updated to the listvew? Also, is there anyway you can email me a working copy of this project so I can play around with it and better understand how you created it? I work better with working examples. =)

Thank you. :D

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() { }.

saupload_latitude_list_view.jpg

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);

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 String[] {"name", "category", "price", "imageUri"},

*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, enjoy!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

MoDaCo is part of the MoDaCo.network, © Paul O'Brien 2002-2015. MoDaCo uses IntelliTxt technology.