Skip to content

Commit f146007

Browse files
committed
Fix delete bug
1 parent f86bad1 commit f146007

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
lines changed

app/src/main/java/com/github/meafs/recover/activites/ReminderActivity.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import androidx.appcompat.app.AppCompatActivity;
5+
56
import android.app.AlarmManager;
67
import android.app.DatePickerDialog;
78
import android.app.PendingIntent;
@@ -25,20 +26,22 @@
2526
import java.text.SimpleDateFormat;
2627
import java.util.Calendar;
2728
import java.util.Date;
29+
2830
//this class is to take the reminders from the user and inserts into the database
2931
public class ReminderActivity extends AppCompatActivity {
3032
Button mSubmitbtn, mDatebtn, mTimebtn, mPname;
3133
EditText mTitledit;
3234
String timeTonotify;
35+
3336
@Override
3437
protected void onCreate(Bundle savedInstanceState) {
3538
super.onCreate(savedInstanceState);
3639
setContentView(R.layout.activity_reminder);
37-
mTitledit = (EditText) findViewById(R.id.editTitle);
38-
mDatebtn = (Button) findViewById(R.id.btnDate); //assigned all the material reference to get and set data
39-
mTimebtn = (Button) findViewById(R.id.btnTime);
40-
mSubmitbtn = (Button) findViewById(R.id.btnSubmit);
41-
mPname = (Button) findViewById(R.id.btnpatient);
40+
mTitledit = findViewById(R.id.editTitle);
41+
mDatebtn = findViewById(R.id.btnDate); //assigned all the material reference to get and set data
42+
mTimebtn = findViewById(R.id.btnTime);
43+
mSubmitbtn = findViewById(R.id.btnSubmit);
44+
mPname = findViewById(R.id.btnpatient);
4245
mTimebtn.setOnClickListener(new View.OnClickListener() {
4346
@Override
4447
public void onClick(View view) {
@@ -70,12 +73,14 @@ public void onClick(View view) {
7073
}
7174
});
7275
}
76+
7377
private void processinsert(String title, String date, String time, String pname) {
7478
String result = new dbManager(this).addreminder(title, date, time, pname); //inserts the title,date,time into sql lite database
7579
setAlarm(title, date, time); //calls the set alarm method to set alarm
7680
mTitledit.setText("");
7781
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
7882
}
83+
7984
private void selectTime() { //this method performs the time picker task
8085
Calendar calendar = Calendar.getInstance();
8186
int hour = calendar.get(Calendar.HOUR_OF_DAY);
@@ -89,6 +94,7 @@ public void onTimeSet(TimePicker timePicker, int i, int i1) {
8994
}, hour, minute, false);
9095
timePickerDialog.show();
9196
}
97+
9298
private void selectDate() { //this method performs the date picker task
9399
Calendar calendar = Calendar.getInstance();
94100
int year = calendar.get(Calendar.YEAR);
@@ -102,6 +108,7 @@ public void onDateSet(DatePicker datePicker, int year, int month, int day) {
102108
}, year, month, day);
103109
datePickerDialog.show();
104110
}
111+
105112
public String FormatTime(int hour, int minute) { //this method converts the time into 12hr format and assigns am or pm
106113
String time;
107114
time = "";
@@ -123,6 +130,7 @@ public String FormatTime(int hour, int minute) {
123130
}
124131
return time;
125132
}
133+
126134
private void setAlarm(String text, String date, String time) {
127135
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); //assigning alarm manager object to set alarm
128136
Intent intent = new Intent(getApplicationContext(), AlarmBroadcast.class);

app/src/main/java/com/github/meafs/recover/activites/ReminderListActvity.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class ReminderListActvity extends AppCompatActivity {
2222

2323
FloatingActionButton mCreateRem;
2424
RecyclerView mRecyclerview;
25-
ArrayList<ReminderModel> dataholder = new ArrayList<ReminderModel>(); //Array list to add reminders and display in recyclerview
25+
ArrayList<ReminderModel> dataholder = new ArrayList<>(); //Array list to add reminders and display in recyclerview
2626
ReminderAdapter adapter;
2727

2828
@Override
@@ -31,9 +31,9 @@ protected void onCreate(Bundle savedInstanceState) {
3131
setContentView(R.layout.reminder_list);
3232

3333

34-
mRecyclerview = (RecyclerView) findViewById(R.id.recyclerView_reminder);
35-
mRecyclerview.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
36-
mCreateRem = (FloatingActionButton) findViewById(R.id.create_reminder); //Floating action button to change activity
34+
mRecyclerview = findViewById(R.id.recyclerView_reminder);
35+
mRecyclerview.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
36+
mCreateRem = findViewById(R.id.create_reminder); //Floating action button to change activity
3737
mCreateRem.setOnClickListener(new View.OnClickListener() {
3838
@Override
3939
public void onClick(View view) {

app/src/main/java/com/github/meafs/recover/adapters/ReminderAdapter.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.meafs.recover.adapters;
22

3+
import android.annotation.SuppressLint;
34
import android.content.Context;
45
import android.content.DialogInterface;
56
import android.view.LayoutInflater;
@@ -20,8 +21,9 @@
2021
import java.util.ArrayList;
2122

2223
public class ReminderAdapter extends RecyclerView.Adapter<ReminderAdapter.myviewholder> {
23-
ArrayList<ReminderModel> dataholder = new ArrayList<ReminderModel>(); //array list to hold the reminders
24+
ArrayList<ReminderModel> dataholder = new ArrayList<>(); //array list to hold the reminders
2425
private Context context;
26+
2527
public ReminderAdapter(ArrayList<ReminderModel> dataholder) {
2628

2729
this.dataholder = dataholder;
@@ -36,34 +38,40 @@ public myviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
3638
}
3739

3840
@Override
39-
public void onBindViewHolder(@NonNull myviewholder holder, int position) {
41+
public void onBindViewHolder(@NonNull myviewholder holder, @SuppressLint("RecyclerView") int position) {
4042
holder.mTitle.setText(dataholder.get(position).getTitle()); //Binds the single reminder objects to recycler view
4143
holder.mDate.setText(dataholder.get(position).getDate());
4244
holder.mTime.setText(dataholder.get(position).getTime());
4345
holder.mPatient.setText(dataholder.get(position).getPname());
4446
holder.reminder_rv_layout.setOnClickListener(new View.OnClickListener() {
4547
@Override
4648
public void onClick(View view) {
47-
AlertDialog.Builder adb=new AlertDialog.Builder(context);
49+
AlertDialog.Builder adb = new AlertDialog.Builder(context);
4850
adb.setTitle("Delete?");
4951
adb.setMessage("Are you sure you want to delete " + position);
5052
final int positionToRemove = position;
5153
adb.setNegativeButton("Cancel", null);
5254
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
5355
public void onClick(DialogInterface dialog, int which) {
54-
String title =(String) ((TextView) view.findViewById(R.id.txtTitle)).getText();
55-
String date =(String) ((TextView) view.findViewById(R.id.txtDate)).getText();
56-
String time =(String) ((TextView) view.findViewById(R.id.txtTime)).getText();
57-
String pname =(String) ((TextView) view.findViewById(R.id.txtPname)).getText();
56+
String title = (String) ((TextView) view.findViewById(R.id.txtTitle)).getText();
57+
String date = (String) ((TextView) view.findViewById(R.id.txtDate)).getText();
58+
String time = (String) ((TextView) view.findViewById(R.id.txtTime)).getText();
59+
String pname = (String) ((TextView) view.findViewById(R.id.txtPname)).getText();
60+
5861
String result = new dbManager(context).deleteReminder(title, date, time, pname);
5962
Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
60-
notifyDataSetChanged();
61-
}});
63+
64+
dataholder.remove(position);
65+
notifyItemRemoved(position);
66+
notifyItemRangeChanged(position, getItemCount());
67+
}
68+
});
6269
adb.show();
6370
}
6471
});
6572
}
6673

74+
6775
@Override
6876
public int getItemCount() {
6977
return dataholder.size();
@@ -77,11 +85,11 @@ class myviewholder extends RecyclerView.ViewHolder {
7785
public myviewholder(@NonNull View itemView) {
7886
super(itemView);
7987

80-
mTitle = (TextView) itemView.findViewById(R.id.txtTitle); //holds the reference of the materials to show data in recyclerview
81-
mDate = (TextView) itemView.findViewById(R.id.txtDate);
82-
mTime = (TextView) itemView.findViewById(R.id.txtTime);
83-
mPatient = (TextView) itemView.findViewById(R.id.txtPname);
84-
reminder_rv_layout = (CardView) itemView.findViewById(R.id.reminder_item_layout);
88+
mTitle = itemView.findViewById(R.id.txtTitle); //holds the reference of the materials to show data in recyclerview
89+
mDate = itemView.findViewById(R.id.txtDate);
90+
mTime = itemView.findViewById(R.id.txtTime);
91+
mPatient = itemView.findViewById(R.id.txtPname);
92+
reminder_rv_layout = itemView.findViewById(R.id.reminder_item_layout);
8593
}
8694
}
8795
}

app/src/main/java/com/github/meafs/recover/utils/dbManager.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public class dbManager extends SQLiteOpenHelper {
1515
public dbManager(@Nullable Context context) {
1616
super(context, dbname, null, 1);
1717
}
18+
1819
String TABLE_NAME = "tbl_reminder";
20+
1921
@Override
2022
public void onCreate(SQLiteDatabase sqLiteDatabase) { //sql query to insert data in sqllite
2123
String query = "create table tbl_reminder(id integer primary key autoincrement,title text,date text,time text, pname text)";
@@ -31,7 +33,7 @@ public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
3133

3234
}
3335

34-
public String addreminder(String title, String date, String time, String pname ) {
36+
public String addreminder(String title, String date, String time, String pname) {
3537
SQLiteDatabase database = this.getReadableDatabase();
3638

3739
ContentValues contentValues = new ContentValues();
@@ -49,6 +51,7 @@ public String addreminder(String title, String date, String time, String pname )
4951
}
5052

5153
}
54+
5255
// below is the method for deleting our course.
5356
public String deleteReminder(String title, String date, String time, String pname) {
5457

@@ -58,7 +61,16 @@ public String deleteReminder(String title, String date, String time, String pnam
5861

5962
// on below line we are calling a method to delete our
6063
// course and we are comparing it with our course name.
61-
float result = db.delete("tablename","title=? and date=? and time=? and pname=?",new String[]{title, date,time, pname});
64+
65+
// String whereClause = "title" + "=?" + " and date" + "=?" + " and time" + "=?" + " and pname" + "=?";
66+
67+
String whereClause = "title" + "=?";
68+
69+
String[] whereArgs = new String[]{title};
70+
71+
float result = db.delete(TABLE_NAME, whereClause, whereArgs);
72+
73+
// float result = db.delete(TABLE_NAME, "title" + "=?" + " and date" + "=?" + " and time" + "=?" + "and pname" + "=?", new String[]{title, date, time, pname});
6274
if (result == -1) {
6375
db.close();
6476
return "Failed";
@@ -67,6 +79,7 @@ public String deleteReminder(String title, String date, String time, String pnam
6779
return "Successfully deleted";
6880
}
6981
}
82+
7083
public Cursor readallreminders() {
7184
SQLiteDatabase database = this.getWritableDatabase();
7285
String query = "select * from tbl_reminder order by id desc"; //Sql query to retrieve data from the database

0 commit comments

Comments
 (0)