WHOATEMYNOODLES
WHOATEMYNOODLES

Reputation: 2731

Room - Saving edits in a list to database design question

I have a list of restaurant menu items.

  1. Burger

  2. Fries

  3. Pepsi

A user can edit any of the menu items and change the string

  1. Cheese Burger

  2. Cheese Fries

  3. 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

Answers (1)

javdromero
javdromero

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

Related Questions