Update recyclerview from the adapter - android

I am trying to update data in the recyclerview from the adapter, a function inside the adapter to do the update.
It is working properly and data is being added to the database but it is not updating the views, I need to restart the app so that it runs the cursor to get data from the database and load the views.
I figured out that the main problem is the different lists in the adapter and in the main activity. The list from the main activity is controlling the views so that when i update the list in the adapter nothing happens.
'''kotlin
private val adapter = Padapter (mainActivityList)
and
class adapter(private val adapterList:ArrayList)
'''
How can I do it ? Should i try to call a function in the main activity ?
I really appreciate any help or suggestions you can provide.

I solved it as adam yong suggested in the comments. I stored the list in the adapter.
At first i didnt know how to do that but i discovered somewhere that i had to call the list to the database thought the adapter like this :
In the main activity:
"'Kotlin
adapter.list.addAll( dbHandler!!.cursor())
"'

Related

Not sure how to deal with ListView IllegalStateException: “The content of the adapter has changed but ListView did not receive a notification”

Sorry for a brick of a code but I read that I need to run something on UI thread, how do I do that? private ArrayList JsonFIveDays(String weatherSearchResults) is where I set my listview adapter if that helps.
You have to notify the adapter whenever you do changes in the weatherArrayList.
For Example,
weatherAdapter.notifyDataSetChanged()
But in your case to use like that you have to declare the weatherAdapter as a property of the class.
I hope it will help you.
You need to notify adapter that you changed data with notifyDataSetChanged(). You can do that in setData method after changing data.

A good aproach on sorting recycler view after update and some doubts

I am having problems sorting items inside the recycler view after updating them.
I found this approach that is looking terrible and im wondering if there is a better approach. Also im using it to add new records to the recycler view but im not sure if it's a good practice of if I should change it.
Another big doubt I have in my code is about how im handling things in the adapter
Is it possible or better to do the sorting through the view holder ?
I really appreciate any help or suggestions you can provide.
Aproach:
Calling function on main activity to do the folowing
MyList.clear()
MyList.addAll( dbHandler!!.cursor())
MyList.sortedWith(compareBy({ it.sortervalue }))
adapter.notifyDataSetChanged()
How im handling buttons, is it a good practice or a big no ?
inside the onBindViewHolder
p0.itemView.editButton.setOnClickListener {
editpress()
Log.d("myTag", "edit pressed")
}
I solved it by moving the list entirely to the adapter so i dont call the adapter giving the list like
in Main activity it was :
private val adapter = adapter (list)
instead i do like this now :
private val adapter = adapter ()
and in main activity on create
adapter.lista.addAll( dbHandler!!.cursor())
so the list is inside the adapter only now when i sort it i dont have to pass it to adapter again.
to sort it :
listaPrioridades.sortByDescending { it.xxxx }
where xxxx is the column you are sorting.

Kotlin clear an adapter

What is the best way to clear an adapter in Kotlin?
I was trying to clear a recyclerview and update the values.
I am using LoaderManager and I am kind of stuck in this state.
If you want update recyclerview after adding/removing data in your adapter use this code
If you use Arraylist to store the data objects then just clear your list and call notifyDataSetChanged.
dataList.clear()
recyclerView?.adapter?.notifyDataSetChanged()

RecyclerView and Adapter, which one should take care of Data Change?

In RecyclerViewFragment Class, there is MovieData, and the Adapter Class stores MovieData.getMovieList( ).
My question is, when Event Handler be triggered,
Add / Delete data directly in RecyclerViewFragment Class
Define add( ) / delete( ) function in Adapter Class and call adapter.add( ) / adapter.delete( ) in RecyclerViewFragment Class
Which way is the better design? Who has the responsibility to take care of data change?
I would suggest to have RecyclerViewFragment containing the data. And create an adapter passing the reference to the data. For example:
mAdapter = new CustomAdapter(mMovieData.getMovieList());
Then in the RecyclerViewFragment just perform the needed actions such as add / remove such as:
mMovieData.getMovieList().add(index);
Think this is the most common way todo it. You do not want to replicate the getMovieList data twice in adapter and RecyclerViewFragment. This will get confusing.
If data change is happening after adapter has been initialized, then it should be handled inside adapter. Adapter also has a nifty function notifyDataSetChanged() which will update item positions inside the adapter when data has changed.
The recyclerview is not concerned with the data, this is all handled by the adapter you have created to present the data. Have a look at the functions here to see what options you have in regard to data changes.
Adapter is the responsible class for filling a list with data ,for example if you have a list that need to be filled with data lets say SCORES then the adapter is the one responsible for filling these scores in the list. on the other side, RecyclerView is reposnsilbe for making the adapter fill it with more speed and effecieny, the recyclerView uses views that are previously used to display data by the adapter and place them in cache for later reuse to display the same type object so that it doesn't have to initialize a new object each time the adapter want to fill item in the list which make it faster.
and here is a link that might help you as well :
Hope my answer have helped you.

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

Categories

Resources