回答編集履歴

4

コメント部分修正

2022/03/09 11:50

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -50,7 +50,7 @@
50
50
 
51
51
  public void addNote(Note note){
52
52
  //データベースに note の内容を追加
53
- //new DatabaseHelper(this).addData(note.title, note.text);
53
+ //_helper.addData(note.title, note.text) ?
54
54
 
55
55
  //アダプタにも追加(notify は addNode 内で行われるのでココではそれは意識しない)
56
56
  adapter.addNote(note);

3

コード変更

2022/03/09 11:32

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -5,19 +5,40 @@
5
5
  「ダイアログを表示したアクティビティのメソッドを実行する」というのであれば、「ダイアログを表示したアクティビティ」を得て、メソッドを呼ぶ必要があります。
6
6
  そしてそれは既にコード内で行っています。
7
7
 
8
+ ※データベース関係は省いています。
9
+ ※クラス名 (Adapter 関係)やメソッド名・役割(ダイアログからのコールバック)を少し変えています
10
+
8
11
  MainActivity.java
9
12
  ```java
10
13
  import android.os.Bundle;
11
- import android.util.Log;
14
+ import android.view.*;
12
- import android.widget.Button;
15
+ import android.widget.*;
13
16
 
17
+ import androidx.annotation.NonNull;
14
18
  import androidx.appcompat.app.AppCompatActivity;
19
+ import androidx.recyclerview.widget.*;
20
+
21
+ import java.util.*;
15
22
 
16
23
  public class MainActivity extends AppCompatActivity {
24
+ private NoteListAdapter adapter;
25
+
17
26
  @Override
18
27
  protected void onCreate(Bundle savedInstanceState) {
19
28
  super.onCreate(savedInstanceState);
20
29
  setContentView(R.layout.activity_main);
30
+
31
+ //RecyclerViewの設定。
32
+ RecyclerView rvNote = findViewById(R.id.rvNote);
33
+ rvNote.setLayoutManager(new LinearLayoutManager(MainActivity.this));
34
+ adapter = new NoteListAdapter();
35
+ rvNote.setAdapter(adapter);
36
+
37
+ //データベースからデータを読み込みアダプタにセット
38
+ //(省略)
39
+ //仮に直接追加
40
+ adapter.addNote(new Note("111", "AAA"));
41
+ adapter.addNote(new Note("222", "BBB"));
21
42
 
22
43
  //fabAddを押したときの処理。
23
44
  Button fabAdd = findViewById(R.id.fabAdd);
@@ -26,9 +47,54 @@
26
47
  fragment.show(getSupportFragmentManager(),"CreatesNotesDialogFragment");
27
48
  });
28
49
  }
29
- //エラー文に記載されている箇所。
50
+
30
- public void notifyAboutRecyclerView(){
51
+ public void addNote(Note note){
52
+ //データベースに note の内容を追加
53
+ //new DatabaseHelper(this).addData(note.title, note.text);
54
+
55
+ //アダプタにも追加(notify は addNode 内で行われるのでココではそれは意識しない)
56
+ adapter.addNote(note);
57
+ }
58
+
59
+ //RecyclerViewのアダプタ
60
+ public class NoteListAdapter extends RecyclerView.Adapter<NoteListAdapter.NoteViewHolder> {
31
- Log.d("MainActivity","called notifyAboutRecyclerView()");
61
+ private final List<Note> noteList = new ArrayList<>();
62
+
63
+ void addNote(Note note) {
64
+ int position = noteList.size();
65
+ noteList.add(note);
66
+ notifyItemInserted(position);
67
+ }
68
+
69
+ @NonNull
70
+ @Override
71
+ public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
72
+ View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_list_layout, parent, false);
73
+ return new NoteViewHolder(view);
74
+ }
75
+
76
+ @Override
77
+ public void onBindViewHolder(NoteViewHolder holder, int position) {
78
+ Note note = noteList.get(position);
79
+ holder.tvTitle.setText(note.title);
80
+ holder.tvText.setText(note.text);
81
+ }
82
+
83
+ @Override
84
+ public int getItemCount() {
85
+ return noteList.size();
86
+ }
87
+
88
+ public class NoteViewHolder extends RecyclerView.ViewHolder {
89
+ final TextView tvTitle;
90
+ final TextView tvText;
91
+
92
+ public NoteViewHolder(View itemView) {
93
+ super(itemView);
94
+ tvTitle = itemView.findViewById(R.id.tvNoteTitle);
95
+ tvText = itemView.findViewById(R.id.tvNoteText);
96
+ }
97
+ }
32
98
  }
33
99
  }
34
100
  ```
@@ -51,21 +117,31 @@
51
117
  View customView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_layout, null);
52
118
  return new AlertDialog.Builder(getContext())
53
119
  .setView(customView)
120
+ .setTitle("ノート追加")
54
- .setPositiveButton("keep", (dialogInterface, i) -> {
121
+ .setPositiveButton("add", (dialogInterface, i) -> {
55
122
  EditText etNoteTitle = customView.findViewById(R.id.etSetNoteTitle);
56
123
  String noteTitle = etNoteTitle.getText().toString();
57
124
  EditText etNoteText = customView.findViewById(R.id.etSetNoteText);
58
125
  String noteText = etNoteText.getText().toString();
59
- //new DatabaseHelper(getContext()).addData(noteTitle, noteText);
126
+ //ダイアログはあくまでデータを入力することだけを行う.
127
+ //(データベースを気にする必要は無くなる.)
60
- ((MainActivity)requireActivity()).notifyAboutRecyclerView();
128
+ ((MainActivity)requireActivity()).addNote(new Note(noteTitle, noteText));
61
129
  })
62
- .setNegativeButton("cxl", (dialogInterface, i) -> {
130
+ .setNegativeButton("cancel", (dialogInterface, i) -> {
63
131
  })
64
132
  .create();
65
133
  }
66
134
  }
67
135
  ```
68
- ダイアログ keep 押下時
136
+ Note.java
69
- ```plain
137
+ ```java
138
+ public class Note {
139
+ final String title;
140
+ final String text;
141
+
70
- D/MainActivity: called notifyAboutRecyclerView()
142
+ Note(String title, String text) {
143
+ this.title = title;
144
+ this.text = text;
145
+ }
146
+ }
71
147
  ```

2

誤字

2022/03/09 07:41

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -2,7 +2,7 @@
2
2
  new MainActivity().notifyAboutRecyclerView();
3
3
  というのは意味がありません。
4
4
  オブジェクトはただ作れば良いものではありません。クラスは同じでもそこから作ったオブジェクトは別々の存在であり、例えば車の設計図は同じでもそれを元に作った複数の車は別々の存在であることと同じです。
5
- 「ダイアログを表示したアクティビティのメソッドを実行する」というのであれば、「ダイアログ表示したアクティビティ」を得て、メソッドを呼ぶ必要があります。
5
+ 「ダイアログを表示したアクティビティのメソッドを実行する」というのであれば、「ダイアログ表示したアクティビティ」を得て、メソッドを呼ぶ必要があります。
6
6
  そしてそれは既にコード内で行っています。
7
7
 
8
8
  MainActivity.java

1

コード追加

2022/03/09 07:39

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -4,3 +4,68 @@
4
4
  オブジェクトはただ作れば良いものではありません。クラスは同じでもそこから作ったオブジェクトは別々の存在であり、例えば車の設計図は同じでもそれを元に作った複数の車は別々の存在であることと同じです。
5
5
  「ダイアログを表示したアクティビティのメソッドを実行する」というのであれば、「ダイアログほ表示したアクティビティ」を得て、メソッドを呼ぶ必要があります。
6
6
  そしてそれは既にコード内で行っています。
7
+
8
+ MainActivity.java
9
+ ```java
10
+ import android.os.Bundle;
11
+ import android.util.Log;
12
+ import android.widget.Button;
13
+
14
+ import androidx.appcompat.app.AppCompatActivity;
15
+
16
+ public class MainActivity extends AppCompatActivity {
17
+ @Override
18
+ protected void onCreate(Bundle savedInstanceState) {
19
+ super.onCreate(savedInstanceState);
20
+ setContentView(R.layout.activity_main);
21
+
22
+ //fabAddを押したときの処理。
23
+ Button fabAdd = findViewById(R.id.fabAdd);
24
+ fabAdd.setOnClickListener(view -> {
25
+ CreatesNotesDialogFragment fragment = new CreatesNotesDialogFragment();
26
+ fragment.show(getSupportFragmentManager(),"CreatesNotesDialogFragment");
27
+ });
28
+ }
29
+ //エラー文に記載されている箇所。
30
+ public void notifyAboutRecyclerView(){
31
+ Log.d("MainActivity","called notifyAboutRecyclerView()");
32
+ }
33
+ }
34
+ ```
35
+ CreatesNotesDialogFragment.java
36
+ ```java
37
+ import android.app.AlertDialog;
38
+ import android.app.Dialog;
39
+ import android.os.Bundle;
40
+ import android.view.LayoutInflater;
41
+ import android.view.View;
42
+ import android.widget.EditText;
43
+
44
+ import androidx.annotation.NonNull;
45
+ import androidx.fragment.app.DialogFragment;
46
+
47
+ public class CreatesNotesDialogFragment extends DialogFragment {
48
+ @NonNull
49
+ @Override
50
+ public Dialog onCreateDialog(Bundle savedInstance) {
51
+ View customView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_layout, null);
52
+ return new AlertDialog.Builder(getContext())
53
+ .setView(customView)
54
+ .setPositiveButton("keep", (dialogInterface, i) -> {
55
+ EditText etNoteTitle = customView.findViewById(R.id.etSetNoteTitle);
56
+ String noteTitle = etNoteTitle.getText().toString();
57
+ EditText etNoteText = customView.findViewById(R.id.etSetNoteText);
58
+ String noteText = etNoteText.getText().toString();
59
+ //new DatabaseHelper(getContext()).addData(noteTitle, noteText);
60
+ ((MainActivity)requireActivity()).notifyAboutRecyclerView();
61
+ })
62
+ .setNegativeButton("cxl", (dialogInterface, i) -> {
63
+ })
64
+ .create();
65
+ }
66
+ }
67
+ ```
68
+ ダイアログ keep 押下時
69
+ ```plain
70
+ D/MainActivity: called notifyAboutRecyclerView()
71
+ ```