Take the 2-minute tour ×

'In the below android list adapter I wish to dynamically change the colour in the display of an element based on a condition, for example if TAG_AMOUNT is greater than 2 then the text view is red. Any help welcome! (the below is based on androidhive JSON parsor)'

protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
// updating UI from Background Thread
runOnUiThread(new Runnable() {
    public void run() {
    * Updating parsed JSON data into ListView
    * */
    ListAdapter adapter = new SimpleAdapter(
        AllProductsActivity.this,     productsList,
        R.layout.list_item, new String[] { TAG_PID,TAG_NAME, TAG_AMOUNT, TAG_LOCATION},
       new int[] { R.id.pid, R.id.name, R.id.amount, R.id.location});
                // updating listview
share|improve this question

1 Answer 1

up vote 1 down vote accepted

Yes you can do this by Customizing SimpleAdapter class.

Here is sample code for the same.

What you basically will be doing is overriding getView method of SimpleAdapter.

Hope this helps

share|improve this answer
Thanks very much for that, only just spotted the reply. It works perfectly and taught me a few things I've been puzzling over. now to adapt it to exactly what I'm trying to do. Appreciated! –  user2365614 May 20 '13 at 18:33
The problem I have now in the SpecialAdapter shown in the link that CRUSADER posted is how you do a test on one of the elements in List<HashMap<String, String>>. In my case there are four categories, id, name, quantity and location, they are passed to List<HashMap<String, String> by productsList and productsList = new ArrayList<HashMap<String, String>>(); in the above example. one of the categories is quantity and I wish to say something like:- if quantity != 1 then color the background or change the font for example. very difficult to explain, so apologies if this is not clear. –  user2365614 May 20 '13 at 22:21

Your Answer


