0

我需要在带有复选框的单独列表视图中显示数据。在那我检查了一些视图,但是当我滚动时,选中状态变为未选中。我正在使用以下类来显示我的列表视图。

public class SearchAdapter extends ArrayAdapter<Map<String,?>>{
        private LayoutInflater inflater;
        private int resId=0; 
        private List<Map<String,?>> listitem;
        public SearchAdapter(Context context, int resId, List<Map<String,?>> dataList){
            super(context, 0, dataList);
            this.resId = resId;
            inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.listitem = dataList;   
        }
        @Override
         public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
         TextView code,date; CheckBox checkbox;
         view = inflater.inflate(resId, parent, false);
         code = (TextView)view.findViewById(R.id.code);
         date= (TextView)view.findViewById(R.id.date);
         checkbox=(CheckBox)view.findViewById(R.id.check);
         Map<String,?> item = listitem.get(position);
         final String[] values=(String[]) item.get("values");
         checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              Integer pos = (Integer)buttonView.getTag();     
             if(isChecked)
             {
              Log.e("checked","checked"+position);
               } else{
                   Log.e("checked","unchecked");
                    }
                    }
                    });
            code.setText(values[32]);
            date.setText(values[31]);

                return view;
        }       
    } 

我的屏幕视图 在此处输入图像描述

所以,请指导我,如何克服这个问题

4

1 回答 1

3

我是同样的问题,但我通过这段代码解决了

public class CustomizeListView extends ListActivity {

    LayoutInflater inflater;
    static int index = 0;

    // AutoCompleteTextView autoCompleteTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, arrayList_Category);
        dataAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        final CustomAdapter adapter = new CustomAdapter(this,
                R.layout.activity_orderitem, arrayListItems);
        setListAdapter(adapter);
    }

    private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>
            implements OnItemSelectedListener {
        boolean[] checkBoxState;
        ViewHolder viewHolder;

        public CustomAdapter(Context context, int textViewResourceId,
                ArrayList<HashMap<String, Object>> arrayListItems) {
            // let android do the initializing :)
            super(context, textViewResourceId, arrayListItems);
            checkBoxState = new boolean[arrayListItems.size()];
        }

        // class for caching the views in a row
        private class ViewHolder {
            CheckBox checkBox;
        }

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            Log.d("getView", "getView");
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_orderitem,
                        null);
                viewHolder = new ViewHolder();
                // cache the views
                convertView.setTag(viewHolder);
            } else
                viewHolder = (ViewHolder) convertView.getTag();

            viewHolder.checkBox.setChecked(checkBoxState[position]);

            viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if (((CheckBox) v).isChecked()) {
                        checkBoxState[position] = true;
                    } else {
                        checkBoxState[position] = false;
                    }
                }
            });

            return convertView;
        }
    }
}
于 2013-07-16T10:34:22.613 回答