I have spinners in my fragment. Selecting each option of my spinners gives me different scores fetching from database. Now, I don't want to fetch the data from database if I select the same option from spinner consecutively. In my current code it is fetching the data on each selection. Is there any way that I can maintain the previous selection of spinners, so that I can give a check to database hit. Thanks.
I tried to solve the above problem and was able to find a good solution. I created a custom spinner taking a reference from this answer https://stackoverflow.com/a/11227847/2734855
The overridden method setSelection(int position) of Spinner class actually gives the event hit back to the OnItemSelectedListener which helps to track the previous spinner selection.
Related
I want to know if there is a specific reason why you cannot use OnItemClickListener with a spinner in Android? Looking through older posts ( setOnItemClickListener Not Works with Spinner and I have an error: setOnItemClickListener cannot be used with a spinner, what is wrong?) it seems as though people don't even bother with OnItemClickListener and rather go straight for OnItemSelectedListener. Why does Eclipse give OnItemClickListener as an option for a spinner if it can never work?
Some context- I'm not really too bothered by which method I use. As soon as the user selects an item from the spinner, I want to make a second spinner visible and populate it from the database based on the option selected in the first spinner. Now when I use OnItemSelectedListener, the second spinner is immediately set to visible. Is there a workaround for this?
Why does Eclipse give OnItemClickListener as an option for a spinner if it can never work?
Because everything extending AdapterView has setOnItemClickListener(). Just because some subclasses ignore it does not mean that the setter magically disappears.
Is there a workaround for this?
There is no workaround, because there is no problem, because it is doing exactly what you say you want to have happen ("I want to make a second spinner visible and populate it from the database based on the option selected in the first spinner"). A Spinner always has a selection if there is data to select from. Hence, your second Spinner should always be visible, since it is "based on the option selected in the first spinner", and there will always be an "option selected in the first spinner".
It sounds like what you really want is to only have the second Spinner visible for specific selections in the first Spinner, where you set up the first Spinner where there is some selection that, in business terms if not technical terms, means "no selection". You are welcome to do this, but then you will have to implement the logic to handle this, only showing the second Spinner when an appropriate value is selected in the first Spinner.
i have an app that has a listview. each view inside of the listview has custom xml and has buttons checkboxes and textviews. i have to get the previous saved settings for the checkboxes from an sqlite database and set them. and if the user changes the checkboxes or radio buttons i have to save those settings in the database.
tried several different ways to do this and most resulted in error and would not display the correct setting for checkboxes when coming back to the page.
what finally worked is the design shown below. where i put any database calls as close as possible to the actual view. however it looks like if there is many rows in the listview it would make many calls to the database and it could get slow.
is my design correct or is there a better way
psudocode
onCreate
instantiate arraylist adapter and set adapter to view
end onCreate
arrayadapter class
getView(){
call databsase and get previous settings for checkboxes in this view and
set the checkboxes to show checked or not depending on database saved settings
onClicklistener or oncheckedchanged listener for getting checkbox positions if changed
call database and set method to store updated positions of checkbox, radiobutton or spinner
}
end arrayadapter
Your approach is correct and your concern is warranted. I can suggest you the following method, but its performance will have to be tested:
onCreate
instantiate arraylist adapter and set adapter to view
end onCreate
arrayadapter class
getView(){
call databsase and get previous settings for checkboxes in this view and
set the checkboxes to show checked or not depending on database saved settings
----> Create two ArrayLists: ArrayList<Boolean> for CheckBoxes
----> and ArrayList<Integer> for RadioButtons.
----> Fill these lists using the values from the databse
onClicklistener or oncheckedchanged listener for getting checkbox positions if changed
call database and set method to store updated positions of checkbox, radiobutton or spinner
----> Here, update the lists, not the database.
----> Execute a AsyncTask to update the database whenever the lists change.
}
end arrayadapter
Another think you can do is to load data from database in Activity.onStart() and save it back in Activity onPause(). All other time use ArrayList to track changes in UI.
In that case you will have this heavy db operations much rarely during the Activity life cycle.
I'm wondering if there's a setText() method in spinners? What I want to do is display the selected item in a spinner (it's saved via sharedpreferences.) so the next time, the user displays the saved data, it should display in the spinner. How can I do that? I already know how to do it an edittext like this : age.setText(age);
But wondering how to show it in spinners.
There is no such method in Spinner class.
You need to implement a SpinnerAdapter which will be responsible for creating the items of the Spinnerand attach it via the setAdapter() method.
The official Spinners guide may help you: http://developer.android.com/guide/topics/ui/controls/spinner.html
I am showing list of all the applications installed on the phone with the help of the packagemanager in the listview and there is a checkbox along with every application name shown in the list. I want to save the name of the applications against who the user has selected the textbox. The saved items will be used to generate a list of favorite apps.
You can Save it in the your database while the user presses on the check box and delete if it is already present in your database if the user unchecks it.You can use listener and put your code of saving inside your listener.
You should add a listener to your checkboxes => OnCheckedChangeListener
You will then be notified each time the state of the checkbox change, if the checkbox is checked, you can add the element to a list, if it is unchecked, you can remove the element from your list.
This requires you to have your own adapter thought.
Good luck and have a nice day
You need to make custom listview with checkboxes : here is one example for you.
You will need to have one Button below/above the Listview clicking on what you can take all the items and store it into your database : a complete Articale for that.
Now you can fetch the data from database anytime to show those favourite apps.
Looking to create a menu toggle that switches between two possible arrays for a spinner. For example, if the spinner is set to show array A, then when I press this menu button, I want the spinner to be set to array B. If I press it again, I want it to be set back to array A.
I can handle the if/then statements and all, but how do I call the spinner's array adapter? I know how to call its value using getSelectedItemPosition(); but is there a similar method for retrieving WHICH array it is currently set to?
I think AdapterView#getAdapter() is inherited by android.widget.Spinner?
What you're seeing is that AdapterView#getAdapter() is an abstract method. You want to be using AbsSpinner#getAdapter() which is where getAdapter() is actually implemented. This will return a SpinnerAdapter.