Update ListView or Activity? - java

I have a listView inside Activity inside tab layout. If I long press the item, I get the option to rename the item. Once the item is renamed, the change can't be seen until the activity is restarted.
I tried solving the problem by simply creating a new intent and re-opening activity, but since that activity is inside tablayout it doesn't work. I also tried re-opening tabLayout activity but then it automatically goes to tab 1, while I'm trying to refresh listView inside tab 2.
So then I tried solving it by creating updateListView() method:
public void updateListView(){
listAdapter.clear();
listAdapter.addAll(recordedFilesArray);
listAdapter.notifyDataSetChanged();
}
But that doesn't work either. When I'm using this method, it completely clears listView and then I have to restart the activity again to see the results.
So, anyone have any idea what I could do to see listView changes without restarting the activity? By the way, if it helps, I'm reading ListView (ArrayList) from a text file.

Try using notifyDataSetChanged() only without clearing your listAdapter

You can make a separate adapter class and implement its getView() function to manage this. Your activity will pass the array list to the adapter. The adapter's getView() will take care of displaying the contents from the list.
When the list gets modified, you need to reinitialize the adapter. So that way getView() always have the updated data to display on the screen.

For renaming the item, you can change your item object in the ArrayList,
and then call adapter.notifyDataSetChanged().

I found the problem. The ArrayList wasn't changed until the activity was restarted. I had to call arrayList.set() to change the element in the ArrayList. Now it works as it should.
My code:
recordedFilesArray.set(toDelete, input.getText().toString()); //toDelete is arg2 variable (onLongItemClickListener)
listAdapter.notifyDataSetChanged();

Related

Using ArrayAdapter with Room

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!

Android - Scroll to position in ListView in ListFragment after data of adapter has changed

I am using a ListFragment with a custom adapter, which is used to inflate the views. The data is loaded via an AsyncTaskLoader once the fragment is created.
My problem is now that I want to scroll (not smooth scroll) to a certain position to show the last selected element, i.e. The fragment gets destroyed when I select an element and I want to show the same position in the list once the fragment is displayed again.
So creating the view and everything works fine, but I do not know where to add the getListView().setSelection(x) line to scroll to a certain position. I tried to invoke it after clearing and adding the elements to the adapter in Loader<?>.onLoadFinished() function, but that does not work, I get the following exceptoins:
java.lang.IllegalStateException: Content view not yet created
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
at fs.pokemon.effect.PokemonFragment.updateData(PokemonFragment.java:239)
at fs.pokemon.effect.PokemonFragment$2.onLoadFinished(PokemonFragment.java:129)
...
My question is: Is there a function/callback so I get notified, when the ListView has finished updating its content? Or how else can I scroll the View to a position after the content has loaded?
Thanks to pskink comment I tried it again and noticed that the exception does not occur in setSelection() but rather in getListView() - I though my loader would reload data after the view was created, but instead it used the cached results, so onLoadFinished() was actually called, before the initialization of the fragments view was finished, thus causing an exception.
The correct way to do it is to determine the position in onLoadFinished() and then override the fragments onViewCreated() and execute getListView().setSelection(curPosition) there.
Probably also the case that the loader cleared its cache and the onLoadFinished() will be called after the onViewCreated() method has to be considered, but so far it works fine.
Set the transcript mode and stackfromBottom for the listview while your listfragment activity is created. Now every time the listview is updated it will be scrolled to bottom to the latest item.
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
getListView().setStackFromBottom(true);
}

adapter.notifyDatasetChanged() without changing current view

Is there a way to add views to a ViewPager (by adding data and calling notifyDatasetChanged() on its PagerAdapter) without changing the view currently being displayed?
I want the dataset to update in the background while the user is viewing other views. My problem now is that, when it finishes updating, it goes back to displaying the initial view. How can I prevent it from doing so?
As far as i know if you call notifyDatasetChanged() it only notifies the pager adapter that there is new data, the data has to be there before. so i think you should just not call notifyDatasetChanged() and when the user goes to the next view, the pageradapter loads the current (new) data. What about this solution?

How to update a listview within the custom adapter?

I have a custom adapter which extends ArrayAdapter, it includes checkboxes for each item in the list.
Within the getview itself I have the onCheckedChanged listener that deletes a item from the database if checked. However the listview does not update, and I cant call the notifyDataSetChanged() as I am not in the main class. Any ideas how?
Post the code so that readers can understand your problem.
You dont have to set any listener in the class that is extending ArrayAdapter. In this class we only inflate the data to the views.
Remove the onCheckedChanagedListener from Adapter class and place it in the ListActivity or Fragment that is using this Adapter.
Call invalidate() on your ListView. That makes the list to be refreshed and displayed again. Maybe it will solve your problem.
And I don't understand why you can't call notifyDataSetChanged() ?

Updating ListView fragment when not focused

I'm working on a tablet application with a ListView fragment on the left, and some other views including a SearchView (essentially a TextView) on the right. When the SearchView is focused, I want to update the data of the listview, but I can't. If I notifyDataSetChanged on the UI thread, nothing happens. If I call setAdapter() again on my listview, the listview goes blank.
Only when I touch the listview again (it gets temporary focus again or something to cause getView() to be called on the adapter) does it get updated.
Anyone got an idea on how to fix this?
Min-Sdk and Target is 4.0.3.
Did you try to call your refresh routine using runOnUiThread() ?
Reform your objects list, bind to a new instance of your adapter, then call notifyDataSetChanged()and setAdapter() again in refresh routine would work.
You have to use a ViewHolder to handle updates in a ListView I think.
What now works for me is updating the data in the underlying adapter and then calling notifyDataSetChanged on that adapter, all from the UI thread.
I did not need to call setAdapter again on the listView.
Of course I had already tried all this before asking the question, so not sure what made it suddenly start working.
Call it inside the runOnUiThread method. Also add the new object to the adapter inside it.
Like that:
runOnUiThread(new Runnable() {
#Override
public void run() {
chatListAdapter.add(message);
chatListAdapter.notifyDataSetChanged();
}
});

Categories

Resources