how set spinner from previous selected value using spinner object? - android

CustomeSpinerAdapter jobAdapter = new CustomeSpinerAdapter(
getApplicationContext(), R.layout.custom_spiner_row,
jobListData);
spJobType.setAdapter(jobAdapter);
here i want to set spiner.setseleciton(position); with value pair
my previous value form the data base is jobID=44

i think the best practice for this solution is the SharedPrefrences Class.
SharedPrefrences AppData = PrefrencesManager.getDefaultSharedPrefrences(this);
AppData.edit().putString("prevSpinnerValue",jobID);
and for getting there is simple snippet too:
AppData.getString("prevSpinnerValue",null);
and set the selected item of your spinner to above value that you get from prefs.
maybe this help.

Related

How to set a particular position as default in spinner?

I am getting list of data from server and setting in spinner through setAdapter, but what data is coming on 3rd position I want to set that as default(0th position). Ex. {Mango, Banana, apple} ; in spinner apple should be default instead of Mango
else if
(mListener.getSelection().get(0).
getGenLovs().get(i).getLovId().
equalsIgnoreCase(File_Key.AB_CUST_TITLE))
{
binding.spinTitle.setAdapter(new
GenLovsSpinner(getContext(),
mListener.getSelection().get(0).
getGenLovs().get(i).getValDes()));
}
I have tried this
String cls=
String.valueOf(mListener.getSelection().
get(0).getGenLovs().get(i).getValDes().get(3));
binding.spinTitle.setSelection(Integer.parseInt(cls),true);
Here when I am using above code I am getting NumberFormatException
binding.spinTitle.setSelection(Integer.parseInt(cls),true);
use this insted of above line
binding.spinTitle.setSelection(Integer.valueOf(cls));
See you are setting up any list or array to spinner adapter.
If you want to set particular as default then try this for example :-
Let you are setting dataList to spinner adapter
after setAdapter() for selection
either spinner.setSelection(dataList.indexOf("apple"),true) or
spinner.setSelection(2,true) as your third data has index 2
Just give binding.spinTitle.setSelection(2);
try this
Use the following: spinnerObject.setSelection(position).

Android pass value to Spinner

I have to pass a value in the sqlite db spinner of another activity.
the value of the step as follows:
extras.putString ("category", tv4.getText().toString ());
But then as I insert it in the spinner?
spinner.set ...... (i.getStringExtra("category"));
Presumably your spinner has an adapter and an array that fills that adapter. You need to first find the index of the string you are passing in from the array. Then you will set the spinner's position to that index.

How to add new value to listpreference and save it?

I have a list Preference that created from resource xml. I added preference that created dialog in which user can add value to listPreference.
using those methods i added new value:
entries = getEntries();
entryValues = getEntryValues();
when user is adding values to listpreference, its displayed. But when preferenceScreen is recreating new value disappearing.
How can i save those new values?
Problem is that when you're reopening your PreferenceScreen, it loads the ListPreference's values from XML. You can change this behavior using the setEntries() and setEntryVaues() methods of ListPreference. Of course you need to somehow store all the values and their indexes that your users enter. You can use databases or SharedPreferences for it. Hope this helps.
EDIT
Saving the value of a ListPreference into the SharedPreferences:
preferences.edit().putString(listPreference.getKey(), listPreference.getValue());

How to put text from spinner to file?

I have this problem. I read with BufferedReader text from one system file, this text contains for example 5 WORDS, but in another case it can contain less or more words, then I put this text (these words) to ONE string and save that string to shared preferences. Then I make spinner from this string,
Code here:
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, yourString.split(" "));
spinner.setAdapter(spinnerArrayAdapter);
And now if for example spinner contains 5 options (5 words) and user select some of these words, I need to put this word to one system file. I use echo command for insertion. So the best thing would be if I could save chosen word from spinner to shared preferences as string. I use if(possition==0) for selection in normal spinner, but I think it's not possible to use it in this case.
Could anybody help me?
you can get selected text by writing this line of code
spinner.getSelectedItem().toString();
SharedPreferences pref = getSharedPreferences(
"Preferences", 0);
SharedPreferences.Editor edit = pref.edit();
edit.putString("ABC", spinner.getSelectedItem().toString());
edit.commit();
not getting your complete idea what you want to do...
but i think you want to get text which is selected in the spinner, then split your string in one array pass this array in your spinner adapter.
After that use onItemSelection method, In this method u will get position and fetch the corresponding record from your array.
Try using the getPosition() method on the ArrayAdapter.
spinnerArrayAdapter.getPosition(savedstring);
If the saved string is in the array second time in then you can call setSelection() to that position otherwise let the user know (e.g. via Toast) that the previous selection is no longer valid.

Android : Set a value from an external file in a spinner

I'm stuck on a problem with spinners.
Actually, I have some tabs with spinners and EditText that I create without any porblems. I have to keep the datas that the user type in an xml file. So I create an xml file and it workd fine.
I also have a loading tab that permis to load datas saved in this xml. So I load the file, I parse it and I fill my EditTexts without any problems.
The problem is from the spinner : I can't put the data from the XML I created in my spinner.
I tried by saving it as a string and then I tried to load it in the spinner by this way :
(Spinner) spinner.setPrompt(string);
That doesn't work, I have the default value but not the saved value.
I also tried to save the integer of the choice made by the user. And then to reload it by this way :
(Spinner) spinner.setSelection(Integer.parseInt(string));
I don't think I use it properly cause I have a FC.
So I don't really know how to proceed to load the value from the xml in my spinner.
Any idea ?
Thanks !
Try this:
String myString = "some value"; //the value you want the position for
ArrayAdapter myAdap = (ArrayAdapter) mySpinner.getAdapter(); //cast to an ArrayAdapter
int spinnerPosition = myAdap.getPosition(myString);
//set the default according to value
mySpinner.setSelection(spinnerPosition);
From:
How to set selected item of Spinner by value, not by position?

Categories

Resources