简体   繁体   English

当ArrayList中的值等于Android中的某个值时,如何更改ListView项目的文本颜色?

[英]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 我像这样使用SimpleAdapter将studentList放在列表视图中

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. 它完美地显示了学生列表,但是我想从studentList ArrayList中更改attendance == "absent"的学生的文本颜色。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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