O_Casper_ne 2013-04-15 03:27 采纳率: 7.7%
浏览 2142

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);
        }
    }
}
  • 写回答

1条回答 默认 最新

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2022-12-21 14:02
    关注

    ChatGPT尝试为您解答,仅供参考
    在这段代码中,有一个ListActivity,它的布局文件包含一个ListView,每一行都有一个CheckBox和一个TextView。ListActivity中还有一个TextView变量selection,用来显示选择的项目。


    在onCreate方法中,首先将ListActivity的布局设为R.layout.pchecklist,然后找到selection TextView,并初始化一个字符串数组tasks。接着,它会创建一个ArrayList,并将tasks数组中的每一项添加到ArrayList中,然后调用setListAdapter方法将ArrayList传递给CheckListAdapter,并将它设置为ListView的适配器。


    当用户点击ListView中的某一项时,会调用onListItemClick方法,在这个方法中,它会调用getTaskAt方法获取选择的项目,然后将它的toString方法的返回值设置为selection TextView的文本。


    CheckListAdapter是一个ArrayAdapter的子类,它的作用是将DailyTask对象转换为ListView中的行。在getView方法中,它会使用布局文件R.layout.lchecklist来创建每一行,并使用CheckListViewAccessor类来访问行中的CheckBox和TextView。


    当用户点击CheckBox时,会调用OnCheckedChangeListener的onCheckedChanged方法。在这个方法中,它会更新DailyTask对象的isCompleted属性,并将TextView的文本更改为大写。


    总的来说,这段代码实现了一个检查清单应用,用户可以在ListView中选择某一项,然后通过点击CheckBox来标记完成情况,每一行的TextView的文本也会相应地更改。


    总的来说,CheckBox和TextView之间的关系是在CheckListAdapter的getView方法中建立的,当用户点击CheckBox时,会调用OnCheckedChangeListener的onCheckedChanged方法,并更新DailyTask对象的isCompleted属性以及TextView的文本。

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!