例如,您应该在自定义适配器的 getView() 方法中设置它们
@Override
public View getView( int position, View convertView, ViewGroup parent ){
ReportListItem listItem = getItem( position );
TextView reportNameTextView;
CheckBox isCheckedCheckBox;
if( convertView == null ) {
//creating convert view
convertView = layoutInflater.inflate( R.layout.report_list_item_layout, null );
//initializing views
reportNameTextView = (TextView) convertView.findViewById( R.id.reportListItem_reportNameTextView );
isCheckedCheckBox = (CheckBox) convertView.findViewById( R.id.reportListItem_isCheckedCheckBox );
//setting tag for view
convertView.setTag( new ReportListViewHolder( reportNameTextView, isCheckedCheckBox ) );
//setting check box on click listener
isCheckedCheckBox.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
ReportListItem item = (ReportListItem) cb.getTag();
item.toggleChecked();
cb.setChecked( item.isChecked() );
}
});
} else {
//getting views from convertViewTag
ReportListViewHolder viewHolder = (ReportListViewHolder) convertView.getTag();
reportNameTextView = viewHolder.getReportNameTextView();
isCheckedCheckBox = viewHolder.getIsCheckedCheckBox();
}
//setting tag for check box and use this tag in on click listener
isCheckedCheckBox.setTag( listItem );
//initializing row views
isCheckedCheckBox.setChecked( listItem.isChecked() );
reportNameTextView.setText( listItem.getReportName() );
return convertView;
}