title: How to change the color of a selected item in a listview?
body: <p>I need to change the color of a selected item in a ListView when its clicked, so the user know what has clicked.</p>
  
  <p>So far I have done this code for that:</p>
  
  <pre><code>listview.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                  int position, long id) {
      for (int a = 0; a&lt; parent.getChildCount();a++){
      parent.getChildAt(a).setBackgroundColor(Color.TRANSPARENT);
      view.refreshDrawableState();
       if(parent.getChildAt(a) == view){
           view.setBackgroundColor(getResources().getColor(R.color.soft_opaque));
           view.refreshDrawableState();
      }
  }
  </code></pre>
  
  <p>What it does is to change the background color of the item selected and keep it like that until I click another item so it change only the current item selected background color</p>
  
  <p>I also was wondering if there is an XML way to do it. What I've found so far is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;item android:state_pressed="true" android:drawable="@color/orange" /&gt; &lt;!-- pressed --&gt;
      &lt;item android:state_focused="true" android:drawable="@color/black" /&gt; &lt;!-- focused --&gt;
      &lt;item android:state_selected="true" android:state_pressed="true" android:drawable="@color/black" /&gt; &lt;!-- focused and pressed--&gt;
      &lt;item android:drawable="@color/black" /&gt; &lt;!-- default --&gt;
  &lt;/selector&gt; 
  </code></pre>
  
  <p>but haven't got what I the previous code does. what it does, though, is to change the background color and change it when its clicked, but doesn't keep the selected item with an specific color</p>
  
  <p>EDIT:
  My adapter</p>
  
  <pre><code>    public class ExtraAdapter extends ArrayAdapter&lt;String&gt; {
      Context context;
      String[] tittleArray;
  
      public ExtraAdapter(Context c, String[] titles) {
          super(c, R.layout.custom_listview_extra, titles);
          this.context = c;
          this.tittleArray = titles;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View row = convertView;
          if (row == null) {
              LayoutInflater inflater = (LayoutInflater) context
                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              row = inflater.inflate(R.layout.custom_listview_extra, parent,
                      false);
          }
          TextView myTitle = (TextView) row.findViewById(R.id.plato_extra);
          myTitle.setText(tittleArray[position]);
  
          return row;
      }
  
  }
  </code></pre>
  
  <p>My Activity</p>
  
  <pre><code>    public class ExtraMenu extends ActionBarActivity {
      ListView lcategory;
      ListView lextras;
      ExtraAdapter adapter;
      String[] categoria;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_extra_test);
          lcategory = (ListView) findViewById(R.id.categorias);
          String[] listaExtras = getResources().getStringArray(R.array.listaExtras);
          adapter = new ExtraAdapter(getApplicationContext(), listaExtras);
          lcategory.setAdapter(adapter);
  
          lcategory.setOnItemClickListener(new OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                      int position, long id) {
                  parent.setSelected(true);
                  /*
                  for (int a = 0; a&lt; parent.getChildCount();a++){
                      parent.getChildAt(a).setBackgroundColor(Color.TRANSPARENT);
                      view.refreshDrawableState();
                      if(parent.getChildAt(a) == view){
                          view.setBackgroundColor(getResources().getColor(R.color.soft_opaque));
                          view.refreshDrawableState();
                      }
                  }*/
              }
          });
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          super.onCreateOptionsMenu(menu);
          getMenuInflater().inflate(R.menu.extra_test, menu);
          return true;
      }
      }
  </code></pre>
  
  <p>My Activity XML</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:weightSum="2" &gt;
  
      &lt;ImageView
          android:id="@+id/logo"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:layout_weight="1.5"
          android:src="@drawable/logo" /&gt;
  
      &lt;ListView
          android:id="@+id/categorias"
          android:layout_weight="0.5"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          //android:background="@drawable/list_color"
          android:layout_marginTop="20dp"&gt;
      &lt;/ListView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>My adapter XML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      style="@style/Widget.AppCompat.ListView.Menu"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal"
      android:paddingTop="10dip"
      android:paddingBottom="10dip"&gt;
  
      &lt;TextView
          android:id="@+id/plato_extra"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          android:layout_marginLeft="20dp"
          //android:background="@drawable/list_color"
          android:textColor="#000000"
          android:textSize="25sp" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>