Update View on Resume (Return to view after selecting an item from list view) - android

I have a view that displays some data for a selected city using a web service, it works fine if I have already selected a city. View is populated with data using onCreateView as its inherits from Fragment.
But, user can select a new city, by going to next view. As the user selects the city and press back button, now how can I update the view ?
Edit:
View1: Displays data if a city is already selected.
User goes from view1 to view 2
View2:Select a city from list view (On list item click, city is saved and onBackPressed is called to move back to View1)
Now when View1 is is presented again, how to update view ?

First way - Create fragment transaction and replace fragment
Second way - Implement setCity method in your fragment and update view in this method.

One way is to use onResume when returning to a view, and if the view needs to be updated.
#Override
public void onResume(){
// TODO: code here
}

Related

How to change item position of a recycle view from within its adapter?

Is it possible to change the item from within the adapter itself?
What am I trying to a achieve?
I have created a simple recycler view and it has a simple list item, but when the user clicks on the item a bottom sheet dialog gets displayed (that bottom sheet is being created in the adapter itself so it's different for every item) that bottom sheet dialog have a back and next button which can allow the user to move back and forth through the item view
When the user clicks on item -> bottom sheet dialog gets displayed -> if he presses next I have to show next following item with its bottom sheet opened.
How can I move from I item to another within the adapter?
you can pass position and list item as ArrayList to bottom sheet and update UI on next and previous buttons.
for example, you can create a method in your bottom sheet to update bottom sheet UI:
var listItem = ArrayList<Objects>()
var position = 0
private fun updateUi() {
val item = listItem[position]
///update ui with item
}
on next button click:
position += 1
if (position == listItem.size)
{
// end of list
} else
{
updateUi()
}
and on pervious button click:
position -= 1
if (position == -1)
{
// end of list
} else
{
updateUi()
}
RecyclerView has a couple of convenience methods, scrollToPosition and smoothScrollToPosition (the latter needs a little setup, check the link). You can use these to change the current item being displayed.
That requires having a reference to the RecyclerView though, which your Adapter doesn't have (or at least, it doesn't expose it). There's a callback when on starts observing the adapter though. So you could store that reference when you get it (but don't hold onto it when the equivalent onDetached callback happens)
Personally I'd want to separate all these things out, have them coordinated by the containing fragment, or some other component. So instead of one component (the adapter) managing what other components are doing and showing, it just calls a function saying "hey this item got clicked" and what happens as a result is none of its concern.
There's lots of ways to coordinate that, but it keeps things neater if you can separate that functionality. When one thing that has a specific job (displaying items and handling clicks) starts doing other stuff (messing with other UI components, handling stuff they do) it can start getting complex. Up to you but it's worth thinking about!
Thanks to everyone who shared their ideas.
Here is what I finally did
created an interface within the adapter
implemented that interface in the fragment which was starting the adapter
when the user press the next or previous button, I dismiss the current bottom sheet and then I call the function in the interface and also pass it the position of either next to or previous item
When the implemented function gets called in the fragment, first I move the list to that position second i get the item on that positon and third call perform on click on that item and it displayed the bottom sheet of that item
here is the fun which get called in fragment
override fun onAddIconClick(position: Int) {
surveyBinding.surveyRecV.scrollToPosition(position)
val v: View? = surveyBinding.surveyRecV.layoutManager?.findViewByPosition(position)
if(v != null) {
v.findViewById<ImageView>(R.id.viewAddIcon).performClick()
val temp = v.findViewById<TextView>(R.id.viewMainTitle)
Log.i("here22" , temp.text.toString())
}
}

Is there way to acces every RecyclerViewHolder?

I have a recyclerview with data that changes during the lifecycle of an app, lets say that in my recycler view holders i have an edittext field that comes with populated data but I want user to have access to change that data, is there a way that when the button is pressed i can somehow access all of those textfields at once?
Hmm you could keep 2 parallel lists in your recycle view adapter? one that has the main data, the other one is updated whenever the user changes something on an edit text.
Once the user presses the update button, you can just copy the data from list2(with modified data) to list1 (original) and update the recycler view.
for example
inside onBindviewholder()
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// find your edittext to set a listener for text change
// get the current item from list2 (list2 has same item originally as the original list)
keep updated the list2 data here.
}
on the main activity once the user presses the button you can do
adapter.mainList = new ArrayList<>(adapter.list2)
adapter.notifyDataSetChanged()

RecyclerView Data passing to same Activity

I have a cart program, in activity is simple, there was a recyclerview and a button. In recycler view I can edit its stock.
Now, I want to get that product_name and product_stock from that I've ever clicked and make changes.
Now, when every stock has been clicked, I want click button on activity, so I want that data I've ever clicked on recyclerview stored to array, so that button can do their action / function. Can you guys lead me, how to import data from recyclerview to its activity itself.
It was Android program to do shopping cart, so I click the data in recycler view it stored to activity array.
I don't have any idea how to store that from recyclerview to activity's array.
My expected result, should be, when I click buy button on activity, every changed stock on recyclerview is shown.
You can simply implement an interface in your adapter like this for getting the values from clicked position in recycleview.
public interface FetchRecyclerViewItems{
void getItems(String product_name,String product_stock);
}
And simply create a setter method for this interface in adapter like this,
private FetchRecyclerViewItems fetchRecyclerViewItems;
public void setFetchRecyclerViewItems(FetchRecyclerViewItems fetchRecyclerViewItems){
this.fetchRecyclerViewItems = fetchRecyclerViewItems;
}
Then set the values like this on your click like this in OnBindViewHolder,
your_view.setOnClickListener(new View.SetOnClickListener)....{
....
fetchRecyclerViewItems.getItems(product_nameFromPosition,product_stockFromPosition);
}
And implement this interface in your activity and you will get the product_name,product_stock values there.
Make sure to initialize the FetchRecyclerViewItems in your activity.

need to reload mainActivity controller on click of button located in customized listview

To make my question more understandable let me start with an image of my view.
I have an xml file named Menu, that has customized list view in it. I have created another xmlview named MenuCell as below.
Now tapping on add button I'm adding Item to the cart. which is working perfectly fine except not updating value of a cart (top right corner) on click event. But If I navigate to different view and come back to this view at this point I'm getting number of items added in the cart reflected properly. So How Can I reload my controllerview when I tap in my arradepter view's ImageButton.
this is my adapter code
holder.imageButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addItem(position);
notifyDataSetChanged();
}
});
void addItem(int position) {
count++;
}
Where count is item added count.
If anyone can tell How am I able to reflect this count of my arrayadpter class to my other controller class that holds actual list view.
Any Help will be appreciated
Thanks in advance.
You need to use callback feature to get notified to your activity , hence add button is part of list component so you can update your list view.
Here are few links for understanding of call back using interface
http://cleancodedevelopment-qualityseal.blogspot.in/2012/10/understanding-callbacks-with-java.html
http://stackoverflow.com/questions/11577695/what-is-a-call-back-interface-in-java
After :
notifyDataSetChanged();
just add:
YourList.invalidateViews();
YourList.scrollBy(0, 0);
Send the reference of your controller to your list adapter class. In the controller I guess you have a method that computes some data and after that call update the view that holds cart data summary. Just make sure that update is on UI thread.
Your List is not refreshing at the instant because you have to refresh data of your adapter and then call notifyDataSetChanged();
Suppose You have the data in an array which is visible in textview.Then In your function addItem add the data into ur array and then call notifyDataSetChanged();
This will immediately tell the list view that watever data it is containing is changed so time to refresh.
If you can paste more of ur adapter code i can be helpful

How to update a Fragment View in Android without changing the current state of the view?

Whenever I click on a refresh button on my fragment and get new set of data, I call this function:
public void updateView(User user){
LinkAdapter linkList = new LinkAdapter(getActivity(),R.layout.link_list,user.links, getActivity());
linkCardView.setAdapter(linkList);
}
This updates the view with new set of links at the top of it, and also drags the view up to the top to show the new links that have been added.
My only problem is I don't want the dragging up to happen. Say the user has scrolled to position B in a listView and then refreshes which results in new data added to the top of the listView, the current View state shouldn't be altered automatically. How do I stop this from happening?
Instead of creating a new adapter and setting it, when you update the content, I'm guessing user.links, make sure you update whatever array you sent into the adapter, then call notifyDataSetChanged() on the adapter.

Categories

Resources