回答編集履歴

6

xml修正

2022/11/06 04:58

投稿

jimbe
jimbe

スコア12623

test CHANGED
@@ -113,8 +113,8 @@
113
113
  xmlns:app="http://schemas.android.com/apk/res-auto"
114
114
  tools:context=".MainActivity"
115
115
  android:id="@+id/recyclerView"
116
- android:layout_width="wrap_content"
116
+ android:layout_width="match_parent"
117
- android:layout_height="wrap_content"
117
+ android:layout_height="match_parent"
118
118
  app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
119
119
  ```
120
120
  res/layout/item.xml

5

コード修正

2022/11/01 12:21

投稿

jimbe
jimbe

スコア12623

test CHANGED
@@ -47,6 +47,7 @@
47
47
  super(itemView);
48
48
  editText = itemView.findViewById(R.id.editText);
49
49
  editText.setOnEditorActionListener((v, actionId, event) -> {
50
+ if(actionId != EditorInfo.IME_ACTION_NEXT) return false;
50
51
  Item item = (Item)editText.getTag();
51
52
  String text = editText.getText().toString();
52
53
  Log.d("Adapter **", "OnEditorActionListener. orig=" + item + ", new text=" + text);
@@ -54,6 +55,7 @@
54
55
  return true;
55
56
  });
56
57
  editText.setOnFocusChangeListener((v, hasFocus) -> {
58
+ if(hasFocus) return;
57
59
  Item item = (Item)editText.getTag();
58
60
  String text = editText.getText().toString();
59
61
  Log.d("Adapter **", "OnFocusChangeListener. orig=" + item + ", new text=" + text);

4

コード修正

2022/11/01 11:24

投稿

jimbe
jimbe

スコア12623

test CHANGED
@@ -92,7 +92,7 @@
92
92
  public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
93
93
  Item item = itemList.get(position);
94
94
  holder.editText.setTag(item);
95
- holder.editText.setText(itemList.get(position).text);
95
+ holder.editText.setText(item.text);
96
96
  }
97
97
 
98
98
  @Override

3

コード修正

2022/11/01 11:10

投稿

jimbe
jimbe

スコア12623

test CHANGED
@@ -91,8 +91,8 @@
91
91
  @Override
92
92
  public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
93
93
  Item item = itemList.get(position);
94
+ holder.editText.setTag(item);
94
95
  holder.editText.setText(itemList.get(position).text);
95
- holder.editText.setTag(item);
96
96
  }
97
97
 
98
98
  @Override

2

コード修正

2022/11/01 11:08

投稿

jimbe
jimbe

スコア12623

test CHANGED
@@ -56,7 +56,7 @@
56
56
  editText.setOnFocusChangeListener((v, hasFocus) -> {
57
57
  Item item = (Item)editText.getTag();
58
58
  String text = editText.getText().toString();
59
- Log.d("Adapter **", "setOnFocusChangeListener. orig=" + item + ", new text=" + text);
59
+ Log.d("Adapter **", "OnFocusChangeListener. orig=" + item + ", new text=" + text);
60
60
  update(item, text);
61
61
  });
62
62
  }

1

コード追加

2022/11/01 11:06

投稿

jimbe
jimbe

スコア12623

test CHANGED
@@ -1,2 +1,126 @@
1
1
  EditText の更新を DB に反映するということだけから見れば、更新のトリガが発生したら実際に更新されていようがいまいが更新をかけるのでは無く、 EditText の値が実際に更新されていた場合にのみ DB を更新するようにすれば、そのチェックのトリガは連続して何度あったとしても更新は一回で済むのではないでしょうか。
2
2
  もし更新回数をカウントアップしているということであれば、これも実際に更新した時だけカウントアップすれば、問題無いように思います。
3
+
4
+ 以下 java ですが、アクション、フォーカスが発生しても、更新されている場合のみ UPDATE が出ると思います。
5
+
6
+ MainActivity.java
7
+ ```java
8
+ import androidx.annotation.NonNull;
9
+ import androidx.appcompat.app.AppCompatActivity;
10
+ import androidx.recyclerview.widget.RecyclerView;
11
+
12
+ import android.os.Bundle;
13
+ import android.util.Log;
14
+ import android.view.*;
15
+ import android.widget.EditText;
16
+
17
+ import java.util.*;
18
+
19
+ public class MainActivity extends AppCompatActivity {
20
+
21
+ @Override
22
+ protected void onCreate(Bundle savedInstanceState) {
23
+ super.onCreate(savedInstanceState);
24
+ setContentView(R.layout.activity_main);
25
+
26
+ RecyclerView recyclerView = findViewById(R.id.recyclerView);
27
+ recyclerView.setAdapter(new Adapter());
28
+ }
29
+
30
+ private static class Item {
31
+ final long id;
32
+ final String text;
33
+ Item(long id, String text) {
34
+ this.id = id;
35
+ this.text = text;
36
+ }
37
+ @Override
38
+ public String toString() {
39
+ return super.toString() + "[id=" + id + ", text='" + text + "']";
40
+ }
41
+ }
42
+
43
+ private static class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
44
+ class ViewHolder extends RecyclerView.ViewHolder {
45
+ final EditText editText;
46
+ public ViewHolder(@NonNull View itemView) {
47
+ super(itemView);
48
+ editText = itemView.findViewById(R.id.editText);
49
+ editText.setOnEditorActionListener((v, actionId, event) -> {
50
+ Item item = (Item)editText.getTag();
51
+ String text = editText.getText().toString();
52
+ Log.d("Adapter **", "OnEditorActionListener. orig=" + item + ", new text=" + text);
53
+ update(item, text);
54
+ return true;
55
+ });
56
+ editText.setOnFocusChangeListener((v, hasFocus) -> {
57
+ Item item = (Item)editText.getTag();
58
+ String text = editText.getText().toString();
59
+ Log.d("Adapter **", "setOnFocusChangeListener. orig=" + item + ", new text=" + text);
60
+ update(item, text);
61
+ });
62
+ }
63
+ }
64
+
65
+ private List<Item> itemList = new ArrayList<>();
66
+
67
+ Adapter() {
68
+ //テストデータ
69
+ itemList.add(new Item(1, "ABC"));
70
+ itemList.add(new Item(2, "DEF"));
71
+ itemList.add(new Item(3, "GHI"));
72
+ itemList.add(new Item(4, "JKL"));
73
+ }
74
+
75
+ private void update(Item item, String text) {
76
+ if(item.text.equals(text)) return;
77
+
78
+ Log.d("Adapter **", "UPDATE table (text) VALUES ('" + text + "') WHERE id=" + item.id);
79
+
80
+ int index = itemList.indexOf(item);
81
+ itemList.set(index, new Item(item.id, text));
82
+ notifyItemChanged(index);
83
+ }
84
+
85
+ @NonNull
86
+ @Override
87
+ public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
88
+ return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false));
89
+ }
90
+
91
+ @Override
92
+ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
93
+ Item item = itemList.get(position);
94
+ holder.editText.setText(itemList.get(position).text);
95
+ holder.editText.setTag(item);
96
+ }
97
+
98
+ @Override
99
+ public int getItemCount() {
100
+ return itemList.size();
101
+ }
102
+ }
103
+ }
104
+ ```
105
+ res/layout/activity_main.xml
106
+ ```xml
107
+ <?xml version="1.0" encoding="utf-8"?>
108
+ <androidx.recyclerview.widget.RecyclerView
109
+ xmlns:android="http://schemas.android.com/apk/res/android"
110
+ xmlns:tools="http://schemas.android.com/tools"
111
+ xmlns:app="http://schemas.android.com/apk/res-auto"
112
+ tools:context=".MainActivity"
113
+ android:id="@+id/recyclerView"
114
+ android:layout_width="wrap_content"
115
+ android:layout_height="wrap_content"
116
+ app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
117
+ ```
118
+ res/layout/item.xml
119
+ ```xml
120
+ <?xml version="1.0" encoding="utf-8"?>
121
+ <EditText
122
+ xmlns:android="http://schemas.android.com/apk/res/android"
123
+ android:id="@+id/editText"
124
+ android:layout_width="match_parent"
125
+ android:layout_height="wrap_content" />
126
+ ```