I have the adapter code below which is backed by a multidimensional array. The code is inside a ListFragment which has a parent Activity.
when a checkbox is selected, i call a listener which sends information to the parent Activity.
All this works fine. My problem is, how can i make a button select/deselect all the checkboxes in the list and send the information in each row to the listener that will send the info to the parent Activity?
I have found the following code that selects all the checkboxed but this doesn't take into account the data in each row that needs to go to the parent Activity.
Thanks Matt.
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
Aucun commentaire:
Enregistrer un commentaire