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

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);
}

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 get the index of string-array

In strings.xml I have string-array which populates the spinner.
I have an note app where user can select subject from spinner and show in recycler view.
So after user added note and if the user clicks the note then it should redirect to that spinner and auto select the subject which is selected previously.
I tried to set position as sub_spinner.setSelection(subPosition) but I can't figure out how to get the subPosition
String.xml
<string-array name="subject">
<item>Communication English</item>
<item>Computer Graphics</item>
<item>COA</item>
<item>Data Communication</item>
<item>Instrumentation II</item>
<item>Software Engineerinng</item>
</string-array>
So if I have sub as COA then i should get index of 2.
Since you have the subject string-array in the xml. You can get the string array in your java code by using
List<String> list= Arrays.asList(getResources().getStringArray(R.array.subject));
Then in order to get the index of the selected subject, you can can implement an OnItemSelectedListener on the Spinner or ListView; anything that you have populated your string array on.
The OnItemSelectedListener implementation has a callback like onItemSelected with parameters, you want the position which provides the position of the selected item. Then you can do:
setSelection(position)
inside of the OnItemSelectedListener. If you need further guidance on how to implement, you can post some more code and I will provide you the exact code to do this.

How to get certain field from the arrayList object on spinner selection

I just created a spinner and make this spinner read from the database. and display them in the spinner. I already did that. But I want to right down the Id of the selected item.
for example : when I select the first raw called (1, tyat abdullah, sergurey )
I need the Id (1) only to be written down without the full record.
I need the Id (1) only to be written down without the full record.
Then when you are doing labels.add, only put the data you want to see
You can also use doctorsList.get(position) within the selection listener, and set the other text view accordingly.
For example, doctorsList.get(position).getId() seems to get the information you are asking for
simply use this to get the selected item index
int index = spinner.getSelectedItemPosition(); //this will give you the seleected item index
is that what you want ?
leme Know :)

Get the respective value from the array according to selected item position

I have a spinner and I'm getting its selected item position by MyOnItemSelectedListener. By the time I'm using an array adapter to load items to the spinner. I have loaded the items and it works perfectly. But I have a small problem. When I doesn't select a value then it shows position 0 but it has got the value of 1st array value. As shown in the below image,
What I want to do is when I haven't select a value then it should get any value from array. And when I select 1st item then only it should get 1st array value.
Array is a created from json response so it is not possible to add a item to array manually.
I have used How to make an Android Spinner with initial text "Select One" (aaronvargas's answer) to add slect option to spinner as 1st selection.
How can I achieve this? Any help will be highly appreciated.
You can use the logic like :
int selectedValue = -1;
if(position<=0){
//Means Item not selected
}else{
selectedValue = array[position-1];
}
I think you can use something like I have shown.
if(position==0){
variable = 0;
}else{
//use ur logic here
}

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.

Categories

Resources