In listView, it contains checkbox. When delete button
is clicked, it will delete the list which the checkbox is checked. I refer to this answer.
Adapter class
public class DeleteAdapter extends BaseAdapter {
private static ArrayList<SearchList> search;
private LayoutInflater mInflater;
ListView listview;
Button delete; // button get from DeleteTask class
int checkBoxCounter = 0;
public DeleteAdapter(Context context, ArrayList<SearchList> searchList, ListView listview,Button delete) {
search=searchList;
this.delete=delete;
this.listview=listview;
mInflater = LayoutInflater.from(context);
delete.setText("DELETE");
}
public int getCount() {
return search.size();
}
public SearchList getItem(int position) {
return search.get(position);
}
public long getItemId(int position) {
return 0;
}
public void removeItem(int position) {
search.remove(position);
this.notifyDataSetChanged();
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.delete_task_with_edittext, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.task_title);
holder.time = (TextView) convertView.findViewById(R.id.time);
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.ckbox = (CheckBox) convertView.findViewById(R.id.checkBox);
holder.ckbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
int getPosition = (Integer) v.getTag(); // Here we get the position that we have set for the checkbox using setTag.
search.get(getPosition).setSelected(((CheckBox)v).isChecked()); // Set the value of checkbox to maintain its state.
checkBoxCounter ++;
delete.setText("DELETE"+""+"("+ checkBoxCounter +")");
} else
{
int getPosition = (Integer) v.getTag(); // Here we get the position that we have set for the checkbox using setTag.
search.get(getPosition).setSelected(((CheckBox) v).isChecked()); // Set the value of checkbox to maintain its state.
checkBoxCounter--;
if (checkBoxCounter == 0) {
delete.setText("DELETE");
}
else {
delete.setText("DELETE" + "" + "(" + checkBoxCounter + ")");
}
}
}
});
convertView.setTag(holder);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SparseBooleanArray checkedItemPositions = listview.getCheckedItemPositions();
int itemCount = listview.getCount();
for(int i=0; i <itemCount; i++){
if(checkedItemPositions.get(i)){
removeItem(i);
--i;
}
}
checkedItemPositions.clear();
//DeleteAdapter.notifyDataSetChanged();
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ckbox.setTag(position);
holder.text.setText(search.get(position).getTitle());
holder.time.setText(search.get(position).getTime());
holder.date.setText(search.get(position).getDate());
holder.ckbox.setChecked(search.get(position).isSelected());
return convertView;
}
static class ViewHolder {
TextView text,time,date;
CheckBox ckbox;
}
}
When I click the delete
button, I get nullpointerexception
error.
java.lang.NullPointerException
at com.example.seng.healthyapp.adapter.DeleteAdapter$2.onClick(DeleteAdapter.java:107)
at android.view.View.performClick(View.java:4230)
at android.view.View$PerformClick.run(View.java:17660)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5433)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:924)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
at dalvik.system.NativeStart.main(Native Method)
Refer to
if(checkedItemPositions.get(i)){
Aucun commentaire:
Enregistrer un commentaire