下面的适配器代码由多维数组支持。代码位于具有父Activity的ListFragment中。
当选中一个复选框时,我会调用一个监听器,该监听器将信息发送给父Activity。
这一切都很好。我的问题是,如何创建一个按钮来选择/取消选择列表中的所有复选框,并将每行中的信息发送给将将信息发送给父活动的监听器?
我找到了以下代码来选择所有已选中的复选框,但这并未考虑需要转到父活动的每一行中的数据。
final ListView list = getListView();
for ( int i=0; i< getListAdapter().getCount(); i++ ) {
list.setItemChecked(i, true);
}
private class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<?> list;
boolean[] checkBoxState;
ViewHolder viewHolder;
public MySimpleArrayAdapter(Context context, ArrayList<?> list) {
super(context, R.layout.rotarowlayout);
Log.e(TAG, "inside adapter constructor");
this.context = context;
this.list = list;
checkBoxState = new boolean[this.list.size()];
//Log.e(TAG, "list has size of " + this.list.size());
}
private class ViewHolder
{
TextView startTime;
TextView duration;
TextView status;
TextView name;
TextView actualIn;
TextView actualOut;
TextView carerName;
TextView statusBox;
ImageView noteStatus;
ImageView doubleupStatus;
ImageView timeCritical;
CheckBox checkBox;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null)
{
convertView = inflater.inflate(R.layout.rotarowlayout, null);
viewHolder = new ViewHolder();
viewHolder.startTime = (TextView) convertView.findViewById(R.id.rowstarttime);
viewHolder.duration = (TextView) convertView.findViewById(R.id.rowduration);
viewHolder.status = (TextView) convertView.findViewById(R.id.rowstatus);
viewHolder.name = (TextView) convertView.findViewById(R.id.rowclientname);
viewHolder.noteStatus = (ImageView) convertView.findViewById(R.id.notestatus);
viewHolder.doubleupStatus = (ImageView) convertView.findViewById(R.id.doubleupstatus);
viewHolder.timeCritical = (ImageView) convertView.findViewById(R.id.timecritical);
viewHolder.actualIn = (TextView) convertView.findViewById(R.id.rowclientactualin);
viewHolder.actualOut = (TextView) convertView.findViewById(R.id.rowclientactualout);
viewHolder.carerName = (TextView) convertView.findViewById(R.id.rowruncarername);
viewHolder.statusBox = (TextView) convertView.findViewById(R.id.rowstatus);
viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.list_checkbox_multi_select_carer);
//link the cached views to the convertview
convertView.setTag( viewHolder);
}else
{
viewHolder = (ViewHolder) convertView.getTag();
}
String record = list.get(position).toString();
convertView.setTag(R.layout.rotarowlayout, record);
String[] itemsInRecord = record.split(",");
Log.e(TAG, "itemin record = " + itemsInRecord.length);
final String[] recordItem = new String[itemsInRecord.length];
for (int x = 0; x < itemsInRecord.length; x++) {
recordItem[x] = itemsInRecord[x];
Log.e(TAG, "recordItem[x]" + x + " " +recordItem[x] );
}
viewHolder.checkBox.setChecked(checkBoxState[position]);
viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
checkBoxState[position] = true;
mListener.onMultiSelectUpdateCallSelected("ADD", recordItem[5]);
}else{
checkBoxState[position] = false;
mListener.onMultiSelectUpdateCallSelected("REMOVE", recordItem[5]);
}
}
});
return convertView;
}
@Override
public int getCount() {
if(this.list != null){
return this.list.size();
}else{
return 0;
}}
}// end of adapter class