Android 根据条件更改 ListView 中的 TextView 字体颜色

Android change TextView Font color in a ListView for a condition

我和 answered by Kartheek有同样的问题(谢谢),并进行了如下测试:

adapter = new ArrayAdapter<String>(this,R.layout.db_msg,messaggi){
      @Override
      public View getView(int position, View convertView, ViewGroup parent) 
{
          View view1 = super.getView(position, convertView, parent);
//          if (position % 2 == 0) {  //Place the condition where you want 
to change the item color.
         testo = messaggi.get(position);
          if(testo.substring(0,5).equals("27-09")){
           view1.setBackgroundColor(Color.parseColor("#e0e0ef"));
        } else {
            //Setting to default color.
            view1.setBackgroundColor(Color.WHITE);
        }
        return view1;
      }
    };

问题:我宁愿更改字体颜色,但是view1.setTextColor(Color.parseColor("#E0E0EF"); 似乎不起作用;

您似乎错过了命令中的最后一个结束符“)”。否则它接缝正确:

view1.setTextColor(Color.parseColor("#E0E0EF"));
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  View view1;
 view1=convertView;
 if (convertView == null) {
        view1 = inflater.inflate(R.layout.db_msg, null);
        testo = messaggi.get(position);
        if(testo.substring(0,5).equals("27-09")){
        view1.setBackgroundColor(Color.parseColor("#e0e0ef"));
    } 
else {
         //Setting to default color.
         view1.setBackgroundColor(Color.WHITE);
    }
       return convertView;
}

向我们展示 db_msg 这个布局在那个布局中有一个 TextView 只需获取它的名称并替换为 "tvIDFrom_db_msg_layout" 这个

adapter = new ArrayAdapter<String>(this,R.layout.db_msg,messaggi){
      @Override
      public View getView(int position, View convertView, ViewGroup parent) 
   {
          View view1 = super.getView(position, convertView, parent);
         if (position % 2 == 0) {  //Place the condition where you want to change the item color.
             testo = messaggi.get(position);
              TextView tvText = (TextView) view1.findViewById(R.id.tvIDFrom_db_msg_layout);
            if(testo.substring(0,5).equals("27-09")){

                 tvText.setTextColor(Color.parseColor("#yourHexCode"));
            } else {
                //Setting to default color.
                tvText.setTextColor(Color.WHITE);
            }
        return view1;
      }
    };