I have a Listview
in my row, I have check box to select a item to delete.
I want to have a button to select all my items and delete them at same time.
When I click my button to select all the items, only the last item gets selected.
Here is my code:
all.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
for(int i=0;i<listview.getChildCount();i++){
Log.i("mhs",listview.getChildCount()+"");
checkBox.setChecked(true);
}
}
});
Here is my Adaptor
:
public class CustomArrayAdapter extends ArrayAdapter<String> {
private LayoutInflater inflater;
// This would hold the database objects i.e. Blacklist
private List<Blacklist> records;
@SuppressWarnings("unchecked")
public CustomArrayAdapter(Context context, int resource, @SuppressWarnings("rawtypes") List objects) {
super(context, resource, objects);
this.records = objects;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//Reuse the view to make the scroll effect smooth
if (convertView == null)
convertView = inflater.inflate(R.layout.list_item, parent, false);
// Fetch phone number from the database object
final Blacklist phoneNumber = records.get(position);
// Set to screen component to display results
((TextView) convertView.findViewById(R.id.serial_tv)).setText("" + (position + 1));
((TextView) convertView.findViewById(R.id.phone_number_tv)).setText(phoneNumber.phoneNumber);
checkBox = (CheckBox) convertView.findViewById(R.id.check);
final LinearLayout me = (LinearLayout) convertView.findViewById(R.id.me);
if (position + 1 >= 1) {
me.setVisibility(View.VISIBLE);
}
checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
// Set the appropriate message into it.
alertDialogBuilder.setMessage(getResources().getString(R.string.block));
// Add a positive button and it's action. In our case action would be deletion of the data
alertDialogBuilder.setPositiveButton(getResources().getString(R.string.yes),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
try {
blackListDao.delete(blockList.get(position));
// Removing the same from the List to remove from display as well
blockList.remove(position);
listview.invalidateViews();
// Reset the value of selectedRecordPosition
selectedRecordPosition = -1;
populateNoRecordMsg();
} catch (Exception e) {
e.printStackTrace();
}
}
});
// Add a negative button and it's action. In our case, just hide the dialog box
alertDialogBuilder.setNegativeButton(getResources().getString(R.string.no),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
checkBox.setChecked(false);
}
});
// Now, create the Dialog and show it.
final AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});
all.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
for(int i=0;i<listview.getChildCount();i++){
Log.i("mhs",listview.getChildCount()+"");
checkBox.setChecked(true);
listview.removeView(i);
}
}
});
return convertView;
}
}
How to select all the items together?