vendredi 26 mai 2017

how to work with Checkbox in Custom Adapter

I am working with checkbox which is in my custom Adapter.I used the Interface in Activity to send that checkbox text in another listview the problem i am facing that when i add the item to checkbox and checked the checkbox the application get stop working..but when i reopen the app and try to checked its again its workfine.when i try to add one new item to checkbox same issue happen and vice-versa

Logcat error:

  java.lang.NullPointerException: Attempt to invoke interface method 'void com.advoco.notepad.ListAdapter$onItemChecked.onItemClick(int)' on a null object reference
                                                                  at com.advoco.notepad.ListAdapter$1.onCheckedChanged(ListAdapter.java:51)
                                                                  at android.widget.CompoundButton.setChecked(CompoundButton.java:156)
                                                                  at android.widget.CompoundButton.toggle(CompoundButton.java:115)
                                                                  at android.widget.CompoundButton.performClick(CompoundButton.java:120)
                                                                  at android.view.View$PerformClick.run(View.java:22429)
                                                                  at android.os.Handler.handleCallback(Handler.java:751)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)


public class ListAdapter extends ArrayAdapter<NoteDone> {
Context context;
LayoutInflater inflater;
List<NoteDone> noteDones;
private onItemChecked onClick;
public ListAdapter(Context context,int resource,List<NoteDone> noteDones) {
    super(context, resource,noteDones);
    this.context = context;
    this.noteDones = noteDones;
    inflater = LayoutInflater.from(context);
}
private class ViewHolder {
    TextView notDone;
    CheckBox checkBox;
}
public interface onItemChecked{
    void onItemClick(int position);
}
public View getView(final int position, View view, ViewGroup parent) {
    final ListAdapter.ViewHolder holder;
    final NoteDone model = noteDones.get(position);
    if (view == null) {
        holder = new ListAdapter.ViewHolder();
        view = inflater.inflate(R.layout.notdone, null);
        holder.checkBox = (CheckBox) view.findViewById(R.id.checkBox);
        view.setTag(holder);
    } else {
        holder = (ListAdapter.ViewHolder) view.getTag();
    }
    holder.checkBox.setText(model.getTask());
    holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            onClick.onItemClick(position);
        }
    });
    return view;
}
public void setOnClick(onItemChecked onClick)
{
    this.onClick=onClick;
}
}

My activity method:

@Override
public void onItemClick(int position) {
    String name = taskList.get(position).getTask();
    dBhelper.insertNewTask2(name);
    dBhelper.deleteTask(name);
    LoadTask();
    LoadTask2();
}




Aucun commentaire:

Enregistrer un commentaire