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币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
TextView右边对齐父控件,左边对齐CheckBox的思路(高手绕道)
android:layout_alignParentRight="true" android:gravity="center_vertical" android:layout_alignTop="@id/login_save_password_checkbox" android:layout_alignBottom="@id/
android textview时实显示checkbox选中的内容
在项目需求中可能遇到这种情况,要求textview时实显示checkbox选中的内容。首先看效果图主要讲解两个知识点。1.listview点击checkbox选中: 首先checkbox设置: android:focusable="false" android:clickable="false" 然后在代码是用一个map作记录,初始化所有选中为 false,在点击
自定义组合控件:textView+checkBox
自定义组合控件:textView+checkBox 先实现布局文件ui_setting_view.xml xml version="1.0" encoding="utf-8"?> RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
Android代码生成TextView,Button,CheckBox等控件的设置技巧
前言代码生成控件时,有时候view的padding和margin,drawablRight,字体颜色的背景选择器,背景颜色的选择器和一些属性可能会和布局文件xml中的设置不一样,注意到这些细节后,我就整理了一些,网友们经常出现的设置问题。1、代码生成TextViewTextView textView = new TextView(this); textView.setText(mStrs[i]);
自定义控件----继承framLayout --textview与checkBox
//在布局文件中 com.bwei.administrator.view.zuheView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/zh" >com.bwei.administrator.view.zuheView> //自定义view中
android 点击变色的两种情况、textview点击变色以及自定义checkbox
1.不需要使用shape的情况 drawable下新建一个资源文件,在需要的地方setbackground 2.需要设置圆角等等需要自定义shape的,和1类似,只是里面的drawable="@color"换做@drawable
CheckedTextView 复选框左显示
<CheckedTextView android:layout_width="wrap_content" android:layout_height="?android:attr/listPreferredItemHeightSmall" android:textAppearan
CheckedTextView,CheckBox和Button样式定义
1.改变CheckBox的默认样式, 由方形变成自定义的形状,具体步骤如下: a> 定义drawable.xml文件,指定CheckBox的button 属性, drawable.xml如下所示“: <item android:state_checked="true" android:state_focused="true" android:drawabl
android开发中ListView与CheckBox的混合使用
首先,写个item的xml布局,里面放置一张图片、一个TextView和一个CheckBox。要 注意的是,这里设置了CheckBox没有焦点,这样的话,无法单独点击checkbox,而是在点 击listview的条目后,Checkbox会响应操作。 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an
CheckBox 的 checked & unchecked
Develop Tool: IntelliJ IDEA Source Code below_ src/com.hhy.ForLearning/MyActivity: package com.hhy.ForLearning; import android.app.Activity; import android.os.Bundle; import android.view