Search code examples
androidlistviewsimpleadapter

Manipulate a row from a ListView populated by SimpleAdapter


Im populating a ListView using the following:

list = (ListView) findViewById(R.id.historylist);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String[] titles = new String[]{"transactionID","date","description","redeem","reward","status"};

try{

for(int i=0;i<rows.length();i++){
    map = new HashMap<String, String>();
    for(int n=0;n<allRows.getJSONArray(i).length();n++){
        map.put(titles[n], allRows.getJSONArray(i).getString(n));
    }
    mylist.add(map);
}

mSchedule = new SimpleAdapter(
    History.this,
    mylist,
    R.layout.history_row,
    titles,
    new int[] {R.id.textView0, R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5});

list.setAdapter(mSchedule);

for(int i=0; i<mylist.size(); i++)
    if(((Map<String, String>) mSchedule.getItem(i)).get("status").equals("0"))
        try{
            // SET BACKGROUND COLOR OF ROW
        }catch(Exception e){
            Log.e("AAA Error getting ListView row", e.toString());
        }

}catch(Exception e){
Log.e("Error Creating ListView", e.toString());
}

I'd like to change the background color of any ListView row which contains a status of "0".

Any ideas?

NEW: Here's the updated code - much nicer (Except for the part where I have to manually chop characters from the JSONArray.. eww..)

private void showListView(JSONArray rows, JSONArray totals){

    list = (ListView) findViewById(android.R.id.list);
    ArrayList<String[]> rowValues = null;
    String[] r = null;

    try{
        rowValues = new ArrayList<String[]>();
        for (int i=0;i<rows.length();i++){

            r = rows.get(i).toString().replaceAll("\\[\"", "").replaceAll("\"\\]", "").split("\",\"");
            rowValues.add(r);
        }

        mba = new MyBaseAdapter(this, rowValues);
        list.setAdapter(mba);

    }catch(Exception e){
        Log.e("showListView() ERROR", e.toString());
    }
}

Solution

  • I don't think you can. The only way to achieve what you are saying is creating a new class extending BaseAdapter and changing the background color of your view on the getView() method in that class