How to refresh ArrayAdapter used in Fragment A, when data is inserted in Fragment B (DialogFragment if it helps) using Room?
I've tried:
Calling adapter.notifyDataSetChanged() in the DialogFragment class (I use a fullscreen dialog to insert,update and delete data from my database. No difference.
Calling
adapter.clear();
adapter.notifyDataSetChanged();
adapter.addAll(dbDAO.getList());
adapter.notifyDatasetChanged
in my DialogFragment. This doesn't work either.
I cannot add just that item in my ArrayAdapter because that would result in duplicate entries (one entry from the database and one from the adapter). This is also something I wouldn't want.
More on my project
I'm making a Timetable app on Android using Java. I'll have to explain how my project is structured to describe my problem.
My project has only one activity, which displays a single fragment (occupying all width and height). This fragment (TimetableFragment) has a Looping Viewpager (made by #siralam here). As described in the LoopingViewPager's documentation, the item views are binded in the LoopingAdapter class within bindView() function.
Please let me know how to fix this. Also please let me know if there's anything else I should describe about my project/problem. Thanks in advance!
Related
I have an Activity with 3 fragments attached(using viewpager) , Whenever a user in any fragment changes the date range I need to load the relevant data from the DB and show it in the same fragment.
Could anyone guide me to a link or suggest an approach to this please.
I would use Room it enables you to attach an observer (using LiveData), and update the view using the ViewModel so anytime the data is changed in the database the view is updated straight away.
In your case the user input can go straight into the query, and once the data is fetched your view will be updated automatically. I personally have a case where I have 3 fragments in a ViewPager and they display similar data, so I use one ViewModel to fetch it.
Here is a codelab that shows how to implement it. Basically you will have a combination of a ViewModel, LiveData and Room.
I have a ViewPager with 2 tabs. Each tab just holds a Fragment with a RecyclerView. The RecyclerView in the first tab holds a list of all Articles while the RecyclerView in the second tab only holds the list of user's favorite Articles. Each Article in the first tab has a button in the RecyclerView row that allows the user to favorite it. I want new favorites to be displayed automatically (without forcing the user to manually press the refresh button) when the user navigates to the second tab. How can I go about doing this? Essentially I need to add / remove items from the RecyclerView which contains the favorites, but from the first fragment.
Things I have tried
Call adapter.notifyDataSetChanged() in the onRefresh() method of the favorites fragment. This works, but the performance hit is big because notifyDataSetChanged() is not a cheap operation. I would like to avoid this if possible.
Tried following this guide, and implemented some interfaces. The problem is, when I try to reference the adapter in my fragment from the containing activity, the adapter is always null. I can post code if needed to show what I was doing if this is supposed to be possible.
Now I am considering using SQLite DB to store changes (favorite removals/additions) in the onClickListeners, and then checking that table in the onResume() method and processing the changes individually, one by one with notifyItemChanged()
Is there a better way to do this?
For SQlite use CursorLoader to load data from SQlite. You can register to be notified when a data changed in a table. Then your loader will automatically load data again. Check this answer. Also check this Thread.
For getting data from internet you can use
Communicate with 2 fragments via parent Activity. Check this and this answer. This tutorial is good for better understanding.
LocalBroadcastManager to notify from one fragment to another fragment.
Hi everyone.
I've come up with an idea for an Android App, and I was thinking how to turn my thoughts into something working. I know how to program for Android even though I'm not that advanced as you might see, so I wanted some tips from you guys who I'm sure will be able to help.
Idea
I was thinking about an App to organize stuff, see your objects on a list and be able to add, move or remove them from the list.
Thoughts
I first thought I needed a RecyclerViewto display each item. Then I thought every item itself might be a box, and so be containing other items inside: this brought me to think of a sort of "nested" system of RecyclerViews. Before going too deep into this system I had to clarify each item should have been a Class, each of which should have had a RecyclerView assigned.
Question
I was wondering how I could make this "nested" system of RecyclerViews. I thought about making a RecyclerView an object, because I need each RecyclerView to display, function and be always the same in any screen of any item. But I don't know what the best way is.
Should I make it an object? How do I create a new RecyclerView through a button so the user can first tap an item and then, eventually, tap a button (inside the item details view for instance) to make it a box item and so create and open a RecyclerView when tapped?
P.S.
It may seem like multiple questions but it actually is only one: how to add and display a RecycerView when I tap a button inside a details screen of an item, of course automatically (have a reference to that RecyclerView).
You have to create the RecyclerView that holds your items list, let's call it rv_list, this RecyclerView has an adapter that takes each item and adapts it to an xml layout row_item.xml that you should define. Now to make an item itself display its own recyclerview you should put a recyclerview inside row_item.xml and populate it when adapting that item to rv_list inside the method onBindViewHolder.
I am working on my hobby project to learn more about android programming and stuffs. So i recently ran into a simple problem but couldn't find a solution for it.
My project for now have 2 activity [MainActivity], [CommentActivity].
In MainActivity, I have a listadapter (cusAdapter) will fetch all the content from a database and display it on a listview. Everything works fine there. It will fetch all the comments and total comments for a certain post based on the post id. In the same MainActivity, i have a comment button and a onclick listener which when clicked will bring user to the next activity [CommentActivity] and load all the comments and data needed using another custom adapter. I also have a total comment TextView that will show the total comment fetched by the json and display it in my listview.
When a user posted a comment on CommentActivity, it will save into the database via AsyncTask.
Now the problem is, when i press back button and navigate back from CommentActivity to MainActivity, i use intent with flag (intent.flag_activity_reorder_to_front). So it will never reload the MainActivity again when back button is pressed. Also it will never update the total comment in the MainActivity. What i want to do is when back button is pressed on CommentActivity, i want it to refresh/reload the ListAdapter in MainActivity to fetch the latest data from database.
Also i don't want to reload the whole MainActivity, i just wanna update the view without reloading the whole activity.
I believe there must be some easy way to do this. I can post codes if you guys need it to assists me.
Thank you.
Yes, there is an easy way.
Start your sencond activity using:
startActivityForResult(intent, RESULT_CODE)
use setResult(RESULT_OK) in your second activity after doing the commit of the changes to the database
override onActivityResult() in your first activity to check when the second activity returns properly
update the first adapter as needed.
Hope it helps.
In your MainActivity you could check in onResume to see if the data has changed, as this will be called when you go back to it from the CommentActivity.
If the data has changed, calling notifyDataSetChanged() on the adapter will reload the view with the new data
I'm currently using a custom listview adapter to connect a listview to a database. However I'd like to add endless list functionality to the listview. I've found another custom listview adapter here https://github.com/commonsguy/cwac-endless which implements the endless functionality i'm looking for.
Now what's the ideal way to integrate these two adapters? Should I make my current listview adapter a sub-class of the endless one? i.e. BaseAdapter -> EndlessListAdapter -> MyCustomListAdapter. Would this even work? As they'll be duplicate overridden methods.
Have you downloaded the demo app of cwac-endless? They have a demo, and this demo answer your question, as it have a class called DemoAdapter that overrides EndlessAdapter. :)
Also, take a look at this
"To use EndlessAdapter, you need to create a subclass that will control
the endlessness, specifying what View to use for the "loading"
placeholder, and then updating that placeholder with an actual row
once data has been loaded."