Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

java - How to edit an entry inside View Model / Repository with Live Data

Maybe I'm misunderstanding this all, but how do you edit data using Live Data/View Model/Repository? I see how query all the data, delete an entry, but I want to edit a particular field.

For example, I have 3 things I'm tracking. Date, Time, and Category.I need to be able to change what the category is.

I tried doing something like this in one of my activities:

        budgetViewModel.getAllEntries().observe(this, new Observer<List<BudgetEntry>>() {
            @Override
            public void onChanged(List<BudgetEntry> budgetEntries) {


                Log.i(TAG, "2nd Observer is going off");

                if (manualUpdate == 1) {
                    Log.i(TAG, "MANUAL UPDATE");

                    budgetEntries.get(0).setCategory(updatedCategory);
                    manualUpdate = 0;

                    Log.i(TAG, "Budget Entries: " + budgetEntries.get(0).getCategory());
                }

                else {
                    Log.i(TAG, "No change");
                }


            }
        });
    }

    }

But it doesn't change it permanently. Only for this instance I guess because the Log shows I changed the category, but when I reload the app or check another activity, it still shows the prior data.

I can't find any tutorials on this so any guidance would be most appreciated!

New View Model Changes. I get error cannot create instance of class:

public class BudgetViewModel extends AndroidViewModel {

private BudgetRepository budgetRepository;

//private LiveData<List<BudgetEntry>> allEntries;

LiveData<List<BudgetEntry>> _allEntries = budgetRepository.getAllEntries();
LiveData<List<BudgetEntry>> allEntries = Transformations.map(_allEntries, new Function<List<BudgetEntry>, List<BudgetEntry>>() {
    @Override
    public List<BudgetEntry> apply(List<BudgetEntry> input) {
        return input;
    }
});



public BudgetViewModel(@NonNull Application application) {
    super(application);
    budgetRepository = new BudgetRepository(application);
    //allEntries = budgetRepository.getAllEntries();


}

public void insert (BudgetEntry budgetEntry) {
    budgetRepository.insert(budgetEntry);
}


public void delete (BudgetEntry budgetEntry) {
    budgetRepository.delete(budgetEntry);
}

public void deleteAllEntries () {
    budgetRepository.deleteAllEntries();
}

public LiveData<List<BudgetEntry>> getAllEntries() {
    return _allEntries;
}

public LiveData<Integer> getCount() {
    return budgetRepository.getCount();
}

}

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the Transformations.map function in your ViewModel for this.

Applies the given function on the main thread to each value emitted by source LiveData and returns LiveData, which emits resulting values.

The given function func will be executed on the main thread.

Kotlin

import androidx.lifecycle.*

class YourViewModel : ViewModel() {
  private val _allEntries: LiveData<List<BudgetEntry>> = budgetRepository.getAllEntries()
  val allEntries: LiveData<List<BudgetEntry>> = Transformations.map(_allEntries) { list: List<BudgetEntry> ->
    list.map?{ budgetEntry ->
       budgetEntry.setCategory(updatedCategorya)
    }
  }
}

Java

import androidx.lifecycle.*;

class YourViewModel extends ViewModel {
  private final LiveData<List<BudgetEntry>> _allEntries = budgetRepository.getAllEntries();
  final LiveData<List<BudgetEntry>> allEntries = Transformations.map(_allEntries, new Function<List<BudgetEntry>, List<BudgetEntry>>() {
        @Override
        public List<BudgetEntry> apply(List<BudgetEntry> input) {
            // do the mapping for each budgetEntry
            return // mapped list
        }
    });
}

Now you can observe allEntries as usual. The map functions take care of updating the LiveData.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...