There is a recyclerview when you click on it, a dialog box is displayed, open, edit and delete, when you click delete, it is deleted, but when you restart the application, everything returns, I know that I need to delete the selected recyclerview from the databases, how to implement it?

 public class MyDatabase extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "_database"; private static final String GROUPS_TABLE_NAME = "groups_table"; public static final String GROUP_ID = "group_id"; public static final String GROUP_NAME = "group_name"; public static final String GROUP_NOTES = "group_notes"; public static final String GROUP_NOTES1 = "group_notes"; public static final String GROUP_NOTES2 = "group_notes"; public static final String GROUP_NOTES3 = "group_notes"; public static final String GROUP_NOTES4 = "group_notes"; public static final String GROUP_NOTES5 = "group_notes"; public static final String GROUP_NOTES6 = "group_notes"; public static final String GROUP_NOTES7 = "group_notes"; public static final String GROUP_NOTES8 = "group_notes"; private static final String CREATE_GROUPS_TABLE = "CREATE TABLE " + GROUPS_TABLE_NAME + " ( " + GROUP_ID + " INTEGER PRIMARY KEY," + GROUP_NAME + " TEXT," + GROUP_NOTES + " TEXT," + GROUP_NOTES1 + " TEXT,"+GROUP_NOTES2+ " TEXT,"+ GROUP_NOTES3 + " TEXT," +GROUP_NOTES4 + " TEXT," +GROUP_NOTES5 + " TEXT," +GROUP_NOTES6 + " TEXT," +GROUP_NOTES7 + " TEXT," +GROUP_NOTES8 + " TEXT );"; private SQLiteDatabase database; public MyDatabase(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_GROUPS_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + GROUPS_TABLE_NAME); onCreate(db); } public void setGroupData(String groupName, String groupNotes,String A1, String A2,String A3, String A4,String A5, String A6,String A7, String A8) { database = getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put(GROUP_NAME, groupName); cv.put(GROUP_NOTES, groupNotes); cv.put(GROUP_NOTES1, A1); cv.put(GROUP_NOTES2, A2); cv.put(GROUP_NOTES3, A3); cv.put(GROUP_NOTES4, A4); cv.put(GROUP_NOTES5, A5); cv.put(GROUP_NOTES6, A6); cv.put(GROUP_NOTES7, A7); cv.put(GROUP_NOTES8, A8); database.insert(GROUPS_TABLE_NAME, null, cv); } public Cursor getGroupData() { database = getReadableDatabase(); String[] columns = {GROUP_ID, GROUP_NAME, GROUP_NOTES,GROUP_NOTES1,GROUP_NOTES2,GROUP_NOTES3,GROUP_NOTES4,GROUP_NOTES5,GROUP_NOTES6,GROUP_NOTES7,GROUP_NOTES8}; String[] columns1 = {GROUP_NOTES1,GROUP_NOTES2,GROUP_NOTES3,GROUP_NOTES4,GROUP_NOTES5,GROUP_NOTES6,GROUP_NOTES7,GROUP_NOTES8}; Cursor c = database.query(GROUPS_TABLE_NAME, columns ,null, null, null, null, GROUP_ID + " DESC"); return c; } public Cursor getGroupName(String[] args) { database = getReadableDatabase(); String query = "SELECT " + GROUP_NAME + " FROM " + GROUPS_TABLE_NAME + " WHERE " + GROUP_NAME + " =?"; Cursor c = database.rawQuery(query, args); return c; } } public class MainRecyclerViewAdapter extends RecyclerView.Adapter<MainRecyclerViewAdapter.MyMainViewHolder> { static Context context; int groupNameTextView, groupNotesTextView,a1,a2,a3,a4,a5,a6,a7,a8; private List<Information> mainInfo; MyDatabase myDatabase; private Boolean check=false; public MainRecyclerViewAdapter(List<Information> mainInfo) { this.mainInfo = mainInfo; } public class MyMainViewHolder extends RecyclerView.ViewHolder { TextView groupNameTextView, groupNotesTextView,a1,a2,a3,a4,a5,a6,a7,a8; ImageView groupImgFull; LinearLayout linearLayout; public MyMainViewHolder(View itemView) { super(itemView); linearLayout = (LinearLayout) itemView.findViewById(R.id.mainLayout); groupNameTextView = (TextView) itemView.findViewById(R.id.group_name_TextView); groupNotesTextView = (TextView) itemView.findViewById(R.id.group_notes_TextView); a1 = (TextView) itemView.findViewById(R.id.a1); a2 = (TextView) itemView.findViewById(R.id.a2); a3 = (TextView) itemView.findViewById(R.id.a3); a4 = (TextView) itemView.findViewById(R.id.a4); a5 = (TextView) itemView.findViewById(R.id.a5); a6 = (TextView) itemView.findViewById(R.id.a6); a7 = (TextView) itemView.findViewById(R.id.a7); a8 = (TextView) itemView.findViewById(R.id.a8); groupImgFull = (ImageView) itemView.findViewById(R.id.group_Img_Full); } } @Override public MainRecyclerViewAdapter.MyMainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.main_single_raw, parent, false); final MainRecyclerViewAdapter.MyMainViewHolder myViewHolder = new MainRecyclerViewAdapter.MyMainViewHolder (view); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(!check) { } else { Toast.makeText(v.getContext(), "!", Toast.LENGTH_SHORT).show(); } } }); return myViewHolder; } @Override public void onBindViewHolder(final MyMainViewHolder holder, final int position) { holder.groupNameTextView.setText(mainInfo.get(position).getGROUP_NAME()); holder.groupNotesTextView.setText(mainInfo.get(position).getGROUP_NOTES()); holder.a1.setText(mainInfo.get(position).getGROUP_NOTES1()); holder.a8.setText(mainInfo.get(position).getGROUP_NOTES8()); holder.groupImgFull.setImageResource(mainInfo.get(position).getGROUP_FULL_ICON_ID()); holder.linearLayout.setOnClickListener(new View.OnClickListener(){ public void onClick(final View v) { final String converter[] ={"Открыть", "Редактировать", "Удалить"}; View convertView = (View) LayoutInflater.from(v.getContext()).inflate(R.layout.razdel_activity_main, null,true); final ListView lv = (ListView) convertView.findViewById(R.id.lv); final ArrayAdapter<String> adapter = new ArrayAdapter<String>(v.getContext(),android.R.layout.simple_expandable_list_item_1,converter); lv.setAdapter(adapter); final AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext()); alertDialog.setView(convertView); alertDialog.setIcon(R.drawable.a); alertDialog.setCancelable(true); final AlertDialog alert = alertDialog.create(); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, final long id) { switch ((int) id) { case 0: Intent intent = new Intent(v.getContext(), DictionaryMainActivityBook.class); intent.putExtra("a1", 1); intent.putExtra("a2",String.valueOf(a2)); intent.putExtra("a3",String.valueOf(a3)); intent.putExtra("a4",String.valueOf(a4)); intent.putExtra("a5",String.valueOf(a5)); intent.putExtra("a6",String.valueOf(a6)); intent.putExtra("a7",String.valueOf(a7)); intent.putExtra("a8",String.valueOf(a8)); v.getContext().startActivity(intent); break; case 1: alert.dismiss(); break; case 2: mainInfo.remove(position); notifyItemRemoved(position); notifyItemRangeChanged(position,mainInfo.size()); alert.dismiss(); break; default: } } }); alert.show(); } }); } @Override public int getItemCount() { return mainInfo.size(); } } 
  • It is necessary to work in the adapter directly with the cursor, and not some kind of sheet. When deleting, get the ID of the record being deleted from the cursor and delete the record from the database by it, then update the adapter - pavlofff

0