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

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
}

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

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

How to get Android custom adapter element position

How to retrieve position of an element from a custom adapter based on element name.. I have a cityAdapter that contains city name and id in key value format. I am trying to retrieve position by the following code
SpinnerModel model=new SpinnerModel();
model.setKey(""+currentLocation);
model.setValue(currentLocationValue);
int position = cityAdapter.getPosition(model);
Position is returning as -1.
Can any one please guide me how to solve the issue.
You can easily get position of a particular item on click of a list,
is there any particular reason so that you don't want to get item position on list click?

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.

Categories

Resources