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.
Related
How to set the string values on spinner prompt?
The process how I am doing is:
I am able to retrieve the json data(it has only one single value), so I am getting the single text with the help of String.
Later, I am trying to set the string value in Spinner prompt
SP_gender is called as Spinner here. In String gender1, I have texts called as "male"
String gender1 = i.getStringExtra("bundle_CusGender");
SP_gender.setPrompt(gender1);
System.out.println("Check bundle_CusGender = : " + gender1);
When I try to print this, I am getting a value as male
System.out: Check bundle_CusGender = : male
How should I set the single text in spinner Android?
Firstly the setPrompt() methods documentation states:
Sets the prompt to display when the dialog is shown.
Which is pretty vague but internally it calls the setPromptText() method which sets the description on the popup and has the following documentation.
Set hint text to be displayed to the user. This should provide
a description of the choice being made.
So if I am understanding your question correctly you want to set the spinners selected item. You should be using either setSelection(int) or setSelection(int, boolean)
For those to work you would need to give your spinner an adapter if you haven't already. If you don't know how you should check out this answer which explains in more detail.
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.
When normally populating a Spinner as I have done in the past I normally use a SpinnerAdapter then normally have items in resources to populate it.
I have currently though a different query, I have in my code a user input for an int and I want my spinner to populate with numbers up to the user selected number. So if the user enters the number '5' it is saved to an int variable. I then want the Spinner to show 1,2,3,4,5 as choices.
I am really not sure how I would approach this.
Thanks, Oli
Edited
Below is a basic example of how you would add Integers to your spinner :
mspin=(Spinner) findViewById(R.id.spinner1);
Integer[] items = new Integer[]{1,2,3,4};
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, items);
mspin.setAdapter(adapter);
You can refer to this and make changes in your project as per your logic. Also in your case you should use an ArrayList of integers since the number of choice of the user seems to be dynamic. you can create an arraylist and replace in for the Integer array in the above code.
Hope this helps!!
Hi I have a spinner for which I would like to change its entry. I have created an array in the values folder. I know that I can edit the entry of the spinner by right clicking on it. But I want to know, how can I change the entry of the spinner using code. I was hoping there would be something like spinner5.editEntries
Can someone help please?
Spinner Spinnermiles = (Spinner) findViewById(R.id.Spinnermiles);
String [] arrmile ={"5","10","20","30","40","50","70","80","90","100"};
adapter = new ArrayAdapter<String>(Searching.this,android.R.layout.simple_spinner_item,arrmile);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinnermiles.setAdapter(adapter);
If you want to change item in spinner at position 3 (which is "30" in example),
Set value at that position e.g.
arrmile[3] = "enter new value you want";
and after that call
adapter.notifyDataSetChanged();
then value at that position will be get updated.
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?