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

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?

Related

Set initial value to AutoCompleteTextView

Case: I have a screen (EditActivity) with a product creating/editing form. This screen has a layout in which there is a drop-down list product category. The list of items in this drop-down is set via ArrayAdapter.createFromResource. I don't have a problem when I use this screen as a creating form. The problem appears when I use this screen as a editing form. I'm trying to set the product category initial value to the field via setText(). It works, and value appears in the dropdown. But other values from the ArrayAdapter disappear and I can't choose them. I also tried to set value using setSelection(), but I'm not sure this is correct way.
Question: How do I insert the initial value into the dropdown so that I can see and select another values from the list?
EditActivity layout part
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/productCategory"
android:hint="#string/ed_product_category"
...
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu">
<AutoCompleteTextView
android:id="#+id/productCategoryAutoComplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"/>
</com.google.android.material.textfield.TextInputLayout>
Setting list items to autocompletefield. This code is executed inside onCreate method of EditActivity.
productCategoriesAutoCompleteView = findViewById(R.id.productCategoryAutoComplete);
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(
this,
R.array.product_categories,
R.layout.list_item
);
productCategoriesAutoCompleteView.setAdapter(arrayAdapter);
How I'm trying to set initial value 1 (work but not as expected)
int itemPosition = arrayAdapter.getPosition("Car");
productCategoriesAutoCompleteView.setText(arrayAdapter.getItem(itemPosition));
How I'm trying to set initial value 2 (looks like doesn't work)
int itemPosition = arrayAdapter.getPosition("Car");
productCategoriesAutoCompleteView.setSelection(itemPosition);
Field in creation form
Field in updating form
What I want in updating form
setText() method has second optional boolean parameter - filter. This param is used to filter items from array adapter.
This worked for me in one of the kotlin projects I was working on.
I firstly set text of the AutoCompleteTextView. Then set an adapter to the AutoCompleteTextView.
So the code should look like this;
productCategoriesAutoCompleteView = findViewById(R.id.productCategoryAutoComplete);
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this,R.array.product_categories,R.layout.list_item);
int itemPosition = arrayAdapter.getPosition("Car");
productCategoriesAutoCompleteView.setText(arrayAdapter.getItem(itemPosition));
productCategoriesAutoCompleteView.setAdapter(arrayAdapter);

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).

Retrieve position of item within Spinner made up from String.xml array

I am working on an android project and have a spinner which contains items from a string-array which is in the string.xml file.
In the strings.xml I have the following array
<string-array name="array_loginType">
<item>Select Login Type</item>
<item>Website</item>
<item>App</item>
<item>Other</item>
</string-array>
and the spinner contains the following XML
<Spinner android:id="#+id/add_cboLoginType"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:prompt="#string/add_select_login_type"
android:padding="4dp"
android:entries="#array/array_loginType"/>
At some point the user can select the item from the spinner and when submitted it saves the item in the database. I am then allowing the user to edit the details and I am trying to set the selected item within the spinner based on the item that was retrieved from the database. I.e. if the saved item within the database says Website then Website will be selected inside the spinner.
Thanks for any help you can provide.
If you know what position in the array holds the correct selection, you can just use the Spinner.setSelection();-method to set the spinner to display it.
In your example, the Website is found in position 1 of the array (1st actual entry is number 0).
Therefore, your code should look something like this:
// Declare the spinner object
Spinner mySpinner = (Spinner) findViewById(R.id.add_cboLoginType);
// Set the correct selection
mySpinner.setSelection(1, true);
The second argument tells the spinner to "animate" the selection - so it actually displays the correct selection, and not just sets the correct value (if it's set to false or not included at all, the spinner will have changed (so anything depending on the selection will work as intended) but it'll still appear to be at the default selection).
So you want to have the user select a type and save it with some other data in a database and when the user tries to edit that data you want to edit screen to have a preselected spinner, correct?
First you need a OnItemClickListener. This will let you know when the user selects something:
Spinner spin = (Spinner) findViewById(R.id.add_cboLoginType);
spin.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(final AdapterView<?> parent, final View view,
final int position, final long id) {
// update the type field on the data object you are creating or editing
// position is the type index
obj.setTypeIndex(position);
}
}
);
That is how you see the change, now to pre-select is in edit mode:
//editMode boolean.. why not
if (editMode) {
spin.setSelection(obj.getTypeIndex, true);
}

How to change the entry of a spinner in code (Android)

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.

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.

Categories

Resources