質問するログイン新規登録

質問編集履歴

2

文法の修正

2019/01/06 07:23

投稿

Aies
Aies

スコア21

title CHANGED
File without changes
body CHANGED
@@ -193,7 +193,7 @@
193
193
 
194
194
  //表示月を取得
195
195
  public String getTitle(){
196
- SimpleDateFormat format = new SimpleDateFormat("yyyy.MM", Locale.US);
196
+ SimpleDateFormat format = new SimpleDateFormat("yyyy.MM", Locale.JAPAN);
197
197
  return format.format(mDateManager.mCalendar.getTime());
198
198
  }
199
199
 
@@ -221,4 +221,6 @@
221
221
 
222
222
  (MainActivity.java:33) は kDB = new KakeiboDatabase(getApplicationContext()); のところです。
223
223
 
224
+ readDataの呼び出し部分は上から三番目のソース内の「//その月日の金額をまとめる」というコメントがついてる部分です。
225
+
224
226
  調べてもいまいちわからなかったのでよろしければご助言お願いいたします。

1

データソースの追記

2019/01/06 07:23

投稿

Aies
Aies

スコア21

title CHANGED
File without changes
body CHANGED
@@ -102,7 +102,118 @@
102
102
  }
103
103
  }
104
104
  ```
105
+ ```AndroidStudio
106
+ public class CalendarAdapter extends BaseAdapter {
107
+ private List<Date> dateArray = new ArrayList();
108
+ private Context mContext;
109
+ private DateManager mDateManager;
110
+ private LayoutInflater mLayoutInflater;
111
+ private String price;
112
+ private SQLiteDatabase db;
113
+ private KakeiboDatabase kDB;
105
114
 
115
+ //カスタムセルを拡張したらここでWigetを定義
116
+ private static class ViewHolder {
117
+ public TextView dateText;
118
+ }
119
+
120
+ public CalendarAdapter(Context context){
121
+ mContext = context;
122
+ mLayoutInflater = LayoutInflater.from(mContext);
123
+ mDateManager = new DateManager();
124
+ dateArray = mDateManager.getDays();
125
+ }
126
+
127
+ @Override
128
+ public int getCount() {
129
+ return dateArray.size();
130
+ }
131
+
132
+ @Override
133
+ public View getView(int position, View convertView, ViewGroup parent) {
134
+ ViewHolder holder;
135
+ if (convertView == null) {
136
+ convertView = mLayoutInflater.inflate(R.layout.calendar_cell, null);
137
+ holder = new ViewHolder();
138
+ holder.dateText = convertView.findViewById(R.id.dateText);
139
+ convertView.setTag(holder);
140
+ } else {
141
+ holder = (ViewHolder)convertView.getTag();
142
+ }
143
+
144
+ //セルのサイズを指定
145
+ float dp = mContext.getResources().getDisplayMetrics().density;
146
+ AbsListView.LayoutParams params = new AbsListView.LayoutParams(parent.getWidth()/7 - (int)dp, (parent.getHeight() - (int)dp * mDateManager.getWeeks() ) / mDateManager.getWeeks());
147
+ convertView.setLayoutParams(params);
148
+
149
+ //その月日の金額をまとめる
150
+ SimpleDateFormat dateF = new SimpleDateFormat("yyyy-MM-dd", Locale.JAPAN);
151
+ MainActivity main = new MainActivity();
152
+ price = main.readData(dateF.format(dateArray.get(position)));
153
+
154
+ //日付とデータベースの金額を表示させる
155
+ SimpleDateFormat dateFormat = new SimpleDateFormat("d", Locale.JAPAN);
156
+ holder.dateText.setText(dateFormat.format(dateArray.get(position)) + "\n" + price);
157
+
158
+ //当月以外のセルをグレーアウト
159
+ if (mDateManager.isCurrentMonth(dateArray.get(position))){
160
+ convertView.setBackgroundColor(Color.WHITE);
161
+ }else {
162
+ convertView.setBackgroundColor(Color.LTGRAY);
163
+ }
164
+
165
+ //日曜日を赤、土曜日を青に
166
+ int colorId;
167
+ switch (mDateManager.getDayOfWeek(dateArray.get(position))){
168
+ case 1:
169
+ colorId = Color.RED;
170
+ break;
171
+ case 7:
172
+ colorId = Color.BLUE;
173
+ break;
174
+
175
+ default:
176
+ colorId = Color.BLACK;
177
+ break;
178
+ }
179
+ holder.dateText.setTextColor(colorId);
180
+
181
+ return convertView;
182
+ }
183
+
184
+ @Override
185
+ public long getItemId(int position) {
186
+ return 0;
187
+ }
188
+
189
+ @Override
190
+ public Object getItem(int position) {
191
+ return null;
192
+ }
193
+
194
+ //表示月を取得
195
+ public String getTitle(){
196
+ SimpleDateFormat format = new SimpleDateFormat("yyyy.MM", Locale.US);
197
+ return format.format(mDateManager.mCalendar.getTime());
198
+ }
199
+
200
+ //翌月表示
201
+ public void nextMonth(){
202
+ mDateManager.nextMonth();
203
+ dateArray = mDateManager.getDays();
204
+ this.notifyDataSetChanged();
205
+ }
206
+
207
+ //前月表示
208
+ public void prevMonth(){
209
+ mDateManager.prevMonth();
210
+ dateArray = mDateManager.getDays();
211
+ this.notifyDataSetChanged();
212
+ }
213
+ }
214
+
215
+ ```
216
+
106
217
  このMainActivityにあるreadDataを外部から呼び出したときに
107
218
  【java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
108
219
   at com.example.a162105.kakeibo.MainActivity.readData(MainActivity.java:33)】