2011-12-09 15 views
0

私はテキストの例に従って、アンドロイドのチェックリストをコーディングしています。 ListActivityはリストビュー(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); 
     } 
    } 
} 

を強調してきました?この関係はいつ確立されましたか?

答えて

1

TextViewにはCheckBoxが関連付けられていません。 CheckBoxTextViewです。階層hereを見てみましょう:言い換えれば

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

CheckBoxcheckeduncheckedのための追加の状態管理とTextViewです。 (事実、CompoundButtonは州の管理を担当していますが、詳細はこちらです)。

0

この関係は、アダプタによって確立されます。各リスト要素について、「チェックボックス」オブジェクトは実際のリスト要素のチェックボックスに対応する。これは、リスト要素のビューを変更することは非常に簡単であり、拡張ではgetView()の特定の要素内から別の要素を変更するのは非常に難しいことを意味します。

あなたはこのように考えることができます。アダプタ内では - getView()ごとに - 現在の要素は、あなたの視点から「ローカル変数」とみなされます。現時点では、すべてのグローバル要素を気にする必要はありません。

関連する問題