SetUpActivityで情報の保存および更新する処理の実装完了 #6

Merged
murakumo merged 1 commits from murakumo_setupの情報をroomに保存し隊 into master 2024-06-19 07:13:51 +00:00
4 changed files with 57 additions and 2 deletions
Showing only changes of commit 2ad164b721 - Show all commits

View File

@ -27,7 +27,7 @@ public class FirestoreReception {
public List<MyDataClass>myDataList = new ArrayList<>(); public List<MyDataClass>myDataList = new ArrayList<>();
//ClassIdを引数にデータの作成を行う //ClassIdを引数にデータの作成を行う
public void getDocumentsByClassId(int classId, MainActivity context) { public void getDocumentsByClassId(int classId) {
CollectionReference collectionRef = db.collection("QuestionnaireForms"); CollectionReference collectionRef = db.collection("QuestionnaireForms");
// classIdが引数のものを取得する // classIdが引数のものを取得する

View File

@ -65,7 +65,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
db = FirebaseFirestore.getInstance(); db = FirebaseFirestore.getInstance();
firestoreReception = new FirestoreReception(); firestoreReception = new FirestoreReception();
firestoreReception.getDocumentsByClassId(100,MainActivity.this); firestoreReception.getDocumentsByClassId(100);

View File

@ -12,12 +12,16 @@ import android.widget.Button;
import android.widget.RadioButton; import android.widget.RadioButton;
import android.widget.TextView; import android.widget.TextView;
import android.widget.TimePicker; import android.widget.TimePicker;
import android.widget.Toast;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import androidx.room.Room;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SetUpActivity extends FragmentActivity public class SetUpActivity extends FragmentActivity
@ -47,6 +51,9 @@ public class SetUpActivity extends FragmentActivity
Button startTimeSetButton; Button startTimeSetButton;
Button endTimeSetButton; Button endTimeSetButton;
@SuppressLint("MissingInflatedId") @SuppressLint("MissingInflatedId")
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -108,6 +115,47 @@ public class SetUpActivity extends FragmentActivity
Log.d(TAG, "Total Student" + totalStudent); Log.d(TAG, "Total Student" + totalStudent);
Log.d(TAG, "onClick: できてるよ"); Log.d(TAG, "onClick: できてるよ");
// データベースへの登録処理
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
//roomのインスタンスを作成
AppDatabase db = Room.databaseBuilder(
getApplicationContext(),
AppDatabase.class,
"SetUpTable"
).build();
SetUpTableDao setUpTableDao = db.setUpTableDao();
// Roomの操作を行う
SetUpTable setUpTable = new SetUpTable(
teacherName,
startPoint,
startTime,
endTime,
intervalTime,
startBreakTime,
endBreakTime,
totalStudent
);
// 同じ名前のエントリが存在するかどうかを確認
SetUpTable existingSetUpTable = setUpTableDao.findByName(teacherName);
if (existingSetUpTable != null) {
// エントリが存在する場合はそのエントリを更新
setUpTable.setId(existingSetUpTable.getId()); // 既存のIDを設定
setUpTableDao.update(setUpTable);
runOnUiThread(() -> Toast.makeText(SetUpActivity.this, "更新しました", Toast.LENGTH_SHORT).show());
} else {
// エントリが存在しない場合は新しいエントリを挿入
setUpTableDao.insertAll(setUpTable);
runOnUiThread(() -> Toast.makeText(SetUpActivity.this, "登録しました", Toast.LENGTH_SHORT).show());
}
});
}); });
startTimeSetButton.setOnClickListener(v -> { startTimeSetButton.setOnClickListener(v -> {
isStartTimeSelected = 1; //ボタンの判別 isStartTimeSelected = 1; //ボタンの判別

View File

@ -2,6 +2,7 @@ package com.example.oplogy;
import androidx.room.Dao; import androidx.room.Dao;
import androidx.room.Insert; import androidx.room.Insert;
import androidx.room.Query; import androidx.room.Query;
import androidx.room.Update;
import java.util.List; import java.util.List;
@ -9,4 +10,10 @@ import java.util.List;
public interface SetUpTableDao { public interface SetUpTableDao {
@Insert @Insert
void insertAll(SetUpTable... setUpTables); void insertAll(SetUpTable... setUpTables);
//更新処理
@Update
void update(SetUpTable setUpTable);
//名前が一致しているかの確認
@Query("SELECT * FROM SetUpTable WHERE teacherName = :name LIMIT 1")
SetUpTable findByName(String name);
} }