前提・実現したいこと
初めて投稿させていただきます。よろしくお願いします。
Android Studioの勉強のため他のサイト様などを参考に、カレンダーUIのメモ帳を作成しています。
・カレンダーが開く
・カレンダーの日付を押すとメモ帳が開く
・メモ帳の保存ボタンを押すとデータベースにデータが保存され、カレンダー画面に戻る
こういったものを作ろうとしているのですが、データを保存した後に、どこの日付を押しても同じデータが読み込まれた状態で開かれてしまいます。一つ一つ日付毎にデータを開きたいのですがやり方が分からず詰まってしまっています。どのようにすれば一つ一つ開くことができるのでしょうか?
どうかご教授お願いいたします。
該当のソースコード
MainActivity.java
1public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,View.OnClickListener{ 2 3 private TextView titleText; 4 private Button prevButton, nextButton; 5 private CalendarAdapter mCalendarAdapter; 6 private GridView calendarGridView; 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 13 findViewById(R.id.hButton).setOnClickListener(this);//上ボタン用追加 14 findViewById(R.id.setButton).setOnClickListener(this);//上ボタン用追加 15 16 titleText = findViewById(R.id.titleText); 17 prevButton = findViewById(R.id.prevButton); 18 prevButton.setOnClickListener(new View.OnClickListener() { 19 @Override 20 public void onClick(View v) { 21 mCalendarAdapter.prevMonth(); 22 titleText.setText(mCalendarAdapter.getTitle()); 23 } 24 }); 25 nextButton = findViewById(R.id.nextButton); 26 nextButton.setOnClickListener(new View.OnClickListener() { 27 @Override 28 public void onClick(View v) { 29 mCalendarAdapter.nextMonth(); 30 titleText.setText(mCalendarAdapter.getTitle()); 31 } 32 }); 33 calendarGridView = findViewById(R.id.calendarGridView); 34 mCalendarAdapter = new CalendarAdapter(this); 35 calendarGridView.setAdapter(mCalendarAdapter); 36 titleText.setText(mCalendarAdapter.getTitle()); 37 38 calendarGridView.setOnItemClickListener(this); 39 } 40 41 @Override 42 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 43 Intent intent = new Intent(getApplication(), TextActivity.class); 44 intent.putExtra("date", mCalendarAdapter.getItem(position).toString()); 45 startActivity(intent); 46 } 47 48 @Override 49 public void onClick(View view){ 50 //ここに遷移するための処理を追加する 51 switch (view.getId()){ 52 case R.id.hButton: 53 Intent intent = new Intent(this,Main2Activity.class); 54 startActivity(intent); 55 break; 56 57 case R.id.setButton: 58 intent = new Intent(this,Main3Activity.class); 59 startActivity(intent); 60 break; 61 } 62 63 /*Intent intent = new Intent(this, Main2Activity.class); 64 startActivity(intent); 65 */ 66 }
TextActivity.java
1public class TextActivity extends AppCompatActivity { 2 3 private String currentDate; 4 private TextView tex; 5 private SQLiteDatabase db; 6 private TextOpenHelper helper = null; 7 private EditText weightText, bodyText; 8 boolean newFlag = false; 9 String id = ""; 10 String youbi; 11 String month; 12 String day; 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_text); 18 19 //データベース作成 20 weightText = findViewById(R.id.weightText); 21 bodyText = findViewById(R.id.body); 22 tex = findViewById(R.id.tex); 23 24 if (helper == null) { 25 helper = new TextOpenHelper(TextActivity.this); 26 } 27 Intent intent = getIntent(); 28 currentDate = intent.getStringExtra("date"); 29 youbi = currentDate.substring(0,3); 30 month = currentDate.substring(4,7); 31 day = currentDate.substring(8,10); 32 tex.setText(month+","+day+","+youbi); 33 id = intent.getStringExtra("id"); 34 if (month ==("") && day == ("") && id == ("")) { 35 newFlag = true; 36 } else { 37 db = helper.getWritableDatabase(); 38 try { 39 // rawQueryというSELECT専用メソッドを使用してデータを取得する 40 Cursor c = db.rawQuery("select id, weight, body from text_table", null); 41 // Cursorの先頭行があるかどうか確認 42 boolean next = c.moveToFirst(); 43 // 取得した全ての行を取得 44 while (next) { 45 // 取得したカラムの順番(0から始まる)と型を指定してデータを取得する 46 Double dispWeight = c.getDouble(1); 47 String dispBody = c.getString(2); 48 //EditText body = (EditText)findViewById(R.id.body); 49 weightText.setText(String.valueOf(dispWeight)); 50 bodyText.setText(dispBody, TextView.BufferType.NORMAL); 51 next = c.moveToNext(); 52 } 53 } finally { 54 } 55 } 56 57 58 //セーブボタン 59 Button saveButton = findViewById(R.id.save); 60 saveButton.setOnClickListener(new View.OnClickListener() { 61 @Override 62 public void onClick(View v) { 63 if (helper == null) { 64 helper = new TextOpenHelper(getApplicationContext()); 65 } 66 if (db == null) { 67 db = helper.getWritableDatabase(); 68 } 69 70 String weight = weightText.getText().toString(); 71 String body = bodyText.getText().toString(); 72 73 insertData(db, month, day, Double.valueOf(weight), body); 74 finish(); 75 76 } 77 }); 78 } 79 80 //データベースinsertData 81 private void insertData(SQLiteDatabase db,String month, String day, Double weight, String body) { 82 83 ContentValues iValues = new ContentValues(); 84 iValues.put("month",month); 85 iValues.put("day",day); 86 iValues.put("weight", weight); 87 iValues.put("body", body); 88 db.insert("text_table", null, iValues); 89 90 } 91 //データベースinsertData 92}
CalenderAdapter.java
1public class CalendarAdapter extends BaseAdapter { 2 private List<Date> dateArray = new ArrayList(); 3 private Context mContext; 4 private DateManager mDateManager; 5 private LayoutInflater mLayoutInflater; 6 7 //カスタムセルを拡張したらここでWidgetを定義 8 private static class ViewHolder { 9 public TextView dateText; 10 } 11 12 public CalendarAdapter(Context context){ 13 mContext = context; 14 mLayoutInflater = LayoutInflater.from(mContext); 15 mDateManager = new DateManager(); 16 dateArray = mDateManager.getDays(); 17 } 18 19 @Override 20 public int getCount() { 21 return dateArray.size(); 22 } 23 24 @Override 25 public View getView(int position, View convertView, ViewGroup parent) { 26 ViewHolder holder; 27 if (convertView == null) { 28 convertView = mLayoutInflater.inflate(R.layout.calendar_cell, null); 29 holder = new ViewHolder(); 30 holder.dateText = convertView.findViewById(R.id.dateText); 31 convertView.setTag(holder); 32 } else { 33 holder = (ViewHolder)convertView.getTag(); 34 } 35 36 //セルのサイズを指定 37 float dp = mContext.getResources().getDisplayMetrics().density; 38 AbsListView.LayoutParams params = new AbsListView.LayoutParams(parent.getWidth()/7 - (int)dp, (parent.getHeight() - (int)dp * mDateManager.getWeeks() ) / mDateManager.getWeeks()); 39 convertView.setLayoutParams(params); 40 41 //日付のみ表示させる 42 SimpleDateFormat dateFormat = new SimpleDateFormat("d", Locale.US); 43 holder.dateText.setText(dateFormat.format(dateArray.get(position))); 44 45 //当月以外のセルをグレーアウト 46 if (mDateManager.isCurrentMonth(dateArray.get(position))){ 47 convertView.setBackgroundColor(Color.WHITE); 48 }else { 49 convertView.setBackgroundColor(Color.GRAY); 50 } 51 52 //当日の背景をライトグレーに 53 if (mDateManager.isToday(dateArray.get(position))) { 54 convertView.setBackgroundColor(Color.LTGRAY); 55 } 56 57 //日曜日を赤、土曜日を青に 58 int colorId; 59 switch (mDateManager.getDayOfWeek(dateArray.get(position))){ 60 case 1: 61 colorId = Color.RED; 62 break; 63 case 7: 64 colorId = Color.BLUE; 65 break; 66 67 default: 68 colorId = Color.BLACK; 69 break; 70 } 71 holder.dateText.setTextColor(colorId); 72 73 return convertView; 74 } 75 76 @Override 77 public long getItemId(int position) { 78 return position; 79 } 80 81 @Override 82 public Object getItem(int position) { 83 return dateArray.get(position); 84 } 85 86 //表示月を取得 87 public String getTitle(){ 88 SimpleDateFormat format = new SimpleDateFormat("yyyy.MM", Locale.US); 89 return format.format(mDateManager.mCalendar.getTime()); 90 } 91 92 //翌月表示 93 public void nextMonth(){ 94 mDateManager.nextMonth(); 95 dateArray = mDateManager.getDays(); 96 this.notifyDataSetChanged(); 97 } 98 99 //前月表示 100 public void prevMonth(){ 101 mDateManager.prevMonth(); 102 dateArray = mDateManager.getDays(); 103 this.notifyDataSetChanged(); 104 }
補足情報(FW/ツールのバージョンなど)
Android Studio3.1.2
データベースはSQLiteを使用しています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/04 04:22
2019/02/04 07:05
2019/02/04 18:20
2019/02/04 18:49 編集
2019/02/04 19:19
2019/02/05 01:30 編集
2019/02/05 04:32
2019/02/05 11:34
2019/02/05 11:49