Android ExpandableListView save input in each item's edit text on button click

时间:2018-09-18 20:00:36

标签: android expandablelistview expandablelistadapter

I'm new to Android and can't quite figure out the right approach for the problem I'm trying to solve. I have an ExpandableListView with several items. Each item has an EditText, except the last item has a button. The contents of the EditTexts are to be loaded from the database. When the button is clicked or when the activity is navigated away from, I want to save the contents of each EditText to my database.

I'm not sure what to call from the activity's class, what to call from my adapter, and how exactly to access each item appropriately. Code is welcome but not necessary, I'm just looking for guidance on the general approach. Thanks.

2 个答案:

答案 0 :(得分:1)

I'd recommend putting a method in your adapter called saveAllValues. It iterates through the list of objects in the adapter and saves them to the database. Call this when your button is clicked and in your activity's onStop() method (which is called when the activity is no longer visible).

You should have your activity fetch the values for the item IDs in an AsyncTask in its onCreate method. Then pass the list of id/value pairs to the adapter in its constructor. It should maintain this list so it can go back through it and save the IDs and values to the DB.

答案 1 :(得分:1)

希望我在以下链接中的回答有所帮助:

  1. Values of counter changes after scrolling ExpendableListView显示了如何在适配器中维护列表以及如何从活动中获取列表。

  2. Remove the divider view in the expandablelistview last item显示了如何使最后一个孩子与其他孩子有所不同。