This is my database helper class:

```
public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "note_db" ;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        sqLiteDatabase.execSQL(Note.CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + Note.TABLE_NAME);

        onCreate(sqLiteDatabase);
    }

    public Long insertnote(String note){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(Note.COL_NOTE,note);
        Long result = db.insert(Note.TABLE_NAME,null,values);
        db.close();

        return result;

    }

    public List<Note> getAllNotes() {
        List<Note> notes = new ArrayList<>();

        // Select All Query
        String selectQuery = "SELECT  * FROM " + Note.TABLE_NAME ;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Note note = new Note();
                note.setId(cursor.getInt(cursor.getColumnIndex(Note.COL_ID)));
                note.setNote(cursor.getString(cursor.getColumnIndex(Note.COL_NOTE)));

                notes.add(note);
            } while (cursor.moveToNext());
        }

        // close db connection
        db.close();

        // return notes list
        return notes;
    }

    public int updateNote(Note note) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(Note.COL_NOTE, note.getNote());

        // updating row
        return db.update(Note.TABLE_NAME, values, Note.COL_ID + " = ?",
                new String[]{String.valueOf(note.getId())});
    }

    public void deleteNote(Note note) {
        SQLiteDatabase db = this.getWritableDatabase();
//        db.execSQL("DELETE FROM " + Note.TABLE_NAME + "WHERE " + Note.COL_ID +" = " + note.getId() +"" );

        db.delete(Note.TABLE_NAME, Note.COL_ID + " = ?",new String[]{String.valueOf(note.getId())});
        db.close();
    }
}

```

this is my Main Activity

i have also make adapter in main activity
in the adapter i have used onlongclick listener.
im not much aware about it.
and when i click on alert dialog delete button it does nothing.
help me to get rid of this.

```
public class MainActivity extends AppCompatActivity {

    RecyclerView rv;
    RecyclerAdapter adapter;
    FloatingActionButton btnadd;
    DatabaseHelper db;
    List<Note> notesList = new ArrayList<>();

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

        db = new DatabaseHelper(this);
        notesList.addAll(db.getAllNotes());

        rv = findViewById(R.id.rv);
        btnadd = findViewById(R.id.btnadd);

        adapter = new RecyclerAdapter(this, notesList);
        adapter.notifyDataSetChanged();
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(mLayoutManager);
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.setAdapter(adapter);
        btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                addnote();

            }
        });

    }

    public void addnote() {

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        View v = getLayoutInflater().inflate(R.layout.add_note,null);

        builder.setView(v);

        final AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        final EditText addnote = v.findViewById(R.id.edtnote);
        final Button addbtn = v.findViewById(R.id.addbtn);
        final Button cancel = v.findViewById(R.id.cancel);

        addbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (addnote.getText().toString().isEmpty()==false){

                    db.insertnote(addnote.getText().toString());

                    dialog.dismiss();
                    Toast.makeText(getApplicationContext(),"Note Added",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(getApplicationContext(),"please add note",Toast.LENGTH_SHORT).show();
                }

            }
        });

       cancel.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               dialog.cancel();
           }
       });

    }

    private void deleteNote(int position) {
        // deleting the note from db
        db.deleteNote(notesList.get(position));

        // removing the note from the list
        notesList.remove(position);
    }

}

```

this is recycler adapter class
it is also placed in main activity

```
class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{

    Context context;
    public List<Note> noteList;
//    DatabaseHelper db = new DatabaseHelper(context);

    public RecyclerAdapter(Context context,List<Note> noteList){
        this.context = context;
        this.noteList = noteList;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView notes;

        public ViewHolder(final View itemView){
            super(itemView);

            notes = itemView.findViewById(R.id.note);

        }

    }

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup viewGroup, final int i) {

        final Context context = viewGroup.getContext();

        View view = LayoutInflater.from(context)
                .inflate(R.layout.show_note,viewGroup,false);

        final ViewHolder holder = new ViewHolder(view);

        final DatabaseHelper db  = new DatabaseHelper(context);

        holder.notes.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(final View view) {

                AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                View v = LayoutInflater.from(view.getContext()).inflate(R.layout.update_delete_note, null);

                final Button update, delete;

                update = v.findViewById(R.id.update);
                delete = v.findViewById(R.id.delete);

                builder.setView(v);

                final AlertDialog dialog = builder.create();
                dialog.show();

                update.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                });

```

i get problem over here.
this code does nothing
and also i want to refresh the recyclerview in real time.

please show me how to do that too.

it is like i insert note and after that when i reopen app then it show note in recycler view .
i want to do that in realtime when i add it should suddenly show me added note.

```
                delete.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        Log.e(String.valueOf(context),noteList.get(i).toString());

                        Note note = noteList.get(i);
                        db.deleteNote(note);
                    }
                });

                return true;
            }
        });

        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int i) {

        final Note note = noteList.get(i);
        holder.notes.setText(note.getNote());

    }

    @Override
    public int getItemCount() {
        return noteList.size();
    }

}

```