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