質問編集履歴

3

206行目を更新

2022/05/12 11:24

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -203,7 +203,7 @@
203
203
  });
204
204
 
205
205
 
206
- return inflater.inflate(R.layout.fragment_home, container, false);
206
+ return view;
207
207
 
208
208
  }
209
209
  }

2

HomeFragment.javaを全文掲載

2022/05/11 12:27

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -10,17 +10,181 @@
10
10
 
11
11
  HomeFragment.java
12
12
  ```ここに言語を入力
13
+ package com.myproject.offlinebudgettrackerappproject;
14
+
15
+ import android.app.DatePickerDialog;
16
+ import android.icu.util.Calendar;
17
+ import android.os.Bundle;
18
+ import android.util.Log;
19
+ import android.view.LayoutInflater;
20
+ import android.view.View;
21
+ import android.view.ViewGroup;
22
+ import android.widget.Button;
23
+ import android.widget.DatePicker;
24
+ import android.widget.EditText;
25
+ import android.widget.RadioButton;
26
+ import android.widget.RadioGroup;
27
+ import android.widget.TextView;
28
+
29
+ import androidx.fragment.app.Fragment;
30
+ import androidx.lifecycle.ViewModelProvider;
31
+
32
+ import com.myproject.offlinebudgettrackerappproject.databinding.ActivityMainBinding;
33
+ import com.myproject.offlinebudgettrackerappproject.model.BudgetTracker;
34
+ import com.myproject.offlinebudgettrackerappproject.model.BudgetTrackerAliasViewModel;
35
+ import com.myproject.offlinebudgettrackerappproject.model.BudgetTrackerViewModel;
36
+
37
+ import org.eazegraph.lib.charts.PieChart;
38
+
39
+ import java.util.List;
40
+
41
+ /**
42
+ * A simple {@link Fragment} subclass.
43
+ * Use the {@link HomeFragment#newInstance} factory method to
44
+ * create an instance of this fragment.
13
- 一部省略
45
+ */
46
+ public class HomeFragment extends Fragment {
47
+
48
+ private BudgetTrackerViewModel budgetTrackerViewModel;
49
+ private BudgetTrackerAliasViewModel budgetTrackerAliasViewModel;
50
+ PieChart homePieChart;
51
+ RadioGroup radioHomeGroup;
52
+ RadioButton radioHomeButton;
53
+ EditText radioSearchHomeName;
54
+ EditText radioSearchDateHomeFrom;
55
+ EditText radioSearchDateHomeTo;
56
+ TextView searchCalcResultHomeTxt;
14
- Button radioSearchHomeBtn;
57
+ Button radioSearchHomeBtn;
15
-
58
+ ActivityMainBinding activityMainBinding;
59
+
60
+ // TODO: Rename parameter arguments, choose names that match
61
+ // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
62
+ private static final String ARG_PARAM1 = "param1";
63
+ private static final String ARG_PARAM2 = "param2";
64
+
65
+ // TODO: Rename and change types of parameters
66
+ private String mParam1;
67
+ private String mParam2;
68
+
69
+ public HomeFragment() {
70
+ // Required empty public constructor
71
+ }
72
+
73
+ /**
74
+ * Use this factory method to create a new instance of
75
+ * this fragment using the provided parameters.
76
+ *
77
+ * @param param1 Parameter 1.
78
+ * @param param2 Parameter 2.
79
+ * @return A new instance of fragment HomeFragment.
80
+ */
81
+ // TODO: Rename and change types and number of parameters
82
+ public static HomeFragment newInstance(String param1, String param2) {
83
+ HomeFragment fragment = new HomeFragment();
84
+ Bundle args = new Bundle();
85
+ args.putString(ARG_PARAM1, param1);
86
+ args.putString(ARG_PARAM2, param2);
87
+ fragment.setArguments(args);
88
+ return fragment;
89
+ }
90
+
91
+ @Override
92
+ public void onCreate(Bundle savedInstanceState) {
93
+ super.onCreate(savedInstanceState);
94
+ if (getArguments() != null) {
95
+ mParam1 = getArguments().getString(ARG_PARAM1);
96
+ mParam2 = getArguments().getString(ARG_PARAM2);
97
+
98
+ budgetTrackerViewModel = new ViewModelProvider.AndroidViewModelFactory(HomeFragment.this
99
+ .getActivity().getApplication())
100
+ .create(BudgetTrackerViewModel.class);
101
+
102
+ // budgetTrackerViewModel.getAllBudgetTrackerLists().observe(this, budgetTrackers -> {
103
+ // Log.d("TAG", "onCreate: " + budgetTrackers.get(0).getProductName());
104
+ // });
105
+ }
106
+ }
107
+
108
+ @Override
109
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
110
+ Bundle savedInstanceState) {
111
+ View view = inflater.inflate(R.layout.fragment_home, container, false);
112
+
113
+ homePieChart = (PieChart) view.findViewById(R.id.pie_chart);
114
+ radioHomeGroup = (RadioGroup) view.findViewById(R.id.home_radio_group);
115
+ radioSearchHomeName = (EditText) view.findViewById(R.id.home_radio_search_name);
116
+ radioSearchDateHomeFrom = (EditText) view.findViewById(R.id.home_radio_search_date_from_txt);
117
+ radioSearchDateHomeTo = (EditText) view.findViewById(R.id.home_radio_search_date_to_txt);
118
+ searchCalcResultHomeTxt = (TextView) view.findViewById(R.id.home_radio_search_calc_result_txt);
16
- radioSearchHomeBtn = (Button) view.findViewById(R.id.home_radio_search_btn);
119
+ radioSearchHomeBtn = (Button) view.findViewById(R.id.home_radio_search_btn);
17
-
120
+ radioSearchHomeBtn.setOnClickListener((View.OnClickListener) getActivity());
121
+ activityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
122
+
18
- 一部省略
123
+ Calendar calendar = Calendar.getInstance();
19
-
124
+ final int year = calendar.get(Calendar.YEAR);
125
+ final int month = calendar.get(Calendar.MONTH);
126
+ final int day = calendar.get(Calendar.DAY_OF_MONTH);
127
+
128
+ radioSearchDateHomeFrom.setOnClickListener(new View.OnClickListener() {
20
- @Override
129
+ @Override
130
+ public void onClick(View view) {
131
+ DatePickerDialog datePickerDialog = new DatePickerDialog(
132
+ getActivity(), new DatePickerDialog.OnDateSetListener() {
133
+ @Override
134
+ public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
135
+ month = month + 1;
136
+ String date = year + "-" + month + "-" + dayOfMonth;
137
+ radioSearchDateHomeFrom.setText(date);
138
+ }
139
+ }, year, month, day);
140
+ datePickerDialog.show();
141
+ }
142
+ });
143
+
144
+ radioSearchDateHomeTo.setOnClickListener(new View.OnClickListener() {
145
+ @Override
146
+ public void onClick(View view) {
147
+ DatePickerDialog datePickerDialog = new DatePickerDialog(
148
+ getActivity(), new DatePickerDialog.OnDateSetListener() {
149
+ @Override
150
+ public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
151
+ month = month + 1;
152
+ String date = year + "-" + month + "-" + dayOfMonth;
153
+ radioSearchDateHomeTo.setText(date);
154
+ }
155
+ }, year, month, day);
156
+ datePickerDialog.show();
157
+ }
158
+ });
159
+
160
+ radioHomeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
161
+ List<BudgetTracker> radioStoreNameLists;
162
+ List<BudgetTracker> radioProductNameLists;
163
+ List<BudgetTracker> radioProductTypeLists;
164
+ String storeName;
165
+ String date1;
166
+ String date2;
167
+ BudgetTracker budgetTracker;
168
+ String calcSumStr;
169
+
170
+ @Override
21
- public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
171
+ public void onCheckedChanged(RadioGroup radioHomeGroup, int checkId) {
22
172
  switch (checkId) {
23
173
  case R.id.home_radio_store_name:
174
+ Log.d("TAG", "onCheckedChanged: store chosen");
175
+ storeName = radioSearchHomeName.getText().toString();
176
+ date1 = radioSearchDateHomeFrom.getText().toString();
177
+ date2 = radioSearchDateHomeTo.getText().toString();
178
+ radioSearchHomeBtn.setOnClickListener(new View.OnClickListener() {
179
+ @Override
180
+ public void onClick(View view) {
181
+ Log.d("TAG", "onClick: Alias Button Clicked");
182
+ BudgetTrackerAliasViewModel.insert(date1, date2, storeName);
183
+
184
+ }
185
+ });
186
+ case R.id.home_radio_product_name:
187
+ Log.d("TAG", "onCheckedChanged: product name chosen");
24
188
  storeName = radioSearchHomeName.getText().toString();
25
189
  date1 = radioSearchDateHomeFrom.getText().toString();
26
190
  date2 = radioSearchDateHomeTo.getText().toString();
@@ -34,124 +198,13 @@
34
198
 
35
199
  }
36
200
  });
37
-
38
201
  }
39
202
  }
203
+ });
204
+
205
+
206
+ return inflater.inflate(R.layout.fragment_home, container, false);
207
+
208
+ }
209
+ }
40
210
  ```
41
- fragment_home.xml
42
- ```ここに言語を入力
43
- <?xml version="1.0" encoding="utf-8"?>
44
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
45
- xmlns:app="http://schemas.android.com/apk/res-auto"
46
- xmlns:tools="http://schemas.android.com/tools"
47
- android:layout_width="match_parent"
48
- android:layout_height="match_parent"
49
- tools:context=".HomeFragment">
50
-
51
- <org.eazegraph.lib.charts.PieChart
52
- android:id="@+id/pie_chart"
53
- android:layout_width="300dp"
54
- android:layout_height="300dp"
55
- android:layout_marginTop="48dp"
56
- android:padding="10dp"
57
- app:egLegendTextSize="18sp"
58
- app:egUsePieRotation="true"
59
- app:egValueTextSize="36sp"
60
- app:layout_constraintEnd_toEndOf="parent"
61
- app:layout_constraintHorizontal_bias="0.495"
62
- app:layout_constraintStart_toStartOf="parent"
63
- app:layout_constraintTop_toTopOf="parent" />
64
-
65
- <RadioGroup
66
- android:id="@+id/home_radio_group"
67
- android:layout_width="wrap_content"
68
- android:layout_height="wrap_content"
69
- android:orientation="horizontal"
70
- app:layout_constraintTop_toBottomOf="@+id/pie_chart"
71
- tools:ignore="MissingConstraints">
72
-
73
- <RadioButton
74
- android:id="@+id/home_radio_store_name"
75
- android:layout_width="wrap_content"
76
- android:layout_height="wrap_content"
77
- android:text="Store name"
78
- android:checked="true" />
79
-
80
- <RadioButton
81
- android:id="@+id/home_radio_product_name"
82
- android:layout_width="wrap_content"
83
- android:layout_height="wrap_content"
84
- android:text="Product name" />
85
-
86
- <RadioButton
87
- android:id="@+id/home_radio_product_type"
88
- android:layout_width="wrap_content"
89
- android:layout_height="wrap_content"
90
- android:text="Product type" />
91
-
92
- </RadioGroup>
93
-
94
- <EditText
95
- android:id="@+id/home_radio_search_name"
96
- android:layout_width="wrap_content"
97
- android:layout_height="wrap_content"
98
- android:layout_marginTop="4dp"
99
- android:ems="10"
100
- android:hint="@string/radio_search_name"
101
- android:inputType="textPersonName"
102
- android:minHeight="48dp"
103
- app:layout_constraintEnd_toEndOf="parent"
104
- app:layout_constraintHorizontal_bias="0.497"
105
- app:layout_constraintStart_toStartOf="parent"
106
- app:layout_constraintTop_toBottomOf="@+id/home_radio_group" />
107
-
108
- <EditText
109
- android:id="@+id/home_radio_search_date_to_txt"
110
- android:layout_width="180sp"
111
- android:layout_height="wrap_content"
112
- android:layout_marginTop="8dp"
113
- android:ems="10"
114
- android:inputType="date"
115
- android:minHeight="48dp"
116
- app:layout_constraintEnd_toEndOf="parent"
117
- app:layout_constraintHorizontal_bias="0.5"
118
- app:layout_constraintStart_toEndOf="@+id/home_radio_search_date_from_txt"
119
- app:layout_constraintTop_toBottomOf="@+id/home_radio_search_name"
120
- tools:ignore="SpeakableTextPresentCheck" />
121
-
122
- <EditText
123
- android:id="@+id/home_radio_search_date_from_txt"
124
- android:layout_width="180sp"
125
- android:layout_height="wrap_content"
126
- android:layout_marginTop="8dp"
127
- android:ems="10"
128
- android:inputType="date"
129
- android:minHeight="48dp"
130
- app:layout_constraintEnd_toStartOf="@+id/home_radio_search_date_to_txt"
131
- app:layout_constraintHorizontal_bias="0.5"
132
- app:layout_constraintStart_toStartOf="parent"
133
- app:layout_constraintTop_toBottomOf="@+id/home_radio_search_name"
134
- tools:ignore="SpeakableTextPresentCheck" />
135
-
136
- <TextView
137
- android:id="@+id/home_radio_search_calc_result_txt"
138
- android:layout_width="wrap_content"
139
- android:layout_height="wrap_content"
140
- android:text="TextView"
141
- app:layout_constraintEnd_toEndOf="parent"
142
- app:layout_constraintHorizontal_bias="0.498"
143
- app:layout_constraintStart_toStartOf="parent"
144
- app:layout_constraintTop_toBottomOf="@+id/home_radio_search_date_from_txt" />
145
-
146
- <Button
147
- android:id="@+id/home_radio_search_btn"
148
- android:layout_width="wrap_content"
149
- android:layout_height="wrap_content"
150
- android:text="Button"
151
- app:layout_constraintEnd_toEndOf="parent"
152
- app:layout_constraintStart_toStartOf="parent"
153
- app:layout_constraintTop_toBottomOf="@+id/home_radio_search_calc_result_txt" />
154
-
155
-
156
- </androidx.constraintlayout.widget.ConstraintLayout>
157
- ```

1

JavaのButtonの生成部分を記述

2022/05/10 13:04

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -10,6 +10,13 @@
10
10
 
11
11
  HomeFragment.java
12
12
  ```ここに言語を入力
13
+ 一部省略
14
+ Button radioSearchHomeBtn;
15
+
16
+ radioSearchHomeBtn = (Button) view.findViewById(R.id.home_radio_search_btn);
17
+
18
+ 一部省略
19
+
13
20
  @Override
14
21
  public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
15
22
  switch (checkId) {