我正在編寫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的文本改變?這種關係何時建立?