前提・実現したいこと
ListViewの表示アイテムにEditTextを含めたいのですが、入力後にENTERキーで確定させないでListViewをスクロールさせるとEditTextへの入力内容が元に戻ってしまいます。
ListViewのアイテムをListItemをいうクラスにして、ListItem型のデータとListViewはAdapterで接続しています。
発生している問題
ListItemのデータを書き換えができれば、表示内容は保持されることは分かりましたが、ENTERキーを押して確定するという動作が必要になってしまっています。文字を入力した段階でデータとして更新する方法が見つかりません。
該当のソースコード
java
1(ListItem.java) 2public class ListItem { 3 private int id; 4 private String value; 5 6 public int getId() { return this.id; } 7 public void setId(int id) { this.id = id; } 8 public String getValue() { return this.value; } 9 public void setValue(String value) { this.value = value; } 10} 11 12(MainActivity.java) 13public class MainActivity extends AppCompatActivity { 14 15 MyListAdapter adapter; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 this.setContentView(layout.activity_main); 21 22 ArrayList<ListItem> data = new ArrayList<>(); 23 for (int i = 0; i < 20; i++) { 24 ListItem item = new ListItem(); 25 item.setValue(Integer.toString(i)); 26 data.add(item); 27 } 28 this.adapter = new MyListAdapter(this, data, layout.list_item); 29 ListView list = (ListView) this.findViewById(id.list); 30 list.setAdapter(this.adapter); 31 } 32} 33 34(MyListAdapter.java) 35public class MyListAdapter extends BaseAdapter { 36 private Context context; 37 private ArrayList<ListItem> data; 38 private int resource; 39 private final LayoutInflater inflater; 40 41 static class ViewHolder { 42 EditText editText; 43 } 44 45 public int getCount() { 46 return this.data.size(); 47 } 48 49 public Object getItem(int position) { 50 return this.data.get(position); 51 } 52 53 public long getItemId(int position) { 54 return this.data.get(position).getId(); 55 } 56 57 public MyListAdapter(Context context, ArrayList<ListItem> data, int resource) 58 { 59 this.context = context; 60 this.data = data; 61 this.resource = resource; 62 inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 63 } 64 65 public View getView(final int position, View convertView, final ViewGroup parent) { 66 Activity activity = (Activity) this.context; 67 final ListItem item = (ListItem) this.getItem(position); 68 final MyListAdapter.ViewHolder holder; 69 70 if (convertView == null) { 71 convertView = activity.getLayoutInflater().inflate(this.resource, null); 72 holder = new MyListAdapter.ViewHolder(); 73 holder.editText = (EditText) convertView.findViewById(id.editText); 74 convertView.setTag(holder); 75 } 76 else{ 77 holder = (MyListAdapter.ViewHolder)convertView.getTag(); 78 } 79 80 holder.editText.addTextChangedListener(new TextWatcher() { 81 @Override 82 public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 83 } 84 85 @Override 86 public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 87 } 88 89 @Override 90 public void afterTextChanged(Editable editable) { 91 /* 実験コード1 スクロール範囲外の行も一緒に変わってしまう */ 92 //item.setValue(editable.toString()); 93 } 94 }); 95 96 holder.editText.setOnKeyListener(new View.OnKeyListener() { 97 @Override 98 public boolean onKey(View view, int i, KeyEvent keyEvent) { 99 //実験コード2 ENTERキーで確定すれば希望通りとなる */ 100 String str = holder.editText.getText().toString(); 101 item.setValue(str); 102 return false; 103 } 104 }); 105 ((EditText) holder.editText).setText(item.getValue()); 106 107 return convertView; 108 } 109}
xml
1(list_item.xml) 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <RelativeLayout 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 android:layout_gravity="center_horizontal"> 11 12 <EditText 13 android:id="@+id/editText" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:layout_alignParentEnd="true" 17 android:layout_alignParentTop="true" 18 android:inputType="none" 19 android:digits="0123456789abcdefABCDEF" 20 android:maxLength="2"/> 21 </RelativeLayout> 22</LinearLayout> 23 24(activity_main.xml) 25<?xml version="1.0" encoding="utf-8"?> 26<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 27 xmlns:tools="http://schemas.android.com/tools" 28 android:layout_width="match_parent" 29 android:layout_height="match_parent" 30 android:paddingBottom="@dimen/activity_vertical_margin" 31 android:paddingLeft="@dimen/activity_horizontal_margin" 32 android:paddingRight="@dimen/activity_horizontal_margin" 33 android:paddingTop="@dimen/activity_vertical_margin" 34 tools:context="jp.oops.listmyadapter.MainActivity"> 35 36 <ListView 37 android:layout_width="fill_parent" 38 android:layout_height="fill_parent" 39 android:id="@+id/list" /> 40 41</RelativeLayout>
試したこと
前出の実験コード1~2のいずれを有効にしてみました。
実験コード2の箇所で、EditTextへ入力したデータを取得できれば、望む結果になりそうです。
補足情報(Android Studio 3.1.4 / windows10 Pro 64bit)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/04 00:57