0

I want to insert value 1 in the SQLite column when switch compact is clicked in a card view inside recyclerView ..but when I clicked switchCompact getting NullPointeException. enter image description here Thanks in advance .. I am new into Android

orderListAdapter.java

    public void onBindViewHolder(@NonNull final orderListHolder holder, final int position) {
    final orderbook orderbook = orderbookList.get(position);
    holder.orderno.setText(orderbook.orderNo + " ");
    holder.customerName.setText(orderbook.customerName);
    holder.itemName.setText(orderbook.itemName);
    holder.workCompleteSwicth.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(holder.workCompleteSwicth.isChecked()) {
                myDb.InsertWorkComplete(1);
            }


        }
    });

DatabaseHelper.java

public void InsertWorkComplete( int isWorkComplete)
{
    SQLiteDatabase db=this.getWritableDatabase();
   ContentValues contentValues=new ContentValues();


 contentValues.put(COLUMN_ISWORKCOMPLETE,isWorkComplete);

     db.update(TABLE_NAME, contentValues, null, null);



}

logcat :-

Process: com.example.android.saffrondesigner, PID: 5359
    java.lang.NullPointerException
        at com.example.android.saffrondesigner.orderListAdapter$1.onCheckedChanged(orderListAdapter.java:60)
        at android.widget.CompoundButton.setChecked(CompoundButton.java:127)
        at androidx.appcompat.widget.SwitchCompat.setChecked(SwitchCompat.java:1060)
        at androidx.appcompat.widget.SwitchCompat.toggle(SwitchCompat.java:1055)
        at android.widget.CompoundButton.performClick(CompoundButton.java:99)
        at android.view.View$PerformClick.run(View.java:18797)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5299)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
        at dalvik.system.NativeStart.main(Native Method)

2 Answers2

0

in here

public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(holder.workCompleteSwicth.isChecked()) {
                myDb.InsertWorkComplete(1);
            }

i think just change holder.workCompleteSwicth.isChecked() to

`public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b) {
                    myDb.InsertWorkComplete(1);
                    //u can setChecked of switch her ex: model.setChecked(true);
                }`

if it doesn't work can you post error in your logcat

Ramadanrizky87
  • 218
  • 2
  • 11
  • first of all thanks for your response, sorry to say your suggestion is not working. I will post my logcat, hope you help me to find the solution – Ziyad Rahman Nov 06 '19 at 16:15
0

First of all don't do database related work on adapter,i will provide you solucation in which you can do database related work on Activity on Fragment. First create one Interface in your Adapter,

interface WorkCompleteInteraction {
        void onWorkComplete(int position, OrderBook orderbook);
    }

get your interface in your constructor,

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private WorkCompleteInteraction interaction;
    private Context mContext;

    public MyAdapter(WorkCompleteInteraction interaction, Context mContext) {
        this.interaction = interaction;
        this.mContext = mContext;
    }

Send callback to your activity/fragment,

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        final orderbook orderbook = orderbookList.get(position);
        if (interaction != null) {
            interaction.onWorkComplete(position, orderbook);
        }
    }

Initialise your adapter in activity/fragment,

MyAdapter adapter = new MyAdapter(this,this);

Implement your interface in your activity,

public class TestActivity extends AppCompatActivity implements MyAdapter.WorkCompleteInteraction {

Finally override your method,

    @Override
    public void onWorkComplete(int position, OrderBook orderbook) {
        //do your database related work here
    }
Kintan Patel
  • 1,230
  • 8
  • 15