简体   繁体   English

更改列表视图上一个文本视图的颜色,而不更改其他颜色

[英]Change color of one textview on listview without change others

I need to change the color of my listview backgroundcolor property. 我需要更改listview backgroundcolor属性的颜色。 I can do it, I change the color, but it changes all my rows with the same color. 我可以做到,更改颜色,但是它更改所有具有相同颜色的行。 I mean, I need one row with red color, other with green color... I put the code below to help: 我的意思是,我需要一行红色,而另一行是绿色...我在下面提供了帮助:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, configureList(idRuta, rocodromo, dificultad)) 
          {
              @Override
              public View getView(int position, View convertView, ViewGroup parent) 
              {

                  View view = super.getView(position, convertView, parent);
                  TextView text = (TextView) view.findViewById(android.R.id.text1);

                 for(int i = 0; i < colores.size(); i++)
                 {
                     if(colores.get(i).equals("0"))
                     {
                         text.setBackgroundColor(Color.GREEN);
                     }
                     else if(colores.get(i).equals("1"))
                     {
                         text.setBackgroundColor(Color.RED);
                     }
                     else if(colores.get(i).equals("2"))
                     {
                         text.setBackgroundColor(Color.YELLOW);
                     }
                     else
                     {
                         text.setBackgroundColor(Color.WHITE);
                     }
                 }

                 return view;
              }
          };

At the first time, I have "colores.get(i) = 1", so it changes color to RED, but then I have "colores.get(i) = 2", so it changes the color to YELLOW. 第一次,我有“ colores.get(i)= 1”,因此它将颜色更改为红色,但是后来我有“ colores.get(i)= 2”,因此将颜色更改为黄色。 But I need to change ONLY the second row to yellow, not the first one, the first one has to be RED. 但是我只需要将第二行更改为黄色,而不是第一行,第一行必须为红色。

In "colores" I have all the color list that I need to change, order by row, for example, when "i=0", I change the row 0 to that color, but when "i=1" I want to change only the row 1, not all the rows. 在“颜色”中,我具有需要更改的所有颜色列表,例如按行排序,例如,当“ i = 0”时,我将行0更改为该颜色,但是当“ i = 1”时,我想更改仅第1行,而不是所有行。

Can anyone helps me? 谁能帮我? Thanks! 谢谢!

Try this: 尝试这个:

if (position % 2 == 1) {
    text.setBackgroundColor(Color.RED);
} else {
    text.setBackgroundColor(Color.YELLOW);  
}

This code sets background color or odd number rows to YELLOW and even to RED 此代码将背景色或奇数行设置为黄色,甚至设置为红色

I think You should take in account "position" parameter given to function getView. 我认为您应该考虑赋予函数getView的“位置”参数。 I assume You like to have first row: GREEN, next RED, next YELLOW, and all the rest WHITE. 我假设您喜欢第一行:绿色,下一个红色,下一个黄色以及所有其余的白色。 To do so: 为此:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, configureList(idRuta, rocodromo, dificultad)) 
      {
          @Override
          public View getView(int position, View convertView, ViewGroup parent) 
          {

              View view = super.getView(position, convertView, parent);
              TextView text = (TextView) view.findViewById(android.R.id.text1);


                 if(colores.get(position).equals("0"))
                 {
                     text.setBackgroundColor(Color.GREEN);
                 }
                 else if(colores.get(position).equals("1"))
                 {
                     text.setBackgroundColor(Color.RED);
                 }
                 else if(colores.get(position).equals("2"))
                 {
                     text.setBackgroundColor(Color.YELLOW);
                 }
                 else
                 {
                     text.setBackgroundColor(Color.WHITE);
                 }


             return view;
          }
      };

In this approach each row (poistion is index) will be tested for color. 在这种方法中,将对每一行(位置为索引)进行颜色测试。

Iteration through colors array is not necessary here, because "position" index should be equal to index of color from colors array. 在此不需要通过颜色数组进行迭代,因为“位置”索引应等于颜色数组中颜色的索引。

Remove that for loop and get color by listview item position. 删除for循环并通过listview项目位置获取颜色。 You just need to set color in each row view. 您只需要在每个行视图中设置颜色。 Change like below, 如下更改

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, configureList(idRuta, rocodromo, dificultad)) 
      {
          @Override
          public View getView(int position, View convertView, ViewGroup parent) 
          {

              View view = super.getView(position, convertView, parent);
              TextView text = (TextView) view.findViewById(android.R.id.text1);

                 if(colores.get(position).equals("0"))
                 {
                     text.setBackgroundColor(Color.GREEN);
                 }
                 else if(colores.get(position).equals("1"))
                 {
                     text.setBackgroundColor(Color.RED);
                 }
                 else if(colores.get(position).equals("2"))
                 {
                     text.setBackgroundColor(Color.YELLOW);
                 }
                 else
                 {
                     text.setBackgroundColor(Color.WHITE);
                 }

             return view;
          }
      };

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

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