0

Listview从android中的其他活动返回时,如何选择项目颜色保持不变?

请给我一个适当的解决方案。在这里,我附上了Adapter

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's 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);
        }



}

主要活动类

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);              

        }

    });
4

1 回答 1

0

为此,您需要保存已选择或未选择的 textview 状态。首先将地图创建为您的类的全局,

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

然后像这样改变你的getView方法..

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

        // only inflate the view if it's 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());

然后像这样将代码添加到侦听器...

    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);
    }
于 2013-04-09T04:51:56.613 回答