开发者

Editable ListView - Update Adapter with UI Values

开发者 https://www.devze.com 2023-02-19 08:09 出处:网络
I would like to create a ListView in Android where I have the ability to 开发者_开发百科add a new blank row, and have the controls in the new row be editable.Then on some event (either the user clicks

I would like to create a ListView in Android where I have the ability to 开发者_开发百科add a new blank row, and have the controls in the new row be editable. Then on some event (either the user clicks add again, selects another row, or some other trigger I haven't determined yet), I want to update the Adapter with whatever values the user entered into the editable row.

I currently have editable controls within each row and the ability to add a blank row via a menu item. I cannot figure out how to sync the user entered data with the Adapter.

I originally thought that Adapters are two way data binds, but that doesn't seem to be the case. From my research and experimenting, if I change an Adapter value and call notifyDataSetChanged(), then the UI gets updated. Is there a reverse operation?


I was able to accomplish the two-way data binding by adding a KeyListener and OnFocusChangeListener to each of the controls on my row's View. Both of these events will call into a method I created on my row's View to loop through all the controls on the view and update my adapter's data with the current values. I had to make sure to not call notifyDataSetChanged(). This method is necessary only for programmatically changing the data source object and having the UI reflect the changes.

Not the most efficient way ever, but it works decently well.

Another thing to note, adding and deleting rows I needed to set both control and view level squelching of updating of my adapter view. For deletions, what I did was add a long click event on my row's View to have a menu with a delete option. Then I started squelching updates on a View level, because I programmatically edit my data source object to remove the given row data and call notifyDataSetChanged() (necessary otherwise OS will throw an exception). Squelching here makes sure I don't hit my events and get into an infinite loop and that my data is properly synced. Then on the deleted row View I set all my controls to squelch their event's update adapter. This is because the deleted row View will still have focus, and I want to make sure I don't update my data source object with values not on the UI. This flag gets flipped once I get the row's View back from the ListView recycling process in getView() of my adapter.

Adding a new row I also need to squelch on just the row's View level. This is because I programmatically change my data source with a new empty row of data, and call notifyDataSetChanged. Reasons are exactly the same as the delete.


your problem is hat you, for example may hav 300 items on the list (that are repesented by EditItems) but only 12-20 EditItems in reality that are recycled. i guess your only way to know that use has finished with his row is FocsedChangedListener on each of your views. Once focus is off use:

  1. in your adapter's getView use: if v is your View then do v.setTag(position)
  2. in the OnFocusChangedListener once focus is off use: int pos = (Integer)v.getTag(); mAdapter.updatePosition(text,pos)
  3. make sure your adapter has an updat mthod that will update the object in position pos with the String 'text'


To refine C Nick's solution a bit, you can use EditText.addTextChangedListener.

0

精彩评论

暂无评论...
验证码 换一张
取 消