Adapter getView() called systematically after closing a Dialog - android

My activity contains a ListView. When I open a dialog (a custom dialog that enherits from Dialog), getView() of the adapter gets called systematically, so there must be a notifyDataSetChanged called somewhere. I dont want my list to be refreshed everytime I close the dialog. How can I prevent this?

It sounds like a lifecycle issue to me... like when you start a new activity from your list activity, and then go back to your list activity... If your list creation code is in onResume, it will be re-run.
Not saying that is truly the issue here, but without seeing your code that's the best guess I can make at it.

if you dont want to set notify data set change then you can save you data temporarily in any object and when you want to update it just save that data into your array list or string array which your are passing in you listview and then call notifydatasetchanged in this way it will update only when you want. It will be better if you will post some code so that we can exactly know what you want to do..

I had the same issue and thought there was nothing I could do about it.
I was adding an onClick to one of my Views when it was visible in the ListView. I didn't know it at the time, but this modification of the View (after getView) was causing the ListView to call getView() on my adapter when a Dialog was shown or closed. (Presumably for view measurement)
Since I couldn't re-add the onClick easily, I changed my code to add the onClick inside the getView() and didn't modify the view afterwards.
Turns out that now since I didn't modify my view at all (after the original getView() was called), the getView() doesn't get called on Dialog show or closed.
Not sure if this solves your issue also, but it may help others.

Related

Listview with Filter won't refresh until keyboard is hidden or return is pressed

I have a listview which displays a list of contacts. I added an EditText to the layout above the listview so i can search using a Filter. I have implemented this using an onTextChanged Listener on the EditText. My listview adapter is a custom adapter which extends BaseAdapter and implements Filterable. My filter works and does the job correctly. The problem is, while i'm typing, the dataset gets refreshed but the listview itself doesn't refresh until i hide the keyboard or press the return key. If i try to click on the listview after i enter some text into the EditText but before i close the keyboard or press return, I get this error :
java.lang.IllegalStateException: The content of the adapter has changed
but ListView did not receive a notification. Make sure the content of
your adapter is not modified from a background thread, but only from the UI thread.
Now i'm definitely not modifying the content of the adapter from a thread other than the UI thread.
Could anyone explain what might be causing this problem?
Actually, I've found a workaround by calling Activity.onContentChanged() but i'm still curious as to why i actually need to refresh manually.
I have finally found a way to do this. The thing is, the dataset was refreshing when i call notifyDataSetChanged();, but the views weren't getting redrawn, I have no idea why (Could have something to do with me using the ViewHolder pattern maybe? Just a random guess...). So what i did is call getListView().invalidateViews(); and the views get redrawn with the correct data.
you can use setNotifyOnChange() which automatically call notifyDataSetChanged(). it actually notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
I had the some problem and I tried a lot of solutions without good result.
I solved this issu by adding android:hardwareAccelerated="true" in manifest file for my activity.
Try to use this solution it can help you

How to refresh Expandable Listview's groupview

I want to achieve a function of when I click the group the activity will do something background and when it has finished ,I want the groupview has something different.So I want to know how to fresh the groupview.
Well you better post some code to get the right advice on your issue but based on the text inside your question I can suggest these steps,
setOnGroupExpandListener(new OnGroupExpandListener(){..}); on your expandable listview.
You will get onGroupExpand method where you will write your logic to refresh or do whatever you need to do in background in an Asynctask obviously.
Once you are done with the background work then inside onPostExecute() of your Asynctask set the adapter again or just call notifydatasetchanged if you have modified the data a bit.

Is it okay to change a ListView's adapter dynamically?

Instead of creating multiple activities, I would like to change the ArrayAdapter of the ListView as needed. I don't see any mention in the API about whether or not it is okay to call setAdapter() more than once.
To be more specific, say I would like to start an activity that has a ListView. In this example, the ListView is initialized with a listView.setAdapter(this) from, say, a CategoryArrayAdapter.
Then a user selects a category. Without starting a new activity, the code will set a new adapter for the same ListView. The new adapter, say ItemArrayAdapter calls listView.setAdapter(this).
Does someone have experience having done this successfully or know of a specific reason why this shouldn't be done?
I don't see any mention in the API about whether or not it is okay to call setAdapter() more than once.
The simple answer is YES, and I have done similar sort of things before.
This is exactly the reason why Adapter is existed and provided in the API. The actual content (Model) and how it is rendered (View) for each list items is isolated and implemented inside android.widget.Adapter, instead of directly bound to android.widget.AdapterView. As long as your adapter is properly implemented, you can swap/change the actual underlying adapter that bound to the ListView, simply by calling the setAdapter() method.
Resetting the adapter is ok, but notice, that there might be a GUI glitch when doing so, as the view whose adapter is being changed has to be redrawn with the new data. Aside from this you should be fine.

Android ListView - how to execute a code when items are ready?

I have a ListView which gets items from a db (using SimpleCursorAdapter). After the list is populated I want to scroll it to a given position. I can't find the right place to do this.
Right now I set a "need to scroll" flag before running the query. Then I check this flag in setViewValue() and if it's set I scroll the list and reset the flag. Although this works, I don't think it's the best way to do it.
Any other ideas about how to perform some actions when a ListView is populated?
Perhaps you're looking for a "scroll-to" like method. Here's a quick reference.
Ok, I think I solved this. Derived my own adapter from SimpleCursorAdapter, overloaded notifyDataSetChanged method so that it scrolls the list when necessary after calling super's method. Looks working.

Android: ListView Listener?

I am wanting to display a text field stating that my listview is empty if there is nothing in my list. I know this can be accomplished quite simply using an android:id/empty textview in the xml file, but this also requires that I extend my activity with something other than Activity and I don't want to do that.
I guess my only solution is to create a textview and then set its visibility to gone when my code detects that something has been added to the listview. I can simply check the array that populates the list, but is there some sort of listener so I don't have to run a checkListIsEmpty() constantly throughout the code. I was hoping I could use something that would simply sit quiet and wait for the listview to become populated and when that happened change the visibility to visible and then begin waiting for it to become empty again.
Or you can use setEmptyView(View v) on your ListView in your Activity.

Categories

Resources