2011-12-09 24 views
0

我正在编写android上的一个清单,以下是教科书中的一个示例。 ListActivity由listview(R.layout.PCheckList)组成,其中每行都有一个复选框和一个锁定在水平布局(R.layout.lchecklist)内的文本视图。 有我完全不明白 的代码下面的代码的线,我突出的线复选框如何知道它与textview的关联

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); 
     } 
    } 
} 

在线表示,如何复选框知道的TextView的文本改变?这种关系何时建立?

回答

1

没有关联TextViewCheckBox。因为CheckBoxTextView。看看层次here

ava.lang.Object 
    ↳ android.view.View 
     ↳ android.widget.TextView 
      ↳ android.widget.Button 
       ↳ android.widget.CompoundButton 
        ↳ android.widget.CheckBox 

换句话说CheckBoxTextViewcheckedunchecked附加状态管理。 (其实CompoundButton负责国家管理,但这些都是细节)。

0

该关系由适配器建立。对于每个列表元素,“复选框”对象对应于实际列表元素的复选框。这意味着修改列表元素的视图非常容易,而且扩展它更改getView()中特定元素内的另一个元素非常困难。

你可能会这样想;在适配器中 - 对于每个getView() - 从您的角度来看,当前元素将被视为“局部变量”。您目前不必担心所有全球元素。