title: How can I keep the state in an item from a ListView (like the item selected) when I scroll the list?
body: <p>I have a problem with a listview; I created two ListView in two fragments to show them in the same Layout </p>
  
  <p>And when I select an item from the left ListView, automatically refresh the right ListView according to the item selected.
  The thing is that when I select an item from the right ListView, it changes the color to green, and that's ok because I programmed in that way, and if I select another item it has to change to green too, and keep the original color from the first selected item, like I show you in the image below</p>
  
  <p><a href="http://i.stack.imgur.com/iZPUc.png" rel="nofollow">First selection</a></p>
  
  <p>but when I scroll the left ListView to select another item, and I return to the top of the list, the state of the item selected has gone,as I show below</p>
  
  <p><a href="http://i.stack.imgur.com/GGzsk.png" rel="nofollow">The new view, without keep the state selected</a></p>
  
  <p>and I know the reason is because the list view recycles its items views, but I've tried many things to try to keep the state no matter if I scroll the list, but I could't achieve this goal.
  And at this point, I don't know what i have to change from my code; I show you the important parts; if you need more information o more from my code tell me please.</p>
  
  <p>The constructor of an ListItem:</p>
  
  <pre><code>    public class Lista_Item {
          private String color, texto;
          private String textoSuperior, textoInferior;
          boolean seleccionado = false;
  
      public Lista_Item(String color, String textoSuperior, String textoInferior, boolean seleccionado) {
          // TODO Auto-generated constructor stub
          this.color = color;
          this.textoSuperior = textoSuperior;
          this.textoInferior = textoInferior;
          this.seleccionado = seleccionado;
      }
  
      public Lista_Item(String color, String texto) {
          // TODO Auto-generated constructor stub
          this.color = color;
          this.texto = texto;
      }
  
      public String getTextoSuperior() {
          return textoSuperior;
      }
  
      public String getTextoInferior() {
          return textoInferior;
      }
  
      public boolean getSeleccionado() {
          return seleccionado;
      }
  
      public String getTexto() {
          return texto;
      }
  
      public String getColor() {
          return color;
      }
  
  
      public void setSeleccionado(boolean seleccionado) {
            this.seleccionado = seleccionado;
           }
  
  
  
  }
  </code></pre>
  
  <p>The adapter:</p>
  
  <pre><code>private class ListAdapter extends  ArrayAdapter&lt;Lista_Item&gt; {
  
  private int mResourceId = 0;
  private LayoutInflater mLayoutInflater;
  
  public ListAdapter(Context context, int resource, int textViewResourceId, ArrayList&lt;Lista_Item&gt; listaItem) {
      super(context, resource, textViewResourceId, listaItem);
      mResourceId = resource;
      mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }
  
  
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
      ViewHolder holder = null;
  
      if (convertView == null) {
  
          convertView = mLayoutInflater.inflate(mResourceId, parent, false);
          holder = new ViewHolder();
  
          holder.name = (TextView) convertView.findViewById(R.id.tvItemListaColor);
          holder.l = (LinearLayout)convertView.findViewById(R.id.layoutColor);
  
          convertView.setTag(holder);
      }else{
          holder = (ViewHolder) convertView.getTag();
      }
  
      Lista_Item pedido = listItems.get(position);
      holder.name.setText(pedido.getTexto());
      holder.l.setBackgroundColor(Color.parseColor(pedido.getColor()));
      holder.name.setTag(pedido);
  
      return convertView;
  }
  
  private class ViewHolder {
      TextView name;
      LinearLayout l;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The event OnItemClickListener:</p>
  
  <pre><code>public AdapterView.OnItemClickListener d = new AdapterView.OnItemClickListener(){
      @Override
      public void onItemClick(AdapterView&lt;?&gt; list, View view, int pos, long id) {
          // TODO Auto-generated method stub
  
      if(listener!=null){
          listener.onPedidoSeleccionado((Lista_Item)list.getAdapter().getItem(pos));
      }
  
      String[] item = parts[pos].split("!");
      Toast.makeText(getActivity(), "Ha pulsado el item " +item[0], Toast.LENGTH_SHORT).show();
      Log.d("Color","Color = "+colorSaved);
  
      if (currentSelectedView != null &amp;&amp; currentSelectedView != view) {
          unhighlightCurrentRow(currentSelectedView, colorSaved);
          colorSaved = item[1];
      }
  
      currentSelectedView = view;
      highlightCurrentRow(currentSelectedView);
      colorSaved = item[1];
  }
  </code></pre>
  
  <p>};</p>
  
  <p>And the methods that I used above:</p>
  
  <pre><code>private void unhighlightCurrentRow(View rowView, String color) {
          rowView.setBackgroundColor(Color.parseColor("#"+color));
      }
  
      private void highlightCurrentRow(View rowView) {
          rowView.setBackgroundColor(Color.GREEN);
  
      }
  </code></pre>
  
  <p><strong>Please, any help???</strong></p>