Android在ListView中更改TextView字体颜色以获取条件

时间:2017-09-27 09:06:05

标签: android listview getview

我遇到与Change the color of a specified item in a listview for android(谢谢)回答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”);似乎无法正常工作;

3 个答案:

答案 0 :(得分:0)

看起来你错过了最后一次关闭&#39;)&#39;在你的命令中。否则它接缝正确:

view1.setTextColor(Color.parseColor("#E0E0EF"));

答案 1 :(得分:0)

 @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;
}

答案 2 :(得分:0)

向我们展示 db_msg 在该布局中的这个布局有一个TextView只是得到它的名称并替换为&#34; tvIDFrom_db_msg_layout&#34;这个

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;
      }
    };