2 o casper ne O_Casper_ne 于 2013.04.15 11:27 提问

checkbox 如何知道与它匹配的 textview?

我在android上创建了一个checklist,ListActivity中包含一个 listview (R.layout.PCheckList) ,每一行都有一个checkbox和一个textview,被锁定在一个水平布局(R.layout.lchecklist)中。
就是有一行代码不知道啥意思。checkbox如何知道TextView里面的哪行text被改变了,他们之间的关系是什么时候建立的?

public class PChecklist extends ListActivity {

    TextView selection;
    String[] tasks={"Do Laundry",
            "Wash Dishes",
            "Make the bed",
            "Study",
            "Buy Groceries",
            "Mow the lawn",
            "Shave",
            "Iron Clothes",
            "Breathe periodically"};

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.pchecklist);

        this.selection = (TextView) findViewById(R.id.selection2);


        ArrayList<DailyTask> dailyTaskListModel = new ArrayList<DailyTask>();
        for(String t:tasks)
        {
            dailyTaskListModel.add(new DailyTask(t));
        }

        this.setListAdapter(new CheckListAdapter(this,dailyTaskListModel));
    }

    private DailyTask getTaskAt(int position)
    {
        return ((CheckListAdapter)getListAdapter()).getItem(position);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        this.selection.setText(String.format("Selection: %s", getTaskAt(position).toString()));
    }

    class DailyTask
    {
        String task;
        boolean isCompleted = false;

        DailyTask(String task)
        {
            this.task = task;
        }

        public String toString()
        {
            if(this.isCompleted)
                return (task.toUpperCase());
            else return (task);
        }
    }

    class CheckListAdapter extends ArrayAdapter<DailyTask>
    {
        Activity activity;

        CheckListAdapter(Activity context,ArrayList<DailyTask> taskList)
        {
            super(context, R.layout.lchecklist,taskList);

            this.activity = context;
        }


        public View getView(int position, View convertView, ViewGroup parent) {
            View recycledView = convertView;
            CheckListViewAccessor checkListViewAccessor=null;
            CheckBox checkbox;

            if(recycledView==null)
            {
                //if there is no view, we have to make one by inflating a layout.
                LayoutInflater inflater = activity.getLayoutInflater();
                recycledView = inflater.inflate(R.layout.lchecklist, null,false);

                checkListViewAccessor = new CheckListViewAccessor(recycledView);
                recycledView.setTag(recycledView);
                checkbox = checkListViewAccessor.getCheckBox();

                CompoundButton.OnCheckedChangeListener checkStateChangedListener = new CompoundButton.OnCheckedChangeListener(){

                    public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
                        //When the check button is pressed, we want two things to happen.
                        //1. Update the model.
                            //for some reason we have to do this.
                            int positionOfCheckedItem = (Integer) cb.getTag();

                            DailyTask task = getTaskAt(positionOfCheckedItem);
                            task.isCompleted = isChecked;

                        //2. Change the String in the row to upper case.
                            cb.setText(task.toString());
                    }
                };

                checkbox.setOnCheckedChangeListener(checkStateChangedListener);

            }else//if recycledView is not null, then we don't need to add a listener, we just need to get access to the UI components
            {
                checkListViewAccessor = (CheckListViewAccessor) recycledView.getTag();
                checkbox = checkListViewAccessor.getCheckBox();
            }

            DailyTask task = getTaskAt(position);
            checkbox.setTag(new Integer(position));
            **checkbox.setText(task.toString());
                    //^This line I don't get.**
            checkbox.setChecked(task.isCompleted);


            return (recycledView);
        }

    }

    class CheckListViewAccessor
    {
        View checkListView;
        CheckBox checkbox=null;

        CheckListViewAccessor(View checkListView)
        {
            this.checkListView = checkListView;
        }

        CheckBox getCheckBox()
        {
            if(checkbox==null)
                this.checkbox = (CheckBox) findViewById(R.id.checkbox);
            return (checkbox);
        }
    }
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!