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.
Related
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!
So I've been developping an app on Android Studio and at some point I need to display couples of values composed of texts and images but I have some issues with the display.
Let's say here that I want a list of fruit names associated with a picture, I'd like to display it that way :
Example
Each image and its related text is in linearlayout, so it's a list of linearlayouts.
The "add fruit" button open an alerdialog where the text and the image can be chosen, by clicking the 'ok' button in the alertdialog the name and the image are saved in a sqlLite database (which I already have programmed), and the linearlayout has to be added in the current list as the dialog is dismissed.
So the issue here is that:
I don't really know how to dynamically display each one of these linearlayouts in this specific order.
I don't know how to make it so that when a new fruit is added and the dialog dismissed, the list in the activity is refreshed directly.
I've been thinking of using a listview but listviews are only horizontal OR vertical but in this case I kind of need a mix of both.
Another idea was to delete the layout containing all the linearlayouts (all the list) when a new fruit was added, and then take my database to create all the linearlayouts again. But dynamically I don't think it will work, the refresh won't be done (?)
My last idea was to create a fragment and use my database in the onCreateView method to display every elements. And to refresh it, I'll just detach and attach again the fragment to the activity each time I add a new element.
I don't think my ideas are the optimal or even a correct way to do it, I've been struggling with it for a while. If you have any suggestions or ideas to help me, it would be great :)
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 want to make a recipes app, the basic layout is the same for all the recipes the only thing that changes are the images, times and ingredients.
The problem is, I could make 40 activities, one for each recipe and performance wouldn't be a problem because the user is only interacting with one activity at the time. However, writing the same code and going on a copy paste spree feels wrong.
I would have to repeat the same code over 40 activities and it would work (I guess), but it would be much easier to create one activity with the functionalities I want like a timer and the layout and in some way make smaller files that insert the data for the selected recipe in that "pre-made template".
There's must be a way of doing it, although I'm not experienced enough
Here is an example layout
It is usually good practice to have a base activity that implements all the code common to several activities then those activities can simply inherit from the base activity like this public class ChildActivity extends BaseActivity.
This will allow you to call methods that are in the BaseActivity from any of the child activities. You can have a read up on Java Inheritance here and here is a blog post with some examples of using a base activity.
You can create only one activity that will receive the Receipt data as extra using Intent. The layout for this activity should contain an image view(or a recycler view to hold all your images), a recyclerview to show your steps/ingredients and a textview for the time.
Receiving these data from the activity(that one that the user selected which receipt he wants too check) that created this new activity, all you need to do is to setup your layout with this data.
Check this question to get how to pass data between activities
Click here to see how to create recycler views.
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