-1

i have custom list adapter with checkboxes i want to remove list items on checked items . when removing item of listview after coming to this fragment deleted item coming back to the listview. and next problem when checked item( this problem for clicking in body list view not not clicking on checkbox) for deleting all item checked not together deleting most clicking More than once deleting buttons. any person help me i enjoyed.
my fragment

        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

    listView.setOnItemClickListener( new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            chk = (CheckBox) view.findViewById(R.id.checkBox6);
            chk.toggle();               

        }
    });
                    Button btn = (Button) view.findViewById(R.id.button1);      
    btn.setOnClickListener( new OnClickListener() {

        @Override
        public void onClick(View v) {
            for (int i = 0; i < list.size()/*this is the original model*/; i++) {

               if(list.get(i).isSelected()==true){  
                   adapter.remove(list.get(i));
                }                  
            }
            adapter.notifyDataSetChanged(); 

        }
    })

listadapter

public class ListAdapter extends BaseAdapter {
private final String TAG = "*** ListAdapter ***";
Context context;
 LayoutInflater myInflater;
 List<contac> list;
    CheckBox checkBox6;
public ListAdapter(Context context) {
    super();
    myInflater = LayoutInflater.from(context);
    this.context = context;
    Log.i(TAG, "Adapter setuped successfully.");
}
public void remove(contac object) {
    list.remove(object);
    notifyDataSetChanged();
}   
public void setData(List<contac> list) {
    this.list = list;

    Log.i(TAG, "Data passed to the adapter.");
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;        

    if (convertView == null) {
        convertView     = myInflater.inflate(R.layout.list_adapter, null);
        holder          = new ViewHolder();
    //  holder.tvId     = (TextView) convertView.findViewById(R.id.tvId);
        holder.tvName   = (TextView) convertView.findViewById(R.id.tvName);
        holder.tvPhone  = (TextView) convertView.findViewById(R.id.tvPhone);
        holder.tvnum    = (TextView) convertView.findViewById(R.id.tvnum);
        holder.tvtype   = (TextView) convertView.findViewById(R.id.tvtype);
        holder.tvcode   = (TextView) convertView.findViewById(R.id.tvcode);
        holder.tvesme   = (TextView) convertView.findViewById(R.id.tvesme);
        holder.checkBox6    =  (CheckBox)convertView.findViewById(R.id.checkBox6);  
        holder.checkBox6
          .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
              contac element = (contac) holder.checkBox6.getTag();
              element.setSelected(buttonView.isChecked());
            }
          });
        convertView.setTag(holder);
        holder.checkBox6.setTag(list.get(position));

    } else {
        holder = (ViewHolder) convertView.getTag();
        ((ViewHolder) convertView.getTag()).checkBox6.setTag(list.get(position));
    }
//  ViewHolder  holder = (ViewHolder) convertView.getTag();
    NumberFormat format = NumberFormat.getCurrencyInstance();
//  holder.tvId.setTag(list.get(position).getIdInString());
//  holder.tvId.setText(list.get(position).getIdInString());
    holder.tvName.setText(format.format(list.get(position).getName()));
    holder.tvPhone.setText(format.format(list.get(position).getPhoneNumber()));
    holder.tvnum.setText(list.get(position).getNUM());  
    holder.tvtype.setText(list.get(position).getTYPE());
    holder.tvcode.setText(list.get(position).getCODE());
    holder.tvesme.setText(list.get(position).getESME());
    holder.checkBox6.setChecked(list.get(position).isSelected());       
    return convertView;
}
static class ViewHolder {
//  TextView tvId;
    TextView tvName;
    TextView tvPhone;
    TextView tvnum;
    TextView tvtype;
    TextView tvcode;
    TextView tvesme;
    CheckBox checkBox6;
}
public List<contac> getcontac() {
    return list;
}   
public void remove(Object position) {
    list.remove(position);  
}
      }

contact

public class contac {
private long id;
private int name;
private int phoneNumber;
private String num;
private String code;
private String esme;
private String type;
      private boolean selected;
public  contac (long id, int name, int phoneNumber,
        String num, String code ,String esme, String type) {
    this.id = id;
    this.name = name;
    this.phoneNumber = phoneNumber;
    this.code = code;
    this.esme = esme;
    this.type = type;
    selected = false;       
}
public long getId() {
    return id;
}   
public String getIdInString() {
    return Long.toString(id);
}   
public int getName() {
    return name;
}
public String getNameInString() {
    return Long.toString(name);
      }
public int getPhoneNumber() {
    return phoneNumber;
}   
public String getPhoneNumberInString() {
    return Long.toString(phoneNumber);
}       
public String getNUM() {
    return num;
}   
public String getTYPE() {
    return type;
}
public String getCODE() {
    return code;
}
public String getESME() {
    return esme;
}   
public void setId(long id) {
    this.id = id;
}       
public void setName(int name) {
    this.name = name;
}   
public void setPhoneNumber(int phoneNumber) {
    this.phoneNumber = phoneNumber;
}
public void setNUM(String num) {
    this.num = num;
}
public void setTYPE(String type) {
    this.type = type;
}
public void setCODE(String code) {
    this.code = code;
}
public void setESME(String esme) {
    this.esme = esme;
}
  public boolean isSelected() {
    return selected;
}
  public void setSelected(boolean selected) {
    this.selected = selected;
}
      }
mohsen
  • 1
  • 3

1 Answers1

0

Follow this answer for directions regarding how to update the adapter if the listview contents has been changed. According to that answer update your adapter as

private class CustomListAdapter extends ArrayAdapter<CustomModel> {

    // you adapter code here...

    public void updateList(List<CustomModel> list) {

        this.list = list;
        notifyDataSetChanged();

    }

and call it from the activity as yourAdapterInstance.updateList(newListContent)

Community
  • 1
  • 1
NullPointerException
  • 3,978
  • 4
  • 34
  • 52