In a reycler list, I have an Viewholder in which there is a button.
The text of a button is based on a variable if user is verified.
Now on click of user can be verified. a dialog is opened, user gets verified. After user is verified, i am updating sharedpreference boolean.
I have an observable on sharedpreference. How do i change/update viewholder items?
After verifying, i want to update button text for all items
You can pass the adapterPosition of the item clicked to the dialog, and on pressing OK, you can call a function inside your RecyclerView adapter to update the given item.
updateItem(position: Int) {
Object item = list.get(position)
item.isVerified = true
notifyItemChanged(position)
}
Make sure you call notifyItemChanged(adapterPosition) in the adapter after you have updated the list item, this will automatically update the item at the clicked position.
Related
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())
}
}
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()
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.
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
}
I created a spinner populated with values from a string array in my "strings.xml. When a user selects an item in the spinner I would like it to redirect them to that page, and then be able to return to the main page via a back button.
How do you send them to another page via a spinner selection?
Add an on tem selection listener. In the listener's onItemSelected method, you'd use position to determine the selected position in the array. Based on the position, you know which string was clicked. Then you'd create an Activity for each "page" and start the appropriate activity.