簡體   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?

我想更改班級列表中缺席的學生的文字顏色。

我有

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

並在里面放弦

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

studentList.add(map);

我像這樣使用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);

它完美地顯示了學生列表,但是我想從studentList ArrayList中更改attendance == "absent"的學生的文本顏色。 我怎么做?

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