简体   繁体   中英

How do I change the text color of a ListView item when a value inside an ArrayList is equals to a certain value in Android?

I want to change the text color of students who were absent in a class list.

I have

ArrayList<HashMap<String, String>> studentList = studentList = new ArrayList<HashMap<String, String>>();

and placed Strings inside

HashMap<String, String> map = new HashMap<String, String>();
map.put("idno", sidno);
map.put("fullname", sfullname);
map.put("attendance", attendance);

studentList.add(map);

I placed the studentList in a listview using a SimpleAdapter like this

ListAdapter adapter = new SimpleAdapter(CheckAttendanceActivity.this, studentList, R.layout.list_students, new String[]{"idno", "fullname", "attendance"}, new int[]{R.id.idno, R.id.fullname, R.id.attendance});
setListAdapter(adapter);

It displays the the list of students perfectly but I want to change the text color of the students who has attendance == "absent" from the studentList ArrayList. How do I do that?

AdaptergetView方法中,检查attendance == "absent" ,然后更改背景颜色

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.your_item_layout, parent, false);
    }
    ....
    if (getItem(position).get("attendance").equals("absent")) {   
        convertView.setBackgroundColor(getContext().getColor(R.color.bg_color_absent));
    } else {      
        convertView.setBackgroundColor(getContext().getColor(R.color.bg_color_normal));
    }

    return convertView;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM