Android: ListView Listener? - android

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.

Related

Access Adapter items of Recyclerview

I'm using Recyclerview to show a list. I want to delete some items like IOS. In my listview template I have added a button to delete item which is invisible by default. In my activity I have another button attached at bottom (Not part of listview) and on tap of this button I want to make all delete buttons of listview visible.
My Question is how can I get reference to all delete buttons of listview in activity and is it the right way to do this?
Thanks
Assuming you have ViewHolders set up, you already have references to all the buttons in your list. All you have to do is to make them visible for every item in the list with a simple loop.
In case you haven't implemented ViewHolders I suggest you check out the documentation and take a look at some simple tutorials on how to use them.
On a side note. If I understood correctly you're making a bottom tab for your app and since you referenced iOS I gotta say this; Remember that Android and iOS are two unique operating systems with their own ways of handling things. Check out Googles pure Android documentation.
In your question title you say RecyclerView, but in your text you say ListView. The solution is similar either way, but it's best to be perfectly clear what you're doing.
In either case, there are at least two different solutions.
First, you could use a boolean flag to determine if all the the item buttons should be showing or not. You check this flag at the time the item view is inflated or created and toggle the button accordingly. If the boolean flag is ever changed, the easiest thing to do is tell the RecyclerView/ListView that the underlying data has changed and to redraw all the views. Call notifyDatasetChanged on the adapter.
The other thing you can do at the time the item buttons should change is iterate all the visible item views, find the button, and change its visibility. With RecyclerView, you can do this, and with ListView you can do this.

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

Android EditTexts AutoSave

I have a database object which includes a lot of String fields. Right now, these string fields are managed by the user through an activity with a lot of edit texts. However, I'm having trouble saving all of the information from my EditTexts to a database. I've tried doing this when the activity calls onPause but it is not working how I would like (I'm using a ListView with the EditTexts so it's hard to say if the views will be there). I've been looking at the text watcher but it seems really tedious to add one for every EditText, some of which are created dynamically.
I've considered extending EditText and implementing something to use the TextWatcher but I'm again not sure about the best way to go about this.
Anybody have any suggestions on how I can accomplish this? Thanks for the help.
I would say you should hold a reference to each of these EditText objects in an ArrayList and then use an array list adapter to provide the data to your listview.
when any of the EditText's are changed you can call notifyDatasetChanged on the adapter.
in the onPause method you can loop thru the ArrayList and save each to your database.
Couple of ideas spring to mind:
1.
Implement a save button at the top/bottom of the list view or in the action bar.
The save button onclicklistener would grab each text in the list view and update the relevant fields in the database.
Place the saveButton work in the UI thread so it will block until completed and the activity won't be destroyed. you could also create a progress dialog just to let users know it's being updated.
2.
Implement a custom adapter for your listview and in the getView() method of the adapter (you have to override this anyway) add a text watcher dynamically... that way it's not all that arduous for you to add them individually

Mono for Android: Listview scrolling issue with spinners and edit text

My problem is on two fronts.
First issue: Scrolling amnesia
I have a ListView with spinners and edit texts. It acquires it's data from webservice. The problem is when I write up a value on the EditText or select something for the spinners and scroll them out of view. When I come back the fields are empty and the spinners are again in their default selection.
Attempted solution
I have tried resolving the issue by setting ScrollingCacheEnabled programmatic and within the AXML file to both true and false just to see if that is an issue. It seems not to have any kind of an effect.
Second issue: Focus Loss
When I touch the EditText within this same ListView I get the keyboard to appear but I loose the focus on the field and it needs to get touched again to get focus and it allows me to be written.
Attempted solution
I fiddled with setting the fields focusable, Focusavle in toucmode, touchable and whatnot but came out empty handed.
Honestly I am quite new to android and to programming on this level as well but I tried my best on this. I might have just missed something due to lack of knowledge or it's just something somebody with more experience could tackle and solve.
The second issue isn't that bad for now (still after filling out quite a number of fields it does get tiring to set it twice...) but the scrolling issue is a must.
I think you problem relates to that you forget to update the items in the Adapters when you alter the Views containing them. So you need to wire up the events from the Views to update the items.
Why? If you look carefully at your Adapter for you ListView you populate the convertView with the values of GetItem(position). So if that item does not reflect the changes you have made to the View you are bound to get the initial values of that item.
So what you need to do is to hook up SpinnerValue.ItemSelected and all the other Views which can be altered events, so that the items in the Adapter get their values updated. Remember to only add event handlers when you first create the View. So that is when convertView is null.
Also consider changing the lines:
if (SpinnerValue.Adapter.Count.Equals(2))
{
SpinnerValue.SetSelection(1);
}
To be based on the items in the Adapter rather than setting it to 1 every time.
You focus problem is based in that ListView is not really made for having Views inside of it wanting the focus. Try setting the ListView DecendantFocusability when you instantiate it to DescendantFocusability.AfterDescendants like so:
lstPrevzem.DescendantFocusability = DescendantFocusability.AfterDescendants;
I resolved the issue by replacing the listview with LinearLayout and pinning the adapter to it. The "fixed" code is available on pastebin.
Fixed code:
http://pastebin.com/vn3SPrFz

Adapter getView() called systematically after closing a Dialog

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.

Categories

Resources