前提・実現したいこと
Android Studioにて、ルーティン管理アプリを作成しています。データベースとしてRealmを使用しており、RoutineEditActivityで新しいルーティンの追加・編集・削除を行って、MainActivityに一覧表示しています。(ここまでは確認済み)
RoutineEditAcitivityにおいて、ユーザーがルーティンを行った場合はCheckBoxにチェックを入れます。日付が変わるごとにそのデータが保存され、5日分の記録を得点化したものを表示したいと思っています。
アプリ制作初心者であり、見苦しいコードであると思いますがご回答よろしくお願いします。
発生している問題・エラーメッセージ
日付が変わる際にRealmのデータを更新して得点の表示を変更したいのですが、エミュレータ上で変更作業が行われません。(アプリを実行することはできます。)
該当のソースコード
java
1RoutineEditActivity.java 2 3package com.example.makeyourroutine; 4 5import androidx.appcompat.app.AppCompatActivity; 6 7import android.app.AlarmManager; 8import android.app.PendingIntent; 9import android.content.Intent; 10import android.graphics.Color; 11import android.os.Bundle; 12import android.view.View; 13import android.widget.Button; 14import android.widget.CheckBox; 15import android.widget.EditText; 16import android.widget.TextView; 17 18import com.google.android.material.snackbar.Snackbar; 19 20import java.util.Calendar; 21 22import io.realm.Realm; 23import io.realm.RealmResults; 24 25public class RoutineEditActivity extends AppCompatActivity { 26 private Realm realm; 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_routine_edit); 32 realm = Realm.getDefaultInstance(); 33 34 TextView textview = findViewById(R.id.textView4); 35 Button save = findViewById(R.id.save); 36 Button delete = findViewById(R.id.delete); 37 final CheckBox checkBox = findViewById(R.id.checkBox); 38 final EditText titleEdit = findViewById(R.id.titleEdit); 39 final EditText targetEdit = findViewById(R.id.targetEdit); 40 41 42 final long routineId = getIntent().getLongExtra("routine_id",-1); 43 if(routineId!=-1){ 44 Routine routine =realm.where(Routine.class).equalTo("id",routineId).findFirst(); 45 titleEdit.setText(routine.title); 46 targetEdit.setText(routine.target); 47 checkBox.setChecked(routine.now); 48 delete.setVisibility(View.VISIBLE); 49 } 50 else{ 51 delete.setVisibility(View.INVISIBLE); 52 } 53 54 boolean onreceive = getIntent().getBooleanExtra("onReceive",false); 55 if(onreceive){ 56 Routine routine =realm.where(Routine.class).equalTo("id",routineId).findFirst(); 57 routine.b5=routine.b4; 58 routine.b4=routine.b3; 59 routine.b3=routine.b2; 60 routine.b2=routine.b1; 61 if(checkBox.isChecked()==true){ 62 checkBox.setChecked(false); 63 routine.b1=20; 64 } 65 else{ 66 routine.b1=0; 67 } 68 int point = routine.b1+routine.b2+routine.b3+routine.b4+routine.b5; 69 textview.setText(Integer.toString(point)+"点"); 70 } 71 72 save.setOnClickListener(new View.OnClickListener(){ 73 @Override 74 public void onClick(final View view) { 75 76 if (routineId == -1) { 77 realm.executeTransaction(new Realm.Transaction() { 78 @Override 79 public void execute(Realm db) { 80 Number maxId = db.where(Routine.class).max("id"); 81 long nextId = (maxId == null) ? 1 : maxId.longValue() + 1; 82 Routine routine = db.createObject(Routine.class, nextId); 83 String str1 = titleEdit.getText().toString(); 84 routine.title = str1; 85 String str2 = targetEdit.getText().toString(); 86 routine.target = str2; 87 routine.now=checkBox.isChecked(); 88 } 89 }); 90 91 Calendar cal = Calendar.getInstance(); 92 cal.set(Calendar.HOUR_OF_DAY,0); 93 cal.set(Calendar.MINUTE,0); 94 cal.set(Calendar.SECOND,0); 95 Intent intent =new Intent(getApplicationContext(),AlarmBroadcastReceiver.class); 96 PendingIntent pending = PendingIntent.getBroadcast(getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT); 97 AlarmManager am =(AlarmManager) getSystemService(ALARM_SERVICE); 98 am.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pending); 99 100 Snackbar snackbar = Snackbar.make(view, "追加しました", Snackbar.LENGTH_SHORT); 101 snackbar.setAction("戻る", new View.OnClickListener() { 102 @Override 103 public void onClick(View v) { 104 finish(); 105 } 106 }); 107 snackbar.setActionTextColor(Color.YELLOW); 108 snackbar.show(); 109 } 110 else{ 111 realm.executeTransaction(new Realm.Transaction() { 112 @Override 113 public void execute(Realm db) { 114 Routine routine = db.where(Routine.class).equalTo("id",routineId).findFirst(); 115 String str1 = titleEdit.getText().toString(); 116 routine.title = str1; 117 String str2 = targetEdit.getText().toString(); 118 routine.target = str2; 119 routine.now=checkBox.isChecked(); 120 } 121 }); 122 123 Snackbar snackbar = Snackbar.make(view, "修正しました", Snackbar.LENGTH_SHORT); 124 snackbar.setAction("戻る", new View.OnClickListener() { 125 @Override 126 public void onClick(View v) { 127 finish(); 128 } 129 }); 130 snackbar.setActionTextColor(Color.YELLOW); 131 snackbar.show(); 132 } 133 } 134 }); 135 136 delete.setOnClickListener(new View.OnClickListener(){ 137 @Override 138 public void onClick(View view){ 139 realm.executeTransaction(new Realm.Transaction() { 140 @Override 141 public void execute(Realm db) { 142 Routine routine = db.where(Routine.class).equalTo("id",routineId).findFirst(); 143 routine.deleteFromRealm(); 144 } 145 }); 146 Snackbar snackbar = Snackbar.make(view, "削除しました", Snackbar.LENGTH_SHORT); 147 snackbar.setAction("戻る", new View.OnClickListener() { 148 @Override 149 public void onClick(View v) { 150 finish(); 151 } 152 }); 153 snackbar.setActionTextColor(Color.YELLOW); 154 snackbar.show(); 155 } 156 }); 157 } 158 159 @Override 160 protected void onDestroy() { 161 super.onDestroy(); 162 realm.close(); 163 } 164}
java
1AlarmBroadcastReceiver.java 2 3package com.example.makeyourroutine; 4 5import android.content.BroadcastReceiver; 6import android.content.Context; 7import android.content.Intent; 8 9public class AlarmBroadcastReceiver extends BroadcastReceiver { 10 11 @Override 12 public void onReceive(Context context, Intent intent) { 13 Intent editintent = new Intent(context,RoutineEditActivity.class); 14 editintent.putExtra("onReceive",true); 15 editintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 16 context.startActivity(editintent); 17 } 18}
java
1Routine.java 2 3package com.example.makeyourroutine; 4 5import io.realm.RealmObject; 6import io.realm.annotations.PrimaryKey; 7 8public class Routine extends RealmObject { 9 @PrimaryKey 10 long id = 0; 11 String title = ""; 12 String target = ""; 13 boolean now = false; 14 int b1 = 0; 15 int b2 = 0; 16 int b3 = 0; 17 int b4 = 0; 18 int b5 = 0; 19}
あなたの回答
tips
プレビュー