I need to highlight the selected row in list view once we select the item.

Eg : I select first item its gets highlighted, if i select 3rd item then remove 1st item highlighted and then highlight 3rd item.

I have seen many examples and questions posted in Stackoverflow but havent found a proper solution.

Below is my code, it works but i need to select the item twice to get highlighted, how can i modify this to work smoothly ?

```
ListView mainListView = (ListView) findViewById( R.id.mainListView );  

        // Create and populate a List of planet names.  
        planets = new String[] { "Mercury", "Venus", "Earth", "Mars",  
                                          "Jupiter", "Saturn", "Uranus", "Neptune",
                                          "Ceres","Pluto","Haumea","Makemake","Eris"};    
        planetList = new ArrayList<String>();  
        planetList.addAll( Arrays.asList(planets) );  

        planetsAdapter = new PlanetsAdapter (this,planetList);
        planetsAdapter .setNotifyOnChange(true);

        // Set the ArrayAdapter as the ListView's adapter.  
        mainListView.setAdapter(planetsAdapter );
        mainListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        mainListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                planetsAdapter.setSelectedPosition(position);
                planetsAdapter.notifyDataSetChanged();
            }
        });

```

Adapted Class :

```
public class PlanetsAdapter extends ArrayAdapter<String> {
        private ArrayList<String> planets;
        private Context mContext = null;
        private LayoutInflater inflater;

        // used to keep selected position in ListView
        private int selectedPos = -1;   // init value for not-selected

        public PlanetsAdapter (Context context,ArrayList<String> objects) {
            super(context,0,objects);
            mContext = context;
            planets = objects;
        }

        public void setSelectedPosition(int pos){
            selectedPos = pos;
            // inform the view of this change
        }

        public int getSelectedPosition(){
            return selectedPos;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                inflater = (LayoutInflater) mContext
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.list_item, null);
            }

            TextView title = (TextView) convertView
                    .findViewById(R.id.rowTextView);

            // change the row color based on selected state
            if(position == selectedPos){
                //title.setTextColor(Color.parseColor("#FFFFFF"));
                title.setBackgroundColor(Color.parseColor("#ED07E1"));
            }else{
                //title.setTextColor(Color.parseColor("#000000"));
                title.setBackgroundColor(Color.parseColor("#e2e2e2"));
            }

            title.setText(getItem(position));
            return convertView;
        }
    }

```

**Edit : Answered Below**

```
For anyone who is looking for answers, i have answered it below hope it helps.

```