简体   繁体   English

在检查某些项目后,清单在清单中的位置如何变化

[英]How come the position of items in my checklistview change after I check on some items

I'm coding a really basic check list app. 我正在编写一个非常基本的清单应用程序。 The user can add tasks and then check them off. 用户可以添加任务,然后将其选中。 But for some reason, when the user check an item - the position in the list changes. 但是由于某种原因,当用户检查项目时,列表中的位置会更改。 Here are a couple of screenshots to show what I mean. 这是几个截图,以显示我的意思。 The code of my ListAdapter follows: 我的ListAdapter的代码如下:

(The Number at the end of the list item is it's position. eg Do Laundry [False]0 is item at position 0.) (列表项末尾的数字是其位置。例如,“洗衣服[False] 0是位置0处的项目。”)

3个项目被新添加到列表中一项被检查检查两个IME

CODE

class TaskListAdapter extends ArrayAdapter<Task> implements OnCheckedChangeListener
    {
        Activity        context;
        View            recycledView;
        ViewAccessor    viewAccessor;
        CheckBox        checkbox;
        int             position;

        public TaskListAdapter(Activity context, int resource, int textViewResourceId, List<Task> objects) {
            super(context, resource, textViewResourceId, objects);
            this.context = context;
        }


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

            this.recycledView   = convertView;

            if(recycledView == null)
            {
                LayoutInflater inflater = context.getLayoutInflater();

                recycledView        = inflater.inflate(R.layout.checkview, null);

                this.viewAccessor   = new ViewAccessor(recycledView);
                this.recycledView.setTag(this.viewAccessor);

                this.checkbox   = (CheckBox) recycledView.findViewById(R.id.cbTaskCompleted);
                this.checkbox.setOnCheckedChangeListener(this);
                this.checkbox.setTag((Integer) position);

            }else
            {
                viewAccessor = (ViewAccessor) this.recycledView.getTag();
                this.checkbox = viewAccessor.getCheckBox();
            }

            this.checkbox.setText( taskList.get(position).toString()+String.valueOf(position) );
            this.checkbox.setChecked( taskList.get(position).isCompleted );

            return recycledView;
        }

        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

            Task taskUnderConsideration = taskList.get( (Integer) buttonView.getTag() ) ;
            taskUnderConsideration.setCompleted(isChecked);
            buttonView.setText(taskUnderConsideration.toString()+buttonView.getTag().toString());
        }
    }

    class ViewAccessor
    {
        View v;
        CheckBox cb = null;

        ViewAccessor(View v)
        {
            this.v = v;
        }

        CheckBox getCheckBox()
        {
            if(cb==null)
                cb = (CheckBox) v.findViewById(R.id.cbTaskCompleted);
            return cb;
        }


    }

If you want to show simple row (with single TextView and CheckBox ), I'll recommend you to use ListView 's method: 如果要显示简单的行(带有单个TextViewCheckBox ),我建议您使用ListView的方法:

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

It'll automatically show the (built-in) CheckBox with the TextView . 它将自动使用TextView显示(内置) CheckBox

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM