I'm working on a tasks app. I am wanting to save data like task name, time, and priority in Room. I have created the required classes as well. However, whenever I try to delete any particular item from the tasks, the app crashes. I am fairly new at using Room, so please pardon me if I made a silly mistake. Please do tell if you require code from the Room's database class or dao class... This is the stack trace:

```
java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
        at androidx.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:385)
        at androidx.room.SharedSQLiteStatement.assertNotMainThread(SharedSQLiteStatement.java:60)
        at androidx.room.SharedSQLiteStatement.acquire(SharedSQLiteStatement.java:86)
        at com.example.taskmasterv3.UserDao_Impl.deleteAll(UserDao_Impl.java:120)
        at com.example.taskmasterv3.SubtaskAdapter$1.onClick(SubtaskAdapter.java:85)
        at android.view.View.performClick(View.java:7520)
        at android.view.View.performClickInternal(View.java:7489)
        at android.view.View.access$3600(View.java:826)
        at android.view.View$PerformClick.run(View.java:28555)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:245)
        at android.app.ActivityThread.main(ActivityThread.java:8004)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)

```

This is the code where the deletion happens :

```
// full subtask adapter code

package com.example.taskmasterv3;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;

public class SubtaskAdapter extends ArrayAdapter<subtask> {

    private final Context context;
    private ArrayList<subtask> values;

    public SubtaskAdapter(Context context, ArrayList<subtask> list) {

        //since your are using custom view,pass zero and inflate the custom view by overriding getview

        super(context, 0 , list);
        this.context = context;
        this.values = list;

    }

    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        //check if its null, if so inflate it, else simply reuse it
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.subtask_item, parent, false);
        }

        //use convertView to refer the childviews to populate it with data
        TextView tvSubtaskName = convertView.findViewById(R.id.tvsumtaskname);
        ImageView ivPri = convertView.findViewById(R.id.ivsumPri);
        ImageView ivTime = convertView.findViewById(R.id.ivsumTime);
        ImageView ivDelete = convertView.findViewById(R.id.ivDelete);

        String subname = tvSubtaskName.getText().toString().trim();

        ivDelete.setTag(position);

// This is the part that is giving me the error :

        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            int position = (Integer) v.getTag();
            remove(values.remove(position));
            AppDatabase.getDatabase(context).userDao().deleteAll();
            notifyDataSetChanged();

            }
        });

        tvSubtaskName.setText(values.get(position).getSubtaskName());

        if (values.get(position).isPriHigh()) {
            ivPri.setImageResource(R.drawable.priority_high);
        } else if (values.get(position).isPriMed()) {
            ivPri.setImageResource(R.drawable.priority_med);
        } else if (values.get(position).isPriLow()) {
            ivPri.setImageResource(R.drawable.priority_low);
        }

        if (values.get(position).isTimeMore()) {
            ivTime.setImageResource(R.drawable.time_symbol_more);
        } else if (values.get(position).isTimeMed()) {
            ivTime.setImageResource(R.drawable.time_symbol_med);
        } else if (values.get(position).isTimeLess()) {
            ivTime.setImageResource(R.drawable.time_symbol_less);
        }

        //return the view you inflated
        return convertView;
    }

    //to keep adding the new subtasks try the following
    public void addANewSubTask(subtask newSubTask){
        ArrayList<subtask> newvalues = new ArrayList<>(this.values);
        newvalues.add(newSubTask);
        this.values = newvalues;
        notifyDataSetChanged();

    }

    @Override
    public int getCount() {
        if (this.values.size()>11)
        {
            Toast.makeText(context, "Max subtask limit reached", Toast.LENGTH_SHORT).show();
            return 10;

        }
        else
        {
            return super.getCount();
        }
    }

}

```

This is the code where I try to save the data in room :

```
package com.example.taskmasterv3;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Dao;
import androidx.room.Database;
import androidx.room.Delete;
import androidx.room.Entity;
import androidx.room.Insert;
import androidx.room.PrimaryKey;
import androidx.room.Query;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import android.content.Intent;
import android.database.SQLException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class SubtaskActivity extends AppCompatActivity {

    EditText etSubtaskName;
    EditText etTaskName;
    Button btnDone, btnCancel;
    RadioGroup radgrpPri, radgrpTime;
    RadioButton radbtnPriHigh, radbtnPriMed, radbtnPriLow, radbtnTimeMore, radbtnTimeMed, radbtnTimeLess;
    boolean priHigh, priMed, priLow, timeMore, timeMed, timeLess;
    String subtaskName;
    String pri;
    String time;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subtask);

        btnDone = findViewById(R.id.btnDone);
        radgrpPri = findViewById(R.id.radgrpPri);
        radgrpTime = findViewById(R.id.radgrpTime);
        radbtnPriHigh = findViewById(R.id.radbtnPriHigh);
        radbtnPriMed = findViewById(R.id.radbtnPriMed);
        radbtnPriLow = findViewById(R.id.radbtnPriLow);
        radbtnTimeMore = findViewById(R.id.radbtnTimeMore);
        radbtnTimeMed = findViewById(R.id.radbtnTimeMed);
        radbtnTimeLess = findViewById(R.id.radbtnTimeLess);
        etSubtaskName = findViewById(R.id.etSubtaskName);
        btnCancel = findViewById(R.id.btnCancel);

        radgrpPri.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (radbtnPriHigh.isChecked())
                {
                    priHigh = true;
                    priLow = false;
                    priMed = false;
                    pri = "h";

                }
                else if (radbtnPriMed.isChecked())
                {
                    priHigh = false;
                    priLow = false;
                    priMed = true;
                    pri = "m";

                }
                else if (radbtnPriLow.isChecked())
                {
                    priHigh = false;
                    priLow = true;
                    priMed = false;
                    pri = "l";
                }
                else {
                    priLow = true;
                    pri = "l";

                }

            }
        });

        radgrpTime.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (radbtnTimeMore.isChecked())
                {
                    timeMore = true;
                    timeMed = false;
                    timeLess = false;

                    time = "more";
                }
               else if (radbtnTimeMed.isChecked())
                {
                    timeMore = false;
                    timeMed = true;
                    timeLess = false;
                    time = "med";
                }
               else if (radbtnTimeLess.isChecked())
                {
                    timeMore = false;
                    timeMed = false;
                    timeLess = true;
                    time = "less";
                }
               else
                {
                    timeLess = true;
                    time = "less";
                }

            }
        });

        btnDone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               String name = etSubtaskName.getText().toString().trim();

               Intent intent = new Intent(SubtaskActivity.this, TaskInfo.class);
               intent.putExtra("subtaskName", name);
               intent.putExtra("priHigh", priHigh);
               intent.putExtra("priMed", priMed);
               intent.putExtra("priLow", priLow);
               intent.putExtra("timeMore", timeMore);
               intent.putExtra("timeMed", timeMed);
               intent.putExtra("timeLess", timeLess);
               setResult(RESULT_OK, intent);
               SubtaskActivity.this.finish();

               // THIS IS WHERE I SAVE THE DATA IN ROOM

                AsyncTask.execute(new Runnable() {
                    @Override
                    public void run() {
                        SubtaskDetails subtaskDetails = new SubtaskDetails(etSubtaskName.toString().trim(), pri, time);
                        AppDatabase.getDatabase(getApplicationContext()).userDao().insertAll(subtaskDetails);
                    }
                });

            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                etSubtaskName.setText("");
                radgrpPri.clearCheck();
                radgrpTime.clearCheck();
                finish();

            }
        });

    }

}

```