Search code examples
androidlistadapter

How do I dynamically change an Android ListAdaptor elements style base on a condition, for example change the color


'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
pDialog.dismiss();
// 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
                setListAdapter(adapter);
            }
        });

Solution

  • 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