ListView内のアダプターを使用したEditTextの処理について困っています。
ご回答よろしくお願いします。
###実現したいこと
EditTextの内容を変更しても表示が消えないこと。変更が反映されること。
そのために、編集したEditTextの内容を適切なItemクラスに反映させること。
###問題点
新しくEditTextを作成し、その中身を編集し、キーボードを消したときに編集内容が消えてしまいます。
これはアダプターにセットしたListが更新されていないからだと思っています。
具体的には以下のような動作をします。
1.初期画面 2.項目を追加するボタンを押下した後
3.編集 4.キーボードを閉じた後(編集画像の左下ボタンを押した後)
###試したこと
ItemListAdapterクラスのコメントアウト部分です。
getView()内でEditTextを取得し、文字列に変換します。
その結果が空文字でなければItemにセットするという処理です。
しかし、思っているようなものにはなりませんでした。
###プログラム
MainActivity.java
lang
1public class MainActivity extends AppCompatActivity { 2 private ArrayList<Item> itemList; 3 private ItemListAdapter itemListAdapter; 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 10 ListView itemListView = findViewById(R.id.item_list); 11 12 this.itemList = new ArrayList<>(); 13 this.itemListAdapter = new ItemListAdapter(this); 14 this.itemListAdapter.setItemList(this.itemList); 15 itemListView.setAdapter(this.itemListAdapter); 16 17 // サンプルデータ 18 for (int i = 0; i < 3; i++) { 19 String itemName = "sampleName" + i; 20 String itemInfo = "sampleInfo" + i; 21 addItem(); 22 Item item = this.itemList.get(this.itemList.size()-1); 23 item.setName(itemName); 24 item.setInfo(itemInfo); 25 } 26 27 Button addButton = findViewById(R.id.item_add_button); 28 addButton.setOnClickListener(new View.OnClickListener() { 29 @Override 30 public void onClick(View v) { 31 addItem(); 32 } 33 }); 34 } 35 36 private void addItem() { 37 Item item = new Item(); 38 this.itemList.add(item); 39 this.itemListAdapter.notifyDataSetInvalidated(); 40 } 41}
activity_main.xml
lang
1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:app="http://schemas.android.com/apk/res-auto" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 tools:context=".MainActivity"> 8 9 <ListView 10 android:id="@+id/item_list" 11 android:layout_width="match_parent" 12 android:layout_height="0dp" 13 android:layout_weight="9"> 14 </ListView> 15 16 <Button 17 android:id="@+id/item_add_button" 18 android:layout_width="match_parent" 19 android:layout_height="0dp" 20 android:layout_weight="1" 21 android:text="Add Item"/> 22 23</LinearLayout>
Item.java
lang
1public class Item { 2 private long id; 3 private String name; 4 private String info; 5 6 public long getId() { 7 return this.id; 8 } 9 10 public void setId(long id) { 11 this.id = id; 12 } 13 14 public String getName() { 15 return this.name; 16 } 17 18 public void setName(String name) { 19 this.name = name; 20 } 21 22 public String getInfo() { 23 return this.info; 24 } 25 26 public void setInfo(String info) { 27 this.info = info; 28 } 29}
ItemListAdapter.java
lang
1public class ItemListAdapter extends BaseAdapter { 2 private Context context; 3 private LayoutInflater layoutInflater = null; 4 private ArrayList<Item> itemList; 5 6 public ItemListAdapter(Context context) { 7 this.context = context; 8 this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 9 } 10 11 public void setItemList(ArrayList<Item> itemList) { 12 this.itemList = itemList; 13 } 14 15 @Override 16 public int getCount() { 17 return this.itemList.size(); 18 } 19 20 @Override 21 public Object getItem(int position) { 22 return this.itemList.get(position); 23 } 24 25 @Override 26 public long getItemId(int position) { 27 return this.itemList.get(position).getId(); 28 } 29 30 @Override 31 public View getView(int position, View convertView, ViewGroup parent) { 32 convertView = layoutInflater.inflate(R.layout.layout_item_row, parent, false); 33 34 Item item = this.itemList.get(position); 35 36 EditText itemName = convertView.findViewById(R.id.item_name); 37 /* 38 String name = String.valueOf(itemName.getText()); 39 if (!name.equals("")) { 40 item.setName(name); 41 } 42 */ 43 itemName.setText(item.getName()); 44 45 EditText itemInfo = convertView.findViewById(R.id.item_info); 46 /* 47 String info = String.valueOf(itemInfo.getText()); 48 if (!info.equals("")) { 49 item.setInfo(info); 50 } 51 */ 52 itemInfo.setText(item.getInfo()); 53 54 return convertView; 55 } 56}
layout_item_row.xml
lang
1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="horizontal"> 5 6 <EditText 7 android:id="@+id/item_name" 8 android:layout_width="0dp" 9 android:layout_height="wrap_content" 10 android:layout_weight="1" 11 android:hint="Name"/> 12 13 <EditText 14 android:id="@+id/item_info" 15 android:layout_width="0dp" 16 android:layout_height="wrap_content" 17 android:layout_weight="1" 18 android:hint="Info"/> 19 20</LinearLayout>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。