Updating ListAdapter from different activity - java

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

Related

How to refresh first recyclerview data when user clicks on second recyclerview items

I have first recyclerview in Main Activity and second recycler is in Fragment. I want when user clicks on first recycler item then second recyclerview data should refresh according to the clicked item.
I am still a beginner i dont know how to achieve this?
Please help me.
Thanks.
Sorry, I think the comment would be more appropriate, but you should describe your problem more clearly.
I still don't understand what this is for, but I can give you a few pointers:
1. If the recyclerview in Fragment gets data from Activity, you can transfer it by setArguments.
2. If the data is not very dependent on Activity, you can write a refresh function in Fragment, then call it from Activity.
Hope it can help you.

Is there any method that i can call on SecondActivity to add a new item on Arraylist on FirstActivity.Fragment that has Recyclerview?

Hi im currently building my First android app and i have a problem that i cant solve this past 3 days. so i have MainActivity and on it there is a Fragment with Recyclerview and ArrayList< Item > listOfItem, on this Fragment there is a Floating button that when i click it will take me to SecondActivity there is a edit text on this SecondaryActivity that i have to fill then i will pass the data back to listOfItem.My problem is what method can i call on SecondActivity to create/add an listOfItem when i go back to MainActivity Fragment? I dont want to make Adapter and listOfItem to be static. Is there a way? thanks
Simple, You can achieve this by using Shared Preference.
What you need to do is just save that Edit Text data to Model(POJO), save that Model to Preference and in your Main Activity - Fragment on Resume method show updated data from that Preference.
Yeah here you need to take care of
The fragments onResume() or onPause() will be called only when the Activities onResume() or onPause() is called. They are tightly coupled to the Activity.
For more convient and efficient handling of data from any activity or fragement to any activity or fragment, you can use Event Bus.. Here is link where you will get the details information: https://github.com/greenrobot/EventBus
The best solution would be to start the second Activity for result.
startActivityForResult(ActivityBIntent)
Then checking the result overriding
onActivityResult() on the first activity and updating your list/adapter from it

Update RecyclerView From Different Tab In Viewpager

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.

Updating an item of ListView from another Activity

I'm creating a ListView in Activity A which contains some kind of Post views, each of them have its own like button.
Every post can be viewed in Activity B as a single post (Which I re-read from server because there's more info to present).
I'm trying to figure a way that if a user click the like button on Activity B (Which updates the server & UI of its own Activity (B)),
would update the UI of the same post
(which I have its ID and position in the list ofcourse)
on Activity A. .
Any thoughts?
You have to update your adapter data for ListView in another activity and once you back to Activity A in onResume() you can call notifyDatasetChanged() to your adapter and refresh your ListView with newly updated data.

Android : Text Fields are displaying empty when returning to previous activity

I have a problem with back button functionality in an Activity of Android. The first Activity having the Spinner to select one item from the Spinner's list and second one is the text field. I implemented search functionality using the Spinner and text field. The results are displaying fine as a ListView.
Here my Problem is:
While returning to the first Activity, the Spinner and text field are showing empty in the Activity. It should show the previous searched results.
Help me with the sample code/ links.
Dont create a new intent. You just need to call finish() from your second Activity to handle back event and move back to your first activity.
Its normal. When your first Activity goes to back ground its finished by System itself. So make sure to save your data in some place and in Activitie's onCreate() and onRestart() method reload the data to TextView and spinner..
Edits:
Create a Data class and Store your search results in String[] array or a String or how ever you like it. and make the class a singlton class. and when you come back to this screen fetch those data's and set Text of TextView and adapter for Spinner..
Shafi yes, i am calling the back function using Intent as Intent i=new
Intent (presentClass.this, previousClass.class);
Don't do this. Because the Activity stack will become like loop with same Activities started again n again. Instead just finish presentClass.. it will come back to previousClass
Just an addition to #ntc post look to onSaveInstanceState(Bundle outState) and onRestoreInstanceState(Bundle state) methods in Activity.

Categories

Resources