Merge pull request '提出状況-1問題の解決' (#57) from murakumo_提出状況-1問題 into master

Reviewed-on: #57
This commit is contained in:
murakumo 2024-07-17 05:28:44 +00:00
commit b2f452c6fe
6 changed files with 90 additions and 73 deletions

View File

@ -22,7 +22,7 @@ public class CreateUUID {
InsertClassIdforFirebase insertClassIdforFirebase = new InsertClassIdforFirebase();
insertClassIdforFirebase.insertClassId(uuidInt);
//テスト用
uuidInt = 100;
uuidInt = 328071;
return uuidInt;
}
}

View File

@ -343,6 +343,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
// `fetchDataAndCreateRoute`メソッド内ではshutdownを呼び出さない
}
// 未提出者がいることの警告ダイアログ
private void showRouteCreationDialog() {
new AlertDialog.Builder(MainActivity.this)
.setTitle("警告")
@ -359,6 +360,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
.show();
}
// ルート作成の非同期処理
private void createRoute(ExecutorService executor) {
// ProgressDialogを作成
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
@ -414,7 +417,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
});
}
// 作成したルートを(Mapに渡せるように)保存するメソッド
private void saveMyDataList(List<MyDataClass> myDataList) {
// 共有プリファレンスのインスタンスを取得
SharedPreferences sharedPreferences = getSharedPreferences("MyDataList", MODE_PRIVATE);
@ -429,6 +432,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
editor.apply();
}
// 保護者の重複による警告ダイアログ
private void showErrorDialog(List<MyDataClass> myDataList) {
List<Integer> studentNumbers = new ArrayList<>();
for (MyDataClass data : myDataList) {
@ -462,14 +466,14 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
.show();
}
// データベースのインスタンスを取得するメソッド
private AppDatabase getDatabaseInstance() {
return Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable").build();
}
//提出状況の取得
private ArrayList<SubmissionStudent> getSubmissionStudents() {
public ArrayList<SubmissionStudent> getSubmissionStudents() {
ArrayList<SubmissionStudent> submissionStudents = new ArrayList<>();
List<MyDataClass> myDataList = firestoreReception.getMyDataList();
@ -477,40 +481,44 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
CountDownLatch latch = new CountDownLatch(1);
executor.execute(() -> {
// 1. Roomデータベースから全生徒数を取得
AppDatabase db = getDatabaseInstance();
SetUpTableDao setUpTableDao = db.setUpTableDao();
int totalStudentInt = setUpTableDao.getTotalStudent();
// 2. Firestoreから生徒番号のリストを取得
ArrayList<Integer> firestoreStudentNumbersList = new ArrayList<>();
for (MyDataClass myData : myDataList) {
int studentNumberInt = myData.getStudentNumber();
firestoreStudentNumbersList.add(studentNumberInt);
}
try {
// 1. Roomデータベースから全生徒数を取得
AppDatabase db = getDatabaseInstance();
SetUpTableDao setUpTableDao = db.setUpTableDao();
int totalStudentInt = setUpTableDao.getTotalStudent();
// 3. SubmissionStudentオブジェクトのリストを作成
for (int i = 1; i <= totalStudentInt; i++) {
boolean submitted = firestoreStudentNumbersList.contains(i);
submissionStudents.add(new SubmissionStudent(i, submitted));
}
// 2. Firestoreから生徒番号のリストを取得
ArrayList<Integer> firestoreStudentNumbersList = new ArrayList<>();
for (MyDataClass myData : myDataList) {
int studentNumberInt = myData.getStudentNumber();
firestoreStudentNumbersList.add(studentNumberInt);
}
// 4. データベース操作が完了したことを通知
latch.countDown();
// 3. SubmissionStudentオブジェクトのリストを作成
for (int i = 1; i <= totalStudentInt; i++) {
boolean submitted = firestoreStudentNumbersList.contains(i);
submissionStudents.add(new SubmissionStudent(i, submitted));
}
} catch (Exception e) {
e.printStackTrace(); // ログにエラーメッセージを出力
} finally {
// 4. データベース操作が完了したことを通知
latch.countDown();
}
});
try {
// データベース操作が完了するのを待つ
latch.await();
latch.await(); // 非同期処理が完了するまで待機
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
executor.shutdown(); // ExecutorServiceをシャットダウン
}
executor.shutdown();
// SubmissionStudentオブジェクトのリストを返す
return submissionStudents;
}
@Override
protected void onDestroy() {
super.onDestroy();

View File

@ -1,7 +1,9 @@
package com.example.oplogy;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
@ -11,10 +13,9 @@ import java.util.ArrayList;
import java.util.List;
public class SubmissionActivity extends AppCompatActivity {
private final List<SubmissionStudent> studentsList = new ArrayList<>();
private RecyclerView recyclerView;
private SubmissionAdapter submissionAdapter;
private final List<SubmissionStudent> studentsList = new ArrayList<>();
ArrayList<Integer> studentNumbersList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -25,10 +26,7 @@ public class SubmissionActivity extends AppCompatActivity {
ImageView backButton = findViewById(R.id.BackMain_fromSubmission);
backButton.setOnClickListener(v -> finish());
// インテントから提出状況の生徒の数を取得
studentNumbersList=getIntent().getIntegerArrayListExtra("submissionStudents");
// RecyclerViewとアダプターの初期化
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
submissionAdapter = new SubmissionAdapter(studentsList);
@ -37,15 +35,21 @@ public class SubmissionActivity extends AppCompatActivity {
// 生徒のリストを取得
fetchStudents();
}
private void fetchStudents() {
// インテントから生徒のリストを取得
ArrayList<SubmissionStudent> submissionStudentsList = getIntent().getParcelableArrayListExtra("submissionStudents");
// 生徒のリストを反復処理しそれをRecyclerViewに表示
studentsList.addAll(submissionStudentsList);
if (submissionStudentsList != null) {
Log.d("SubmissionActivity", "Size of submissionStudentsList: " + submissionStudentsList.size());
studentsList.addAll(submissionStudentsList);
} else {
Log.e("SubmissionActivity", "submissionStudentsList is null");
Toast.makeText(this, "生徒のリストが取得できませんでした", Toast.LENGTH_SHORT).show();
}
// データが変更されたことをアダプターに通知
submissionAdapter.notifyDataSetChanged();
}
}

View File

@ -8,24 +8,13 @@ import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class SubmissionAdapter extends RecyclerView.Adapter<SubmissionAdapter.StudentViewHolder> {
private final List<SubmissionStudent> studentsList;
public static class StudentViewHolder extends RecyclerView.ViewHolder {
public TextView studentNumberTextView;
public TextView statusTextView;
public StudentViewHolder(View view) {
super(view);
// レイアウトファイルのTextViewを取得
studentNumberTextView = view.findViewById(R.id.studentNumberTextView);
statusTextView = view.findViewById(R.id.statusTextView);
}
}
public SubmissionAdapter(List<SubmissionStudent> students) {
this.studentsList = students;
}
@ -42,24 +31,42 @@ public class SubmissionAdapter extends RecyclerView.Adapter<SubmissionAdapter.St
public void onBindViewHolder(StudentViewHolder holder, int position) {
SubmissionStudent student = studentsList.get(position);
holder.studentNumberTextView.setText(String.valueOf(student.getStudentNumber()));
// 提出済みかどうかで表示を変える
if (student.isSubmitted()) {
holder.statusTextView.setText("提出済み");
holder.statusTextView.setBackgroundColor(Color.BLACK);
holder.statusTextView.setTextColor(Color.WHITE);
holder.studentNumberTextView.setBackgroundColor(Color.BLACK);
holder.studentNumberTextView.setTextColor(Color.WHITE);
} else {
holder.statusTextView.setText("未提出");
holder.statusTextView.setBackgroundColor(Color.RED);
holder.statusTextView.setTextColor(Color.WHITE);
holder.studentNumberTextView.setBackgroundColor(Color.RED);
holder.studentNumberTextView.setTextColor(Color.WHITE);
}
updateStatus(holder, student.isSubmitted());
}
@Override
public int getItemCount() {
return studentsList.size();
}
//真偽値に応じて提出済みか未提出をセットする
private void updateStatus(StudentViewHolder holder, boolean isSubmitted) {
if (isSubmitted) {
holder.statusTextView.setText("提出済み");
setColors(holder, Color.BLACK, Color.WHITE);
} else {
holder.statusTextView.setText("未提出");
setColors(holder, Color.RED, Color.WHITE);
}
}
//真偽値に応じて色をセットする
private void setColors(StudentViewHolder holder, int backgroundColor, int textColor) {
holder.statusTextView.setBackgroundColor(backgroundColor);
holder.statusTextView.setTextColor(textColor);
holder.studentNumberTextView.setBackgroundColor(backgroundColor);
holder.studentNumberTextView.setTextColor(textColor);
}
public static class StudentViewHolder extends RecyclerView.ViewHolder {
public TextView studentNumberTextView;
public TextView statusTextView;
public StudentViewHolder(View view) {
super(view);
// レイアウトファイルのTextViewを取得
studentNumberTextView = view.findViewById(R.id.studentNumberTextView);
statusTextView = view.findViewById(R.id.statusTextView);
}
}
}

View File

@ -4,7 +4,6 @@
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/studentNumberTextView"
android:layout_width="0dp"
@ -22,10 +21,10 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="提出状況"
android:textSize="36sp"
android:background="@android:color/white"
android:gravity="center_vertical"
android:paddingStart="8dp"
android:background="@android:color/white"
android:textColor="@android:color/black" />
</LinearLayout>
android:text="提出状況"
android:textColor="@android:color/black"
android:textSize="36sp" />
</LinearLayout>

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
@ -10,7 +9,7 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
@ -29,15 +28,15 @@
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="提出状況"
android:textSize="40dp"
android:padding="8dp"
/>
android:textSize="40dp" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
android:layout_height="0dp"
android:layout_weight="9"
android:scrollbars="vertical" />
</LinearLayout>