简体   繁体   中英

Change color of one textview on listview without change others

I need to change the color of my listview backgroundcolor property. 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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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