当我在分隔的列表视图中滚动时,复选框状态已更改

时间:2013-07-16 10:22:08

标签: android listview checkbox

我需要使用复选框在分离的列表视图中显示数据。在那里我检查了一些视图,但当我滚动时,检查状态更改为未选中。我正在使用以下课程来显示我的列表视图。

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

我的屏幕视图 enter image description here

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

1 个答案:

答案 0 :(得分: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;
        }
    }
}