在android中滚动时保持复选框的状态

时间:2014-12-16 11:32:57

标签: android

我在做什么:

  • 我想在listview中设置复选框并尝试主要状态 onscroll
  • 我试过有布尔数组状态,但无法实现它......所以 我想出了模特课


AdptSearchFilterCategories .java

public class AdptSearchFilterCategories extends BaseAdapter {

    private Context context;
    List<ModelFilterCategories> mdlLst;

    public AdptSearchFilterCategories(Context context,
            List<ModelFilterCategories> mdlLst) {
        this.context = context;
        this.mdlLst = mdlLst;
    }

    @Override
    public int getCount() {
        return mdlLst.size();
    }

    @Override
    public Object getItem(int position) {
        return mdlLst.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    static class ViewHolder {
        protected CheckBox chkBxCatId;
    }

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

        final ModelFilterCategories lclData = mdlLst.get(position);

        ViewHolder viewHolder = null;
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.adpt_searchfilter_category, null);

            viewHolder = new ViewHolder();
            viewHolder.chkBxCatId = (CheckBox) convertView.findViewById(R.id.chkBxCatId);
            viewHolder.chkBxCatId.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                            mdlLst.get(getPosition).setCheckboxState(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                        }
                    });
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.chkBxCatId.setTag(position); // This line is important.

        viewHolder.chkBxCatId.setText(lclData.getCategoryName());
        // txtTitle.setId(Integer.valueOf(mapData.get("id")));
        viewHolder.chkBxCatId.setTag(lclData.getCategoryId());


        return convertView;
    }

}

ModelFilterCategories.java

public class ModelFilterCategories {

    boolean checkboxState;
    String categoryName;
    String categoryId;

    public boolean isCheckboxState() {
        return checkboxState;
    }
    public void setCheckboxState(boolean checkboxState) {
        this.checkboxState = checkboxState;
    }
    public String getCategoryName() {
        return categoryName;
    }
    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }
    public String getCategoryId() {
        return categoryId;
    }
    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }

}

日志:

12-16 17:00:16.000: E/AndroidRuntime(9421): FATAL EXCEPTION: main
12-16 17:00:16.000: E/AndroidRuntime(9421): Process: com.windhyaworks, PID: 9421
12-16 17:00:16.000: E/AndroidRuntime(9421): java.lang.ClassCastException: java.lang.String cannot be cast to com.windhyaworks.adapters.AdptSearchFilterCategories$ViewHolder
12-16 17:00:16.000: E/AndroidRuntime(9421):     at com.windhyaworks.adapters.AdptSearchFilterCategories.getView(AdptSearchFilterCategories.java:69)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.AbsListView.obtainView(AbsListView.java:2255)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.ListView.onMeasure(ListView.java:1175)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.View.measure(View.java:16540)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.View.measure(View.java:16540)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.View.measure(View.java:16540)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.View.measure(View.java:16540)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.View.measure(View.java:16540)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at com.navdrawer.SimpleSideDrawer.onMeasure(SimpleSideDrawer.java:443)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.View.measure(View.java:16540)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.View.measure(View.java:16540)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1942)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1132)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1321)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.Choreographer.doFrame(Choreographer.java:544)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.os.Handler.handleCallback(Handler.java:733)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.os.Looper.loop(Looper.java:136)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at android.app.ActivityThread.main(ActivityThread.java:5086)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at java.lang.reflect.Method.invokeNative(Native Method)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at java.lang.reflect.Method.invoke(Method.java:515)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-16 17:00:16.000: E/AndroidRuntime(9421):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

试试这个

// getView

 if (convertView == null) {
            convertView = mInflater
                    .inflate(R.layout.filter_list_view, null);
            holder = new ViewHolder();

            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.checkBox = (CheckBox) convertView
                    .findViewById(R.id.checkBox);
            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                        ((FilterListBean) itemList.get(getPosition)).setSelected(buttonView.isChecked()); // had to use casting since get method was required,Set the value of checkbox to maintain its state.
                    }
                });

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }



        holder.checkBox.setTag(position); 
        holder.title.setText(menuList.getTitle());
        holder.checkBox.setChecked( menuList.getSelected());

//在适配器的构造函数中

for (int i = 0; i < this.getCount(); i++) {
        Log.v("count---", getCount() + "");
        itemChecked.add(i, false); // initializes all items value with false
    }

//适配器类变量列表

  private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();

答案 1 :(得分:0)

如果您在ListView上的操作不是太昂贵,您可以只注释该部分,getView方法每次都会返回一个新行,而不是从缓存中获取它。

 if (convertView == null) 

您的代码将如下所示

final ModelFilterCategories lclData = mdlLst.get(position);
ViewHolder viewHolder = null;           
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.adpt_searchfilter_category, null);
viewHolder = new ViewHolder();
viewHolder.chkBxCatId = (CheckBox) convertView.findViewById(R.id.chkBxCatId);
viewHolder.chkBxCatId.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();  // Here we get the posion that we have set for the checkbox using setTag.
                            mdlLst.get(getPosition).setCheckboxState(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                        }
                    });
        viewHolder.chkBxCatId.setTag(position); // This line is important.
        viewHolder.chkBxCatId.setText(lclData.getCategoryName());
        // txtTitle.setId(Integer.valueOf(mapData.get("id")));
        viewHolder.chkBxCatId.setTag(lclData.getCategoryId());
        return convertView;

答案 2 :(得分:0)

偷偷摸摸的错误

我有适配器::

<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/chkBxCatId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CheckBox"
    android:textColor="@android:color/white" />

我修改为::

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content">
<CheckBox 
    android:id="@+id/chkBxCatId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CheckBox"
    android:textColor="@android:color/white" />
</LinearLayout>