Reputation: 2731
I have a list of restaurant menu items.
Burger
Fries
Pepsi
A user can edit any of the menu items and change the string
Cheese Burger
Cheese Fries
Diet pepsi
The user can hit the save button to save all the edits. My question is what is if there is a better way than what I'm doing currently.
Right now when ever a user starts typing or anything, such as adding even a single letter "Burgerz", I would save that into a instance variable Map with the position of the item thats changing as the key, so 0, Burgerz. If he adds another "z", I would put it into the map as 0, Burgerzz.
When the user hits the save button, I would loop through the entire map and then make Updates to my local SQLite database
"UPDATE menu_item WHERE id = 0"
If the user has 30 edits to the menu, it would loop through the map and make 30 Update queries.
This is the only way I could think of, I was wondering if anyone knows a better way to do this?
Upvotes: 0
Views: 39
Reputation: 1937
An alternative is to use setOnFocusChangeListener
on every EditText
et.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
update(id)
}
}
});
Important:
If your edit text is in a listview, every key down will result in the box losing and getting focus. See this solution to keep track of the currently focused box:
Android edittext in listview loses focus on calling notifydatachanged
Upvotes: 1