Search code examples
javaandroidlistviewcolorsandroid-arrayadapter

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!


Solution

  • 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.