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

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.

Related

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()

How to update the particular list item in list view in android

I'm new to Android. I have an application where some items appear in a list view. Now I want to add some new items. I want to show the word "New" for newly-added items, and when a new item is clicked that "New" word should disappear. How should I do this?
Follow that answer that is use for gridview you can also same for listview, notifyDataSetChanged();
Android Gridview is not refreshed while pressing back button
Update the data backing your list adapter and call notifyDataSetChanged() on the adapter.
On selecting particular item from the list, you can change the status in the model class of that item ,from new to empty string "". And then depending on this value you can set the visibility of view if you want.
As the model in the datalist is updated you need to notify your adapter that underlying data has beeen changed. So call notifyDataSetChanged() on the adapter.
UPDATE:
notifyDataSetChanged() -
Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
When an Adapter is constructed, it holds the reference for the List that was passed in. If you pass-in a List that was a member of an Activity, and change some of the data later, the Adapter is still holding a reference to the original List. The Adapter does not know you changed the List in the Activity. Hence call notifyDataSetChanged() on Adapter.
Try following code.
listview.notifyDataSetChanged();
listview.invalidateViews();

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

Activity list view display different data (String array) on click of listitem

I have an activity which contains listview, when click on any list item, i want to display same activity with listview with different data and so on.
When i click on back button i need to display same activity listview with old data.
Is it possible? or is there is any other way to achieve this. I dont want to create any new activities or fragments for this?
Thanks
KrishIndia
You can write the function such that each time you click an item, it generates the list, assigns the data to your adapter and attaches it to your list view of choice.
If fetching the data won't be expensive for you, you could use only one adapter (array adapter or list adapter or simple adapter should be fine) and just re-assign it each time. (Ex. If you were listing files in a directory, inside your onClick() function you would have some function declaration like list_items(directoryName) that you would call each time. That function would declare and set an adapter for your data.)
If you are concerned about the expense of re-fetching the data, just store multiple adapters (outside your onClick() function) and set them to your list view as needed.
on click of list item just set new adapter or new data and call notifyDataSetChanged()
and for backpress override onBackPressed() and handle accordingly
Maintain separate adapter for each new data. When you want to return back just pass the relevent adapter object to that list view

How to recreate the listview of a former ListFragment after pressing back button

I have a ListFragment with a listview in it, that contains a navigation-structure. By selecting an item of the list, the next navigation hierarchy stage should be displayed. And so on.
The next hierarchy stage is created by a new fragment (remove the old fragment and add the same fragment new via fragmant transactions by using addToBackStack), in which the arrayadapter of the new listview is updated to the items of the next navigation hierarchy. That work pretty fine.
I want the user to have the possibility to navigate back via the back button. And here start my problems. I have no clue how to save the listview so it could be recreated after using the back button.
I thought using onSaveInstanceState would be a good idea. But onSaveInstanceState ist not called when the transaction gets comitted. I checked it by placing a Log.d("..","...) in the method. It´s not called. (What I found out, is, that onSaveInstanceState is called when rotating from portrait- to landscape-view and vice-versa. But that´s no help for my problem).
So what would be the best idea to store the elements of the listview and get it back to recreate the listview after using the back button and the former fragment is getting created? All items are Strings that are stored in an ArrayList that is bound to the ListAdapter.
Here my code. I implemented an interface, via the click on an item in the listview calls a method in the parent-activity. In this method I first call a method to fill the already existing ArrayList (navigationsItems) with the new items that are content of the next navigation-stage:
The code of my ArrayAdapter:
arrayAdapterFuerNaviList = new ArrayAdapter<String>(
BRC_BoardOverviewActivity.this,
android.R.layout.simple_list_item_1,
navigationsItems);
In this method I first call a method to fill an ArrayList (navigationsItems) with the items of the next navigation-stage:
// load new itemlist to navigation-
public void inhaltAktuelleNaviListeInArrayLaden() {
navigationsItems.clear();
for (int i = 0; i < prefs.getInt("aktuelleNaviListe_size", 0); i++) {
(...)
navigationsItems.add(titel);
}
}
Then I call the method, with which I load the new ListFragment and bind the new ArrayAdapter to it:
// push navifragment to BackStack and create a new instance of it
public void naviFragmenteNeuLaden() {
// get a reference to the actual ListFragment
Fragment removefragment = frgm.findFragmentById(R.id.navi_container);
// initialize ne ListFragment
BRC_NavigationsFragment navifragment = (BRC_NavigationsFragment) frgm
.findFragmentById(R.id.navi_container);
// remove the old and bind the new ListFragment from/to the container ...
FragmentTransaction frgmta = frgm.beginTransaction();
frgmta.remove(removefragment);
frgmta.add(R.id.navi_container, navifragment);
// ... push the old ListFragment on the BackStack and commit ...
frgmta.addToBackStack(null);
frgmta.commit();
frgm.executePendingTransactions();
// ... bind the updated ArrayAdapter to the new ListFragment ...
try {
navifragment.setListAdapter(arrayAdapterFuerNaviList);
} catch (Exception e) {
// TODO
e.printStackTrace();
}
// ... and notify
try {
arrayAdapterFuerNaviList.notifyDataSetChanged();
} catch (Exception e) {
// TODO
e.printStackTrace();
}
}
As mentioned, forward-navigating is no problem. But when i press back button for e.g. in the 2nd navigation-stage, the former ListFragment gets loaded (verfified with Log.d("..."."...)-Messages in onCreatView() of ListFragment), but the former listview which was in it isn´t created.
I am currently working on a new approach. When you click on an item in the list, I write the current list, together with a reference to the current ListFragment into a vector based stack. Then I catch with "onBackPressed()"-method the press of the back button. So now when the button is pressed, I call on the already mentioned methods (see above), write the item-data from the stack back into the list, call via the stored reference the forme ListFragmt and bind the updated ArrayAdapter to it. This is work in progress actually. I will write the result of this approach when finished.
I solved the problem.
For the different navigation contents of the ListView I have defined a class "Naviliste", in which all the items are stored. If someone clicks on an item, then the a new listview with the correspondingly new list will be generated. These lists are indexed, so each list get´s a fixed ID assigned.
By clicking an item in the ListView, the ID of the current Naviliste is pushed onto a stack. After that a new fragment will be created an the new list bound to it. The same procedure repeats if the user goes a step further, and so on ...
If the user pushes the back button to go a step back, the following is done:
The push of the back button is catched via the onPressedBack()-method placed in the parent activity. Via this method an own method is called. This pulls the last ID that was pushed onto the stack and then builds with it in a new created fragment the former list.
At the same time I will run along a counter that counts in which navigation depth I am. In each navigation step forward it get´s inceased by 1. And in reverse just reduced by 1. With this counter I am able to query if the user is in the navigation-root (counter = 0) and once more pushes the back button to leave the current activity. If so, i call finish() and the parent activity with its fragments get´s closed. Voilá!
It works great. Took a little bit time to get over it. But now I am happy that my solution works. :)

Categories

Resources