Listview selected item color changed when returning from another activity in android

How to make the Listviewselected color of the object remains unchanged when returning from another activity in android?

Please give me the right solution. Here I attached a Adapterclass

public class SelectedAdapter extends ArrayAdapter {

// used to keep selected position in ListView
        private int selectedPos = -1;   // init value for not-selected
        private int selitem;
        public SelectedAdapter(Context context, int textViewResourceId, List objects) {
            super(context, textViewResourceId, objects);
        }

        public void setSelectedPosition(int pos){

            selectedPos = pos;

            // inform the view of this change
            notifyDataSetChanged();
        }

        public int getSelectedPosition(){
            return selectedPos;
        }



@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;

            // only inflate the view if it null
            if (v == null) {
                LayoutInflater vi
                            = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.simplerow, null);
            }

            // get text view
            TextView label = (TextView)v.findViewById(R.id.rowTextView);

            // change the row color based on selected state
            if(selectedPos == position){
                label.setBackgroundColor(Color.CYAN);

                //label.setBackgroundResource(R.color.blue2);
            }else{
                label.setBackgroundColor(Color.WHITE);
            }

            label.setText(this.getItem(position).toString());
            /*
            // to use something other than .toString()
            MyClass myobj = (MyClass)this.getItem(position);
            label.setText(myobj.myReturnsString());
            */
            return(v);
        }



}

Main activity class

final Context context = this;
protected ListView applv ;
private ArrayAdapter<String> listAdapter ;
private SelectedAdapter selectedAdapter;
private ArrayList list;

            list = new ArrayList();

    list.add("Actuator");
    list.add("Agitator - Liquid");
    list.add("Agitator - Slurry");
    list.add("Air Separator");
    list.add("Belt Plow");
    list.add("Blower");


    applv = (ListView) findViewById( R.id.lvapps);
    applv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

    selectedAdapter = new SelectedAdapter(this,0,list);
    selectedAdapter.setNotifyOnChange(true);
    applv.setAdapter( selectedAdapter );


    applv.setOnItemClickListener(new OnItemClickListener() {      
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {

            itemsel = applv.getAdapter().getItemViewType(position);
            // TODO Auto-generated method stub  
            item = applv.getAdapter().getItem(position).toString();
            selectedAdapter.setSelectedPosition(position);              

        }

    });
0
source share
1 answer

To do this, you need to save the selected text view states. first create a map as global for ur class, like,

Map<Integer, Boolean> map_textcolor=new HashMap<Integer, Boolean>();

then change the getView ur method this way.

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        // only inflate the view if it null
        if (v == null) {
            LayoutInflater vi
=  (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.simplerow, null);
        }

        // get text view
        TextView label = (TextView)v.findViewById(R.id.rowTextView);
*******************************************************************************
        // change the row color based on selected state
        Boolean selectedPos=map_textcolor.get(position);
        if(selectedPos!=null&&selectedPos.booleanValue())
        {
       //label.setSelected(true);
       label.setTextColor(Color.CYAN);
       selected_view=label;
        }
        else
        {
       //label.setSelected(false);
       label.setTextColor(Color.WHITE);
        }
******************************************************************************         
        label.setText(this.getItem(position).toString());

then add code to listner like this ...

    final int p=position;
    label.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
    // TODO Auto-generated method stub
      if(selected_view!=null)
  {
    //selected_view.setSelected(false);
    label.setTextColor(Color.WHITE);
    map_textcolor.remove(prev_pos);
  }
  selected_view=view;
 // selected_view.setSelected(true);
  label.setTextColor(Color.CYAN);
  map_textcolor.put(p, true);
  prev_pos=p;
}
});
        /*
        // to use something other than .toString()
        MyClass myobj = (MyClass)this.getItem(position);
        label.setText(myobj.myReturnsString());
        */
        return(v);
    }
0
source

All Articles