質問編集履歴
5
ShopFragmentの85~87行目を編集しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -38,9 +38,11 @@
|
|
38
38
|
```ここに言語を入力
|
39
39
|
package com.myproject.offlinebudgettrackerappproject;
|
40
40
|
|
41
|
+
import android.app.Application;
|
41
42
|
import android.os.Bundle;
|
42
43
|
|
43
44
|
import androidx.fragment.app.Fragment;
|
45
|
+
import androidx.lifecycle.ViewModelProvider;
|
44
46
|
|
45
47
|
import android.util.Log;
|
46
48
|
import android.view.LayoutInflater;
|
@@ -118,6 +120,9 @@
|
|
118
120
|
BudgetTracker budgetTracker;
|
119
121
|
|
120
122
|
String storeName = enterStoreNameForQuery.getText().toString();
|
123
|
+
budgetTrackerViewModel = new ViewModelProvider.AndroidViewModelFactory(ShopFragment.this
|
124
|
+
.getApplication())
|
125
|
+
.create(BudgetTrackerViewModel.class);
|
121
126
|
budgetTracker = new BudgetTracker();
|
122
127
|
budgetTracker.setStoreName(storeName);
|
123
128
|
|
4
元に戻しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -121,7 +121,7 @@
|
|
121
121
|
budgetTracker = new BudgetTracker();
|
122
122
|
budgetTracker.setStoreName(storeName);
|
123
123
|
|
124
|
-
budgetTrackerViewModel.getStoreNameLists(
|
124
|
+
budgetTrackerViewModel.getStoreNameLists(storeName);
|
125
125
|
|
126
126
|
Log.d("TAG", "onClick: " + enterStoreNameForQuery.getText().toString());
|
127
127
|
// Log.d("TAG", "onClick: " + list);
|
@@ -165,14 +165,14 @@
|
|
165
165
|
return allBudgetTrackerLists;
|
166
166
|
}
|
167
167
|
|
168
|
-
public List<BudgetTracker> getStoreNameLists(
|
168
|
+
public List<BudgetTracker> getStoreNameLists(String storeName) {
|
169
|
-
storeNameLists = repository.queryStoreName(
|
169
|
+
storeNameLists = repository.queryStoreName(storeName);
|
170
170
|
return storeNameLists;
|
171
|
+
}
|
171
172
|
|
172
173
|
public static void insert(BudgetTracker budgetTracker) {
|
173
174
|
repository.insert(budgetTracker);
|
174
175
|
}
|
175
176
|
}
|
176
177
|
|
177
|
-
|
178
178
|
```
|
3
BudgetTRackerViewModelのgetStoreNameListsのgetStoreNameListsメソッドを変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -165,10 +165,9 @@
|
|
165
165
|
return allBudgetTrackerLists;
|
166
166
|
}
|
167
167
|
|
168
|
-
public List<BudgetTracker> getStoreNameLists(
|
168
|
+
public List<BudgetTracker> getStoreNameLists(BudgetTracker budgetTracker) {
|
169
|
-
storeNameLists = repository.queryStoreName(
|
169
|
+
storeNameLists = repository.queryStoreName(budgetTracker.getStoreName());
|
170
170
|
return storeNameLists;
|
171
|
-
}
|
172
171
|
|
173
172
|
public static void insert(BudgetTracker budgetTracker) {
|
174
173
|
repository.insert(budgetTracker);
|
2
エラー内容を更新しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -15,10 +15,10 @@
|
|
15
15
|
|
16
16
|
エラー内容
|
17
17
|
```ここに言語を入力
|
18
|
-
2021-12-
|
18
|
+
2021-12-16 16:39:45.420 30171-30171/? E/AndroidRuntime: FATAL EXCEPTION: main
|
19
|
-
Process: com.myproject.offlinebudgettrackerappproject, PID:
|
19
|
+
Process: com.myproject.offlinebudgettrackerappproject, PID: 30171
|
20
20
|
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.myproject.offlinebudgettrackerappproject.model.BudgetTrackerViewModel.getStoreNameLists(java.lang.String)' on a null object reference
|
21
|
-
at com.myproject.offlinebudgettrackerappproject.ShopFragment$1.onClick(ShopFragment.java:
|
21
|
+
at com.myproject.offlinebudgettrackerappproject.ShopFragment$1.onClick(ShopFragment.java:86)
|
22
22
|
at android.view.View.performClick(View.java:7259)
|
23
23
|
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
|
24
24
|
at android.view.View.performClickInternal(View.java:7236)
|
@@ -31,6 +31,7 @@
|
|
31
31
|
at java.lang.reflect.Method.invoke(Native Method)
|
32
32
|
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
|
33
33
|
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
|
34
|
+
|
34
35
|
```
|
35
36
|
|
36
37
|
ShopFragment.java
|
@@ -51,6 +52,8 @@
|
|
51
52
|
import com.myproject.offlinebudgettrackerappproject.model.BudgetTracker;
|
52
53
|
import com.myproject.offlinebudgettrackerappproject.model.BudgetTrackerViewModel;
|
53
54
|
|
55
|
+
import java.util.List;
|
56
|
+
|
54
57
|
/**
|
55
58
|
* A simple {@link Fragment} subclass.
|
56
59
|
* Use the {@link ShopFragment#newInstance} factory method to
|
@@ -112,12 +115,16 @@
|
|
112
115
|
storeSearchQueryBtn.setOnClickListener(new View.OnClickListener() {
|
113
116
|
@Override
|
114
117
|
public void onClick(View view) {
|
118
|
+
BudgetTracker budgetTracker;
|
115
119
|
|
116
120
|
String storeName = enterStoreNameForQuery.getText().toString();
|
121
|
+
budgetTracker = new BudgetTracker();
|
122
|
+
budgetTracker.setStoreName(storeName);
|
117
123
|
|
118
|
-
budgetTrackerViewModel.getStoreNameLists(
|
124
|
+
budgetTrackerViewModel.getStoreNameLists(budgetTracker.getStoreName());
|
119
125
|
|
120
126
|
Log.d("TAG", "onClick: " + enterStoreNameForQuery.getText().toString());
|
127
|
+
// Log.d("TAG", "onClick: " + list);
|
121
128
|
|
122
129
|
}
|
123
130
|
});
|
@@ -130,102 +137,43 @@
|
|
130
137
|
|
131
138
|
BudgetTrackerViewModel.java
|
132
139
|
```ここに言語を入力
|
133
|
-
package com.myproject.offlinebudgettrackerappproject;
|
140
|
+
package com.myproject.offlinebudgettrackerappproject.model;
|
134
141
|
|
135
|
-
import android.
|
142
|
+
import android.app.Application;
|
136
143
|
|
137
|
-
import androidx.
|
144
|
+
import androidx.annotation.NonNull;
|
145
|
+
import androidx.lifecycle.AndroidViewModel;
|
146
|
+
import androidx.lifecycle.LiveData;
|
138
147
|
|
139
|
-
import android.util.Log;
|
140
|
-
import
|
148
|
+
import com.myproject.offlinebudgettrackerappproject.data.BudgetTrackerRepository;
|
141
|
-
import android.view.View;
|
142
|
-
import android.view.ViewGroup;
|
143
|
-
import android.widget.Button;
|
144
|
-
import android.widget.EditText;
|
145
149
|
|
146
|
-
import com.myproject.offlinebudgettrackerappproject.model.BudgetTracker;
|
147
|
-
import com.myproject.offlinebudgettrackerappproject.model.BudgetTrackerViewModel;
|
148
|
-
|
149
150
|
import java.util.List;
|
150
151
|
|
151
|
-
/**
|
152
|
-
* A simple {@link Fragment} subclass.
|
153
|
-
* Use the {@link ShopFragment#newInstance} factory method to
|
154
|
-
* create an instance of this fragment.
|
155
|
-
*/
|
156
|
-
public class
|
152
|
+
public class BudgetTrackerViewModel extends AndroidViewModel {
|
157
153
|
|
154
|
+
public static BudgetTrackerRepository repository;
|
155
|
+
public final LiveData<List<BudgetTracker>> allBudgetTrackerLists;
|
158
|
-
|
156
|
+
public List<BudgetTracker> storeNameLists;
|
159
157
|
|
160
|
-
|
161
|
-
|
158
|
+
public BudgetTrackerViewModel(@NonNull Application application) {
|
159
|
+
super(application);
|
162
|
-
|
160
|
+
repository = new BudgetTrackerRepository(application);
|
163
|
-
|
161
|
+
allBudgetTrackerLists = repository.getAllBudgetTrackerData();
|
164
|
-
private static final String ARG_PARAM2 = "param2";
|
165
|
-
|
166
|
-
// TODO: Rename and change types of parameters
|
167
|
-
private String mParam1;
|
168
|
-
private String mParam2;
|
169
|
-
|
170
|
-
public ShopFragment() {
|
171
|
-
// Required empty public constructor
|
172
162
|
}
|
173
163
|
|
174
|
-
/**
|
175
|
-
* Use this factory method to create a new instance of
|
176
|
-
* this fragment using the provided parameters.
|
177
|
-
*
|
178
|
-
* @param param1 Parameter 1.
|
179
|
-
* @param param2 Parameter 2.
|
180
|
-
* @return A new instance of fragment ShopFragment.
|
181
|
-
*/
|
182
|
-
// TODO: Rename and change types and number of parameters
|
183
|
-
public
|
164
|
+
public LiveData<List<BudgetTracker>> getAllBudgetTrackerLists() {
|
184
|
-
ShopFragment fragment = new ShopFragment();
|
185
|
-
Bundle args = new Bundle();
|
186
|
-
args.putString(ARG_PARAM1, param1);
|
187
|
-
args.putString(ARG_PARAM2, param2);
|
188
|
-
fragment.setArguments(args);
|
189
|
-
return
|
165
|
+
return allBudgetTrackerLists;
|
190
166
|
}
|
191
167
|
|
192
|
-
@Override
|
193
|
-
public
|
168
|
+
public List<BudgetTracker> getStoreNameLists(String storeName) {
|
194
|
-
super.onCreate(savedInstanceState);
|
195
|
-
if (getArguments() != null) {
|
196
|
-
|
169
|
+
storeNameLists = repository.queryStoreName(storeName);
|
197
|
-
|
170
|
+
return storeNameLists;
|
198
|
-
|
199
|
-
}
|
200
171
|
}
|
201
172
|
|
202
|
-
@Override
|
203
|
-
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
204
|
-
Bundle savedInstanceState) {
|
205
|
-
View view = inflater.inflate(R.layout.fragment_shop, container, false);
|
206
|
-
EditText enterStoreNameForQuery = (EditText) view.findViewById(R.id.shop_search);
|
207
|
-
Button storeSearchQueryBtn = (Button) view.findViewById(R.id.btn_shop_search);
|
208
|
-
|
209
|
-
storeSearchQueryBtn.setOnClickListener(new View.OnClickListener() {
|
210
|
-
@Override
|
211
|
-
|
173
|
+
public static void insert(BudgetTracker budgetTracker) {
|
212
|
-
|
174
|
+
repository.insert(budgetTracker);
|
213
|
-
|
214
|
-
String storeName = enterStoreNameForQuery.getText().toString();
|
215
|
-
budgetTracker = new BudgetTracker();
|
216
|
-
budgetTracker.setStoreName(storeName);
|
217
|
-
|
218
|
-
budgetTrackerViewModel.getStoreNameLists(budgetTracker.getStoreName());
|
219
|
-
|
220
|
-
Log.d("TAG", "onClick: " + enterStoreNameForQuery.getText().toString());
|
221
|
-
// Log.d("TAG", "onClick: " + list);
|
222
|
-
|
223
|
-
}
|
224
|
-
});
|
225
|
-
|
226
|
-
return view;
|
227
175
|
}
|
228
|
-
|
229
176
|
}
|
230
177
|
|
178
|
+
|
231
179
|
```
|
1
ShopFragmnetの82~84行目を更新しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -130,42 +130,102 @@
|
|
130
130
|
|
131
131
|
BudgetTrackerViewModel.java
|
132
132
|
```ここに言語を入力
|
133
|
-
package com.myproject.offlinebudgettrackerappproject
|
133
|
+
package com.myproject.offlinebudgettrackerappproject;
|
134
134
|
|
135
|
-
import android.
|
135
|
+
import android.os.Bundle;
|
136
136
|
|
137
|
-
import androidx.
|
137
|
+
import androidx.fragment.app.Fragment;
|
138
|
-
import androidx.lifecycle.AndroidViewModel;
|
139
|
-
import androidx.lifecycle.LiveData;
|
140
138
|
|
139
|
+
import android.util.Log;
|
141
|
-
import
|
140
|
+
import android.view.LayoutInflater;
|
141
|
+
import android.view.View;
|
142
|
+
import android.view.ViewGroup;
|
143
|
+
import android.widget.Button;
|
144
|
+
import android.widget.EditText;
|
142
145
|
|
146
|
+
import com.myproject.offlinebudgettrackerappproject.model.BudgetTracker;
|
147
|
+
import com.myproject.offlinebudgettrackerappproject.model.BudgetTrackerViewModel;
|
148
|
+
|
143
149
|
import java.util.List;
|
144
150
|
|
151
|
+
/**
|
152
|
+
* A simple {@link Fragment} subclass.
|
153
|
+
* Use the {@link ShopFragment#newInstance} factory method to
|
154
|
+
* create an instance of this fragment.
|
155
|
+
*/
|
145
|
-
public class
|
156
|
+
public class ShopFragment extends Fragment {
|
146
157
|
|
147
|
-
public static BudgetTrackerRepository repository;
|
148
|
-
public final LiveData<List<BudgetTracker>> allBudgetTrackerLists;
|
149
|
-
|
158
|
+
BudgetTrackerViewModel budgetTrackerViewModel;
|
150
159
|
|
160
|
+
|
151
|
-
|
161
|
+
// TODO: Rename parameter arguments, choose names that match
|
152
|
-
super(application);
|
153
|
-
|
162
|
+
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
|
154
|
-
|
163
|
+
private static final String ARG_PARAM1 = "param1";
|
164
|
+
private static final String ARG_PARAM2 = "param2";
|
165
|
+
|
166
|
+
// TODO: Rename and change types of parameters
|
167
|
+
private String mParam1;
|
168
|
+
private String mParam2;
|
169
|
+
|
170
|
+
public ShopFragment() {
|
171
|
+
// Required empty public constructor
|
155
172
|
}
|
156
173
|
|
174
|
+
/**
|
175
|
+
* Use this factory method to create a new instance of
|
176
|
+
* this fragment using the provided parameters.
|
177
|
+
*
|
178
|
+
* @param param1 Parameter 1.
|
179
|
+
* @param param2 Parameter 2.
|
180
|
+
* @return A new instance of fragment ShopFragment.
|
181
|
+
*/
|
182
|
+
// TODO: Rename and change types and number of parameters
|
157
|
-
public
|
183
|
+
public static ShopFragment newInstance(String param1, String param2) {
|
184
|
+
ShopFragment fragment = new ShopFragment();
|
185
|
+
Bundle args = new Bundle();
|
186
|
+
args.putString(ARG_PARAM1, param1);
|
187
|
+
args.putString(ARG_PARAM2, param2);
|
188
|
+
fragment.setArguments(args);
|
158
|
-
return
|
189
|
+
return fragment;
|
159
190
|
}
|
160
191
|
|
192
|
+
@Override
|
161
|
-
public
|
193
|
+
public void onCreate(Bundle savedInstanceState) {
|
162
|
-
|
194
|
+
super.onCreate(savedInstanceState);
|
163
|
-
|
195
|
+
if (getArguments() != null) {
|
196
|
+
mParam1 = getArguments().getString(ARG_PARAM1);
|
197
|
+
mParam2 = getArguments().getString(ARG_PARAM2);
|
198
|
+
|
199
|
+
}
|
164
200
|
}
|
165
201
|
|
202
|
+
@Override
|
203
|
+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
204
|
+
Bundle savedInstanceState) {
|
205
|
+
View view = inflater.inflate(R.layout.fragment_shop, container, false);
|
206
|
+
EditText enterStoreNameForQuery = (EditText) view.findViewById(R.id.shop_search);
|
207
|
+
Button storeSearchQueryBtn = (Button) view.findViewById(R.id.btn_shop_search);
|
208
|
+
|
209
|
+
storeSearchQueryBtn.setOnClickListener(new View.OnClickListener() {
|
210
|
+
@Override
|
166
|
-
|
211
|
+
public void onClick(View view) {
|
167
|
-
|
212
|
+
BudgetTracker budgetTracker;
|
213
|
+
|
214
|
+
String storeName = enterStoreNameForQuery.getText().toString();
|
215
|
+
budgetTracker = new BudgetTracker();
|
216
|
+
budgetTracker.setStoreName(storeName);
|
217
|
+
|
218
|
+
budgetTrackerViewModel.getStoreNameLists(budgetTracker.getStoreName());
|
219
|
+
|
220
|
+
Log.d("TAG", "onClick: " + enterStoreNameForQuery.getText().toString());
|
221
|
+
// Log.d("TAG", "onClick: " + list);
|
222
|
+
|
223
|
+
}
|
224
|
+
});
|
225
|
+
|
226
|
+
return view;
|
168
227
|
}
|
228
|
+
|
169
229
|
}
|
170
230
|
|
171
231
|
```
|