質問編集履歴

3

ProfuctFragmrnt/NewBudgetTrackerを編集

2022/06/18 11:37

投稿

Yakusugi
Yakusugi

score98

test CHANGED
File without changes
test CHANGED
@@ -38,7 +38,7 @@
38
38
  ```ここに言語を入力
39
39
  package com.myproject.offlinebudgettrackerappproject;
40
40
 
41
- <import省略
41
+ <省略>
42
42
 
43
43
  /**
44
44
  * A simple {@link Fragment} subclass.
@@ -47,6 +47,7 @@
47
47
  */
48
48
  public class ProductFragment extends Fragment {
49
49
 
50
+ private static final int RESULT_OK = -1;
50
51
  BudgetTrackerViewModel budgetTrackerViewModel;
51
52
  private ProductListViewAdapter productListViewAdapter;
52
53
  public static final String PRODUCT_FRAGMENT_ID = "product_fragment_id";
@@ -106,9 +107,6 @@
106
107
  Button productSearchQueryBtn = (Button) view.findViewById(R.id.btn_product_search);
107
108
  productListView = (ListView) view.findViewById(R.id.product_listview);
108
109
  TextView productTypeSum = (TextView) view.findViewById(R.id.product_type_sum_result_txt);
109
- // AutoCompleteTextView autoCompleteProductTextView = (AutoCompleteTextView) view.findViewById(R.id.product_search_txt);
110
- // ArrayAdapter<String> adapter;
111
- // String [] strings = getResources().getStringArray(R.array.product_type_string_array);
112
110
 
113
111
  activityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
114
112
 
@@ -151,7 +149,7 @@
151
149
  BudgetTracker productItemId = budgetListItems.get(intId);
152
150
  Intent productFragmentIntent = new Intent(getActivity(), NewBudgetTracker.class);
153
151
  productFragmentIntent.putExtra(PRODUCT_FRAGMENT_ID, productItemId.getId());
154
- startActivity(productFragmentIntent);
152
+ startActivityForResult(productFragmentIntent, 1);
155
153
  }
156
154
  });
157
155
 
@@ -161,64 +159,32 @@
161
159
 
162
160
  });
163
161
 
162
+ return view;
163
+ }
164
+
165
+ @Override
164
- // Get intent from NewBudgetTracker
166
+ public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
165
- Intent nbtToPfIntent = getActivity().getIntent();
167
+ super.onActivityResult(requestCode, resultCode, data);
166
- Bundle nbtToPfGetIntentBundle = nbtToPfIntent.getExtras();
168
+
167
- if (nbtToPfGetIntentBundle != null) {
169
+ if (requestCode == 1) {
170
+ if (resultCode == RESULT_OK) {
168
- newBudgetTrackerIntentId = getActivity().getIntent().getIntExtra(NewBudgetTracker.NBT_TO_PF, 0);
171
+ int result = data.getIntExtra("result", 0);
169
- //observe only works when using LiveData
170
- viewModelProductNameLists = budgetTrackerViewModel.getProductTypeLists(productType);
172
+ viewModelProductNameLists = budgetTrackerViewModel.getProductTypeLists(productType);
171
- ProductListViewAdapter productListViewAdapter;
173
+
172
- productListViewAdapter = new ProductListViewAdapter(getActivity(), viewModelProductNameLists);
174
+ productListViewAdapter = new ProductListViewAdapter(getActivity(), viewModelProductNameLists);
173
- productListViewAdapter.notifyDataSetChanged();
174
- productListView.setAdapter(productListViewAdapter);
175
+ productListView.setAdapter(productListViewAdapter);
176
+ }
175
177
  }
176
-
177
- return view;
178
178
  }
179
179
  }
180
180
  ```
181
181
  NewBudgetTracker.java
182
182
  ```ここに言語を入力
183
- //Update button
184
- updateButton.setOnClickListener(new View.OnClickListener() {
185
- @Override
186
- public void onClick(View view) {
187
- if (shopFragmentIntentId != 0 || productFragmentIntentId != 0 || dateFragmentIntentId != 0) {
188
- int idStore = shopFragmentIntentId;
189
- int idProduct = productFragmentIntentId;
190
- int idDate = dateFragmentIntentId;
191
- String date = enterDate.getText().toString();
192
- String storeName = enterStoreName.getText().toString();
193
- String productName = enterProductName.getText().toString();
194
- String productType = enterProductType.getText().toString();
195
- int price = Integer.parseInt(enterPrice.getText().toString());
196
-
197
- if (TextUtils.isEmpty(date) || TextUtils.isEmpty(storeName) || TextUtils.isEmpty(productName) || TextUtils.isEmpty(productType) || TextUtils.isEmpty(String.valueOf(price))) {
198
- Snackbar.make(enterProductName, R.string.empty, Snackbar.LENGTH_SHORT).show();
199
- } else {
200
- BudgetTracker budgetTracker = new BudgetTracker();
201
- if (shopFragmentIntentId != 0) {
183
+ if (shopFragmentIntentId != 0) {
202
- budgetTracker.setId(idStore);
184
+ setResult(RESULT_OK, shopFragmentGetIntent);
203
185
  } else if (productFragmentIntentId != 0) {
204
- budgetTracker.setId(idProduct);
186
+ setResult(RESULT_OK, productFragmentGetIntent);
205
187
  } else if (dateFragmentIntentId != 0) {
206
- budgetTracker.setId(idDate);
188
+ setResult(RESULT_OK, dateFragmentGetIntent);
207
189
  }
208
- budgetTracker.setDate(date);
209
- budgetTracker.setStoreName(storeName);
210
- budgetTracker.setProductName(productName);
211
- budgetTracker.setProductType(productType);
212
- budgetTracker.setPrice(price);
213
- BudgetTrackerViewModel.updateBudgetTracker(budgetTracker);
214
- // Todo Automatic search after updated an item
215
- finish();
216
- Intent nbtToPfIntent = new Intent(NewBudgetTracker.this, NewBudgetTracker.class);
217
- nbtToPfIntent.putExtra(NBT_TO_PF, 0);
218
- startActivity(nbtToPfIntent);
219
- }
220
- }
221
-
222
- }
223
- });
224
190
  ```

2

ProductFragment編集

2022/06/17 13:29

投稿

Yakusugi
Yakusugi

score98

test CHANGED
File without changes
test CHANGED
@@ -38,7 +38,7 @@
38
38
  ```ここに言語を入力
39
39
  package com.myproject.offlinebudgettrackerappproject;
40
40
 
41
- <import部分は省略>
41
+ import省略
42
42
 
43
43
  /**
44
44
  * A simple {@link Fragment} subclass.
@@ -55,6 +55,8 @@
55
55
  private List<BudgetTracker> budgetTracker;
56
56
  List<BudgetTracker> viewModelProductNameLists;
57
57
  ActivityMainBinding activityMainBinding;
58
+ private int newBudgetTrackerIntentId = 0;
59
+ String productType;
58
60
 
59
61
  // TODO: Rename parameter arguments, choose names that match
60
62
  // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
@@ -115,6 +117,8 @@
115
117
  productListViewAdapter = new ProductListViewAdapter(getActivity(), 1);
116
118
  productListView.setAdapter(productListViewAdapter);
117
119
 
120
+
121
+
118
122
  productSearchQueryBtn.setOnClickListener(new View.OnClickListener() {
119
123
  @Override
120
124
  public void onClick(View view) {
@@ -123,7 +127,7 @@
123
127
  // 2022/02/11 追加
124
128
  ProductListViewAdapter productListViewAdapter;
125
129
 
126
- String productType = enterProductTypeForQuery.getText().toString();
130
+ productType = enterProductTypeForQuery.getText().toString();
127
131
  budgetTrackerViewModel = new ViewModelProvider(requireActivity()).get(BudgetTrackerViewModel.class);
128
132
 
129
133
  budgetTracker = new BudgetTracker();
@@ -148,14 +152,6 @@
148
152
  Intent productFragmentIntent = new Intent(getActivity(), NewBudgetTracker.class);
149
153
  productFragmentIntent.putExtra(PRODUCT_FRAGMENT_ID, productItemId.getId());
150
154
  startActivity(productFragmentIntent);
151
-
152
-
153
- viewModelProductNameLists = budgetTrackerViewModel.getProductTypeLists(productType);
154
- ProductListViewAdapter productListViewAdapter;
155
- productListViewAdapter = new ProductListViewAdapter(getActivity(), viewModelProductNameLists);
156
- productListViewAdapter.notifyDataSetChanged();
157
- productListView.setAdapter(productListViewAdapter);
158
-
159
155
  }
160
156
  });
161
157
 
@@ -164,6 +160,19 @@
164
160
  }
165
161
 
166
162
  });
163
+
164
+ // Get intent from NewBudgetTracker
165
+ Intent nbtToPfIntent = getActivity().getIntent();
166
+ Bundle nbtToPfGetIntentBundle = nbtToPfIntent.getExtras();
167
+ if (nbtToPfGetIntentBundle != null) {
168
+ newBudgetTrackerIntentId = getActivity().getIntent().getIntExtra(NewBudgetTracker.NBT_TO_PF, 0);
169
+ //observe only works when using LiveData
170
+ viewModelProductNameLists = budgetTrackerViewModel.getProductTypeLists(productType);
171
+ ProductListViewAdapter productListViewAdapter;
172
+ productListViewAdapter = new ProductListViewAdapter(getActivity(), viewModelProductNameLists);
173
+ productListViewAdapter.notifyDataSetChanged();
174
+ productListView.setAdapter(productListViewAdapter);
175
+ }
167
176
 
168
177
  return view;
169
178
  }

1

NewBudgetTrackerの一部内容を記載

2022/06/17 13:21

投稿

Yakusugi
Yakusugi

score98

test CHANGED
File without changes
test CHANGED
@@ -169,64 +169,47 @@
169
169
  }
170
170
  }
171
171
  ```
172
- ProductListViewAdapter.java
172
+ NewBudgetTracker.java
173
173
  ```ここに言語を入力
174
- package com.myproject.offlinebudgettrackerappproject.adapter;
175
-
176
- <import部分は省略>
177
-
178
- public class ProductListViewAdapter extends ArrayAdapter<BudgetTracker> {
179
-
180
- private static final Object INVALID_POSITION = -1;
181
- private LiveData<List<BudgetTracker>> budgetTrackerList;
182
- private Context context;
174
+ //Update button
183
- private View.OnClickListener listener;
175
+ updateButton.setOnClickListener(new View.OnClickListener() {
184
-
176
+ @Override
177
+ public void onClick(View view) {
185
- public ProductListViewAdapter(Context context, List<BudgetTracker> budgetTrackerList) {
178
+ if (shopFragmentIntentId != 0 || productFragmentIntentId != 0 || dateFragmentIntentId != 0) {
179
+ int idStore = shopFragmentIntentId;
180
+ int idProduct = productFragmentIntentId;
181
+ int idDate = dateFragmentIntentId;
182
+ String date = enterDate.getText().toString();
183
+ String storeName = enterStoreName.getText().toString();
184
+ String productName = enterProductName.getText().toString();
185
+ String productType = enterProductType.getText().toString();
186
+ int price = Integer.parseInt(enterPrice.getText().toString());
187
+
188
+ if (TextUtils.isEmpty(date) || TextUtils.isEmpty(storeName) || TextUtils.isEmpty(productName) || TextUtils.isEmpty(productType) || TextUtils.isEmpty(String.valueOf(price))) {
189
+ Snackbar.make(enterProductName, R.string.empty, Snackbar.LENGTH_SHORT).show();
190
+ } else {
186
- super(context, R.layout.product_list_item, budgetTrackerList);
191
+ BudgetTracker budgetTracker = new BudgetTracker();
192
+ if (shopFragmentIntentId != 0) {
193
+ budgetTracker.setId(idStore);
194
+ } else if (productFragmentIntentId != 0) {
195
+ budgetTracker.setId(idProduct);
196
+ } else if (dateFragmentIntentId != 0) {
197
+ budgetTracker.setId(idDate);
187
- }
198
+ }
188
-
199
+ budgetTracker.setDate(date);
200
+ budgetTracker.setStoreName(storeName);
201
+ budgetTracker.setProductName(productName);
202
+ budgetTracker.setProductType(productType);
203
+ budgetTracker.setPrice(price);
204
+ BudgetTrackerViewModel.updateBudgetTracker(budgetTracker);
189
- //TODO Someday, I need to adapt LiveData data transactions instead of the traditional List data transactions.
205
+ // Todo Automatic search after updated an item
190
- public ProductListViewAdapter(Context context, LiveData<List<BudgetTracker>> budgetTrackerList) {
206
+ finish();
191
- super(context, R.layout.product_list_item, (List<BudgetTracker>) budgetTrackerList);
207
+ Intent nbtToPfIntent = new Intent(NewBudgetTracker.this, NewBudgetTracker.class);
208
+ nbtToPfIntent.putExtra(NBT_TO_PF, 0);
209
+ startActivity(nbtToPfIntent);
192
- }
210
+ }
193
-
194
- public ProductListViewAdapter(Context context, int num) {
195
- super(context, num);
196
- }
211
+ }
197
-
198
- //When tapped
212
+
199
- public void setOnItemClickListener(View.OnClickListener listener) {
200
- this.listener = listener;
201
- }
213
+ }
202
-
203
- @NonNull
204
- @Override
205
- public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
206
-
207
- BudgetTracker budgetTracker = getItem(position);
208
-
209
-
210
- if (convertView == null) {
211
- convertView = LayoutInflater.from(getContext()).inflate(R.layout.product_list_item, parent, false);
212
- }
214
+ });
213
-
214
- ImageView productImageViewRow = convertView.findViewById(R.id.product_circle_image_view);
215
- TextView productStoreNameRow = convertView.findViewById(R.id.product_store_name_text_row);
216
- TextView productDateRow = convertView.findViewById(R.id.product_date_text_row);
217
- TextView productProductNameRow = convertView.findViewById(R.id.product_product_name_text_row);
218
- TextView productProductTypeRow = convertView.findViewById(R.id.product_product_type_text_row);
219
- TextView productPriceRow = convertView.findViewById(R.id.product_price_text_row);
220
-
221
- productImageViewRow.setImageResource(R.drawable.products);
222
- productStoreNameRow.setText(budgetTracker.getStoreName());
223
- productDateRow.setText(budgetTracker.getDate());
224
- productProductNameRow.setText(budgetTracker.getProductName());
225
- productProductTypeRow.setText(budgetTracker.getProductType());
226
- productPriceRow.setText(String.valueOf(budgetTracker.getPrice()));
227
-
228
- return convertView;
229
- }
230
-
231
- }
232
- ```
215
+ ```