2011-12-09 56 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() - 從您的角度來看,當前元素將被視爲「局部變量」。您目前不必擔心所有全球元素。