Trying to pass LiveData List as an argument - android

I am trying to implement a SearchView in my room with a view SQLite Database. The app has a RecyclerView with a CardView that displays rows from the database. I am trying to make it so a user can use a SearchView to filter the CardView and only show cards matching the search. I have been following the information in this post: Android - Implementing search filter to a RecyclerView .
Anyway, my app currently uses a LiveData method getAllFlares() and it returns all of the rows from the database. The return is of course of type LiveData. The method of filtering in the link has you pass an ArrayList as an argument to a filtering method. So I was trying to pass the results of getAllFlares() to the filtering method. To make the types match up, I altered the filtering method to accept a LiveData list instead of an ArrayList. Here is the code where I am trying to pass the LiveData list to the filtering method:
LiveData<List<Flare>> flare;
flare = mFlareViewModel.getAllFlares();
adapter.filterFlareList(flare);
But it gives this error like:
updateFlareList (java.util.List<com.android.fibrnah.Flare>) in FlareListAdapter cannot be applied to (androidx.lifecycle.LiveData<java.util.List<com.android.fibrnah.Flare>>)
What am I doing wrong? I feel that there is some fundamental flaw in what I am doing with LiveData.

Related

Sorting recyclerview by recently accessed

I am doing a side project of making an app (with Java since I already know it). I have a recyclerview which loads some data via the room database library. The elements of the recyclerview are clickable.
My problem is I want the user to be able to sort the recyclerview so that the most recently accessed items go to the top.
My original idea was to assign the entities to have two variables - a String list_name which also serves as the id, and an Int order_of_access. Also, in my ViewModel I have a getAllLists method which returns a livedata list. I have an onChanged listener in the fragment activity which nicely updates the recyclerview when data is added/removed.
When the user adds a new list, it is assigned an order_of_access of the listsize (+1). But when the user deletes a group of lists, or clicks on a list, I want to update the order_of_access, say with an updateOrderAccess method.
Do you think this is the best way of doing what I want?
Where should I place updateOrderAccess and how would you recommend it be written? Since the method getAllLists returns livedata, it is tempting to put updateOrderAccess in an observer in the fragment (in onChanged) - but this will obviously create an infinite loop. It seems more in the correct philosophy to put it in the ViewModel, but then how would you suggest the updateOrderAccess method to be written? I'm having some trouble conceptualising what I need.
I hope the question is not too vague - I will update it if you need more details.
Where should I place updateOrderAccess and how would you recommend it
be written?
I am so sure that you must write it in the view model, as long as updateOrderAccess() is editing the list which is observable then you have andexpose by that the ui state then you have to put it in view model, and the observers will be notified ( in this case it is recycle view) and it will redraw the list in the order you offered.
note: do not you ever update the state(ui data) outside the state holder so you implement UDF (unidirectional Data Flow) pattern.
see the references below to read more about UDF so you never get confused where to declare your functions by letting the architicture lead you:
Guide to app architecture
ui layer
state holders and ui state
Do you think this is the best way of doing what I want?
i am not very sure that i got exactly what your app do, but it seems like you want to re-order the elements of recycle view depending on the ui event (click) or data change (deleting or adding new element), now you have two choices:
if the order is very importnat to you that much you want to keep it even if the app has been destroyed
then you have to add a field in the room entity represent the ordering (let us call it order) and whenever the user click on the recycle view you have to update the rooms field "order" which is "flow" or "liveData" or any observable type, that will tell the view model that there is a changing in the data, now the view model have to re-order the new data by the field "order" and pass it to the recycle view to show it.
if your app do not have to save the order changes after the app been destroyed
then you can simply do that:
create list which is called "orderedList" you will put the list items in it by the right order, and another list called "unorderlist" which have getAllLists
for the first case where the ordering is being changed by user click, you
can declare a function in viewModel then use it in the ui
controller (your activity or fragment), so whenever the list item is
clicked this function just re-order the orderedList elements ( which
is observable, so the changes reflect on the ui ) just by change the
clicked item position to the front of the list.
for the second case where the ordering changes by data changes like
add or delet a list item in the database, then you have to compare
the legnth of orderlist and unorderlist legnth, if unorderList is
longer then it is an add situation else it is a delete situation, in
adding case just add the last item of unorderList to the orderList,
else you have to check the deleted item and delete it from
orderList.

MVI+MVVM: who should store input data?

I'm trying to implement a simple screen, which idea is the following:
It receives a list of Items from another screen. Each item contains only id;
It displays those items in a ListView;
Each item in the ListView is represented by LsitViewItem class, which adds optional name property to the id property of the Item;
Thus each cell displays id and additionally it displays an input field, which allows user to input name;
There's also a button on the screen. When user presses this button all Items with their ids and optianally (if user has input any) names are sent to backend.
I want to implement this screen via MVVM architecture, combined with the MVI concept. I'll call Model from the MVI concept State just to distinguish with the Model from MVVM. With this concept I need a single source of data. I also need to propagate back to "smth" all user inputs to the "name" fields.
It looks like my model is a backend. Another words it is a Retrofit service implementation or some wrapper class around it.
With this configuration I don't understand the following:
Who should stote the initial Items list from the previous screen? A ViewModel or Model?
If Model then do I need to propagate all names changes back to the Model (according to the MVI concept)? If yes, then what would be the difference between ListViewItems (presentation layer) and Items (Model layer)?
Is my interpretation of the Model correct or I can omit it? Another words can I have MVVM without Model? So, all my Items are stored within ViewModel and when user presses the button they're just send to the backend through some class, which is not part of MVVM architecture at all?
Other ideas/suggestions?

Couchbase lite on Android, retrieve views

This question is about Couchbase lite (no Sync Gateway).
I'm new to Couchbase, I managed to use the demo app, but I don't understand it completely.
It contains this code which (as far as I understand, since I'm not native English speaker) retrieve views to populate a listview with the indexes:
// This code can be found in ListsActivity.java
// in the setupViewAndQuery() method
com.couchbase.lite.View listsView = mDatabase.getView("list/listsByName");
if (listsView.getMap() == null) {
listsView.setMap(new Mapper() {
#Override
public void map(Map<String, Object> document, Emitter emitter) {
String type = (String) document.get("type");
if ("task-list".equals(type)) {
emitter.emit(document.get("name"), null);
}
}
}, "1.0");
}
listsLiveQuery = listsView.createQuery().toLiveQuery();
Could anyone give me a hand with what each part is doing?
In which step is the listview populated
Can I change "list/listsByName" in the code (line 3)? What would happen?
Can I emit more than one element?
The code is a little bit convoluted. Let's answer the easy parts first.
Can I change "list/listsByName" in the code (line 3)?
Yes. That's just the name of the Couchbase View. You choose the View name. Unfortunately the terms used in Couchbase and Android overlap some. A Couchbase View is a kind of static index of your database.
Can I emit more than one element?
Yes. You can emit most anything you want. Take a look at the documentation here
Now, tracing how the Android ListView gets updated:
In ListsActivity.java notice in the onCreate method a ListAdapter instance gets added to the ListView. This ListAdapter is a private inner class that extends LiveQueryAdapter.
LiveQueryAdapter is in the utils subpackage. If you look at its constructor, you'll see it adds a change listener to the query passed in. When triggered, this change listener sets an enumerator equal to the rows passed back by the live query, then calls notifyDataSetChanged to tell the list to refresh itself. That, in turn, causes getView in ListAdapter to get called. That's where the data is pulled from the database and used to populate a list entry.

How to update CustomListView item in Android using Model where the model data is downloaded from internet?

I have a CustomListView in my android app. Each item consists of two pieces of text which are to be retrieved from an online SQL database. I'm using a Model class called ListModel and a custom adapter called CustomAdapter. I'm using an Asynctask to download the model data from the internet. But the problem is that, adding of a ListModel object to my ArrayList is not working when I do it in the onPostExecute method of my Asynctask. So, the listview is not getting updated. How do I display the Model items on my Custom List as soon as they get downloaded? Is there any way to do that?
This type of problems occures when adapter not properly notify after data downloaded. Notify your adapter in postExecute by notifyDataSetChanged () method
Generally this is because notifyDataSetChanged() isn't called on the arrayadapter. (but stacktraces/your code would be helpful)
In addition, this is a prime use of an in-memory SQLite database (if you plan on doing any custom queries)
Or a full on-disk SQLite DB if you want to cache data.
(Adding a content provider(by just surrounding the SqliteDB) would also be nice if you want to abstract away some more and provide observers, etc. )

How does an Android adapter decide what to re-render?

Say I have a List<User>. Now I can wrap this list in an ArrayAdapter.
List<User> users = Users.getAll();
ArrayAdapter<User> = new ArrayAdapter<User>(this, android.R.layout.simple_list_item_1, users);
I then bind the adapter to a listview to display the list of Users.
Users.getAll() uses Sugar ORM to query the database and return a list of users. Items can be added to the user list from the activity that displays the user list. I am wondering how do I keep the listview updated.
Option 1
One way is to manually update the users as a I add to the database and then call adapter.notifyDataSetChanged(). This works, but it doesn't feel right because I am maintaining a "fake" list that represents what is in the database.
Option 2
I am wondering how bad is it if I just clear the items in users, update it with the results of a new database query and then call adapter.notifyDataSetChanged()?
Will all the child views be thrown away and be re-rendered? Or does it call the equals() method to see if the models bound to each child is the same and then update only what is new?
Other Info
Since I am using SugarORM, I don't think I can get access to the Cursor to do something more efficient. However if there is a better way to keep the list synced with SugarORM, I am happy to hear that as well.
In answer to your option 2: No, it doesnt call equals, because the adapter works in conjunction with the widget to re-use the views, it doens't create a new view foreach item in the list, it create a view foreach visible item and as you scroll re-uses view that left the screen.
The best option here is to create your own adapter, creating a class extending BaseAdapter and creating your own logic inside it requerying the database and notifying the change to the listview (or gridview)..
On the other hand doing what you said here:
I am wondering how bad is it if I just clear the items in users, update it with the results of a new database query and then call adapter.notifyDataSetChanged()?
isn't bad either.
Create a DAO class that extends Observable, then have your Adapter implement Observer. Now every time you add or remove a SugarRecord, do through the DAO class and whoever is register as the Observer will get notified through the following method:
#Override
public void update(Observable observable, Object o)
You can more about Observable/Observer pattern here. This is just one of the many examples and tutorials out there.

Categories

Resources