0

ListviewAndroidで他のアクティビティから戻ったときに、選択したアイテムの色を変更したままにする方法は?

適切な解決策を教えてください。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

そのためには、選択されているかどうかにかかわらず、テキストビューの状態を保存する必要があります。最初にマップを ur クラスへのグローバルとして作成します。

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 に答える