room値を拾う処理を実装 #8

Merged
Utahshi merged 1 commits from CreateRoot2 into master 2024-06-20 09:22:25 +00:00
5 changed files with 67 additions and 23 deletions
Showing only changes of commit 6fc1e6daa5 - Show all commits

View File

@ -1,7 +1,7 @@
package com.example.oplogy;
import androidx.room.Database;
import androidx.room.RoomDatabase;
import androidx.room.TypeConverters;
@Database(entities = {SetUpTable.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {

View File

@ -1,7 +1,10 @@
package com.example.oplogy;
import android.content.Context;
import android.util.Log;
import androidx.room.Room;
import com.google.firebase.Timestamp;
import java.text.SimpleDateFormat;
@ -9,11 +12,22 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CreateRoot {
public void receiveData(List<MyDataClass> myDataList) {
private Context context;
private AppDatabase db;
public CreateRoot(MainActivity activity) {
this.context = activity;
this.db = Room.databaseBuilder(activity.getApplicationContext(), AppDatabase.class, "SetUpTable").build();
}
public void receiveData(List<MyDataClass> myDataList) {
for (int i = 0; i < myDataList.size(); i++) {
//希望時間帯の終了時刻から開始時刻を引いて希望時間帯の長さ(timezone)に入れる
MyDataClass data = myDataList.get(i);
List<Timestamp> firstDay = data.getFirstDay();
Timestamp startTime = firstDay.get(0);
@ -21,6 +35,7 @@ public class CreateRoot {
Long timezone = endTime.getSeconds() - startTime.getSeconds();
data.setTimezone(timezone);
//TimeStampを日付に変換
Date startDate = new Date(startTime.getSeconds() * 1000);
Date endDate = new Date(endTime.getSeconds() * 1000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@ -37,7 +52,7 @@ public class CreateRoot {
Log.d("CreateRoot", "(index: " + i + ") startDate: " + myDataList.get(i).getStartDateString());
Log.d("CreateRoot", "(index: " + i + ") data: " + myDataList.get(i));
}
// timezoneを比較するComparatorを作成
// timezoneを比較するComparatortimezoneが短い順に並べる
Comparator<MyDataClass> comparator = new Comparator<MyDataClass>() {
@Override
public int compare(MyDataClass data1, MyDataClass data2) {
@ -51,6 +66,17 @@ public class CreateRoot {
Log.d("CreateRoot", "(index: " + i + ") timezone: " + myDataList.get(i).getTimezone());
Log.d("CreateRoot", "(index: " + i + ") startDate: " + myDataList.get(i).getStartDateString());
Log.d("CreateRoot", "(index: " + i + ") data: " + myDataList.get(i));
}
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(new Runnable() {
@Override
public void run() {
SetUpTableDao setUpTableDao = db.setUpTableDao();
String startTime=setUpTableDao.getStartTime();
Log.d("CreateRoot", "開始時間" + startTime);
}
});
}
}

View File

@ -11,7 +11,6 @@ import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -24,7 +23,7 @@ public class FirestoreReception {
}
//firestoreから受け取ったデータを束ねるためのマップ
public List<MyDataClass>myDataList = new ArrayList<>();
public List<MyDataClass> myDataList = new ArrayList<>();
//ClassIdを引数にデータの作成を行う
public void getDocumentsByClassId(int classId) {
@ -61,11 +60,10 @@ public class FirestoreReception {
//取得したデータをログ表示
for(MyDataClass data :myDataList){
for (MyDataClass data : myDataList) {
Log.i("FirestoreReceptiond", "data: " + data.toString());
}
CreateRoot createRoot=new CreateRoot();
createRoot.receiveData(myDataList);
} else {
Log.w("FirestoreReceptiond", "Error getting documents.", task.getException());
}
@ -73,4 +71,12 @@ public class FirestoreReception {
});
}
public int getMyDataListSize() {
return myDataList.size();
}
public List<MyDataClass> getMyDataList() {
return myDataList;
}
}

View File

@ -14,7 +14,10 @@ import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.firestore.FirebaseFirestore;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// ID作成のTextViewとImageView
private TextView creatUUID;
@ -68,39 +71,44 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
firestoreReception.getDocumentsByClassId(100);
}
// クリック処理
// クリック処理
@Override
public void onClick(View view) {
// ID作成のクリック処理
if(view == creatUUID){
if (view == creatUUID) {
imageUuid.setImageResource(R.drawable.ischecked_uuid);
showUUIDYesNoDialog();//UUIDを表示するかのダイアログ
}
// セットアップのクリック処理
if(view == setUp){
if (view == setUp) {
imageSetup.setImageResource(R.drawable.ischecked_uuid);
Intent toSetup = new Intent(MainActivity.this,SetUpActivity.class);
Intent toSetup = new Intent(MainActivity.this, SetUpActivity.class);
startActivity(toSetup);
}
// ルート作成のクリック処理
if(view == root){
if (view == root) {
//CreateRootにmyDataListを渡す
imageRoot.setImageResource(R.drawable.pin);
Intent toRoot = new Intent(MainActivity.this,Maps.class);
List<MyDataClass> myDataList = firestoreReception.getMyDataList();
CreateRoot createRoot = new CreateRoot(MainActivity.this);
createRoot.receiveData(myDataList);
Intent toRoot = new Intent(MainActivity.this, CreateRoot.class);
toRoot.putExtra("myDataList", (ArrayList<MyDataClass>) myDataList);
startActivity(toRoot);
}
// 提出状況のクリック処理
if(view == submission){
Intent toSubmission = new Intent(MainActivity.this,SubmissionActivity.class);
if (view == submission) {
Intent toSubmission = new Intent(MainActivity.this, SubmissionActivity.class);
startActivity(toSubmission);
}
}
private void showUUIDYesNoDialog() {
//ダイアログの表示
AlertDialog.Builder builder = new AlertDialog.Builder(this);
@ -119,7 +127,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("DialogNO","DialogでNoが選ばれました");
Log.d("DialogNO", "DialogでNoが選ばれました");
}
});
builder.show();

View File

@ -1,11 +1,10 @@
package com.example.oplogy;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;
import java.util.List;
@Dao
public interface SetUpTableDao {
@Insert
@ -16,4 +15,9 @@ public interface SetUpTableDao {
//名前が一致しているかの確認
@Query("SELECT * FROM SetUpTable WHERE teacherName = :name LIMIT 1")
SetUpTable findByName(String name);
//開始時間と終了時間の取得
@Query("SELECT startTime FROM SetUpTable")
String getStartTime();
@Query("SELECT endTime FROM SetUpTable")
String getEndTime();
}