finding the array index value for the selected android spinner item - android

during registration user can select the titleName from a android Spinner. Im loading data to the Spinner from xml class. I want to pass the titleName as well as title ID,
How can I get the value of selected titleName's array index?
eg- if user selected Mr then i want to pass the value 0.
my java code
states = getResources().getStringArray(R.array.title_array);
titleSP = (Spinner) findViewById(R.id.registerTitle);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, states);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
titleSP.setAdapter(dataAdapter);
final Button button = (Button) findViewById(R.id.btnRegister);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
String TitleName = titleSP.getSelectedItem().toString();
MY title.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title_prompt">Choose the title</string>
<string-array name="title_array">
<item>Mr</item>
<item>Mrs</item>
<item>Miss</item>
<item>Dr</item>
<item>Rev</item>
<item>Ms</item>
</string-array>
</resources>

it the position parameter in your Adapter. You can retrieve it implementing OnItemClickListener. The third parameter of onItemClick
is the value you are looking for

Use the Spinners OnItemClickListener
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String[] titles = getResources().getStringArray(R.id.title_array);
String selectedTitle = titles[position];
}
});
The position variable gives the position of the item in the spinner, which you can match directly to the position of the title in the data source.
Edit:
You can use the built in method of a spinner to get the current selected item position. So if you select the first item, you would get selected item position 0. As this is based on your data source, selected item position 0, would be the same as index position 0 in R.id.title_array which would be "Mr".
titleSP.getSelectedItemPosition();

Related

How to send value to server based on user clicked in spinner item in android?

I have a spinner in my activity which contain Operators name ,what I want when user selected item from spinner I want to send another value to server,like
if user select "Airtel" from spinner I have to send "AR" to server same like other items .how can I do that.
code:-\
<string-array name="operators">
<item>Select Operator</item>
<item>Aircel</item>
<item>Airtel</item>
<item>BSNL</item>
<item>Idea</item>
<item>Vodafone</item>
<item>MTNL Delhi</item>
<item>MTNL Mumbai</item>
</string-array>
here when user select item from above list I have to send value from below list according to above items.
<string-array name="Operators_Code">
<item>AC</item>
<item>AT</item>
<item>BS</item>
<item>ID</item>
<item>VD</item>
<item>MT</item>
</string-array>
A Spinner, similar to ListView, RecyclerView, etc., is an "adapter backed" View. That is, it gets the items to be displayed from an Adapter.
When you set the entries to be shown using android:entries="#array/operators, Spinner internally creates an Adapter with the supplied array items. This simple solution, however, doesn't support complex objects.
For your use case, you'll have to create a custom Adapter for your Spinner.
public class Operator {
String name;
String code;
#Override
public String toString() {
return name;
}
}
final List<Operator> operators = new ArrayList(7);
operators.add(new Operator("Select operator", null));
operators.add(new Operator("Aircel", "AC"));
operators.add(new Operator("Airtel", "AC"));
....
Next, set the adapter on your spinner:
final ArrayAdapter<String> operatorsAdapter = new ArrayAdapter<>(context, android.R.layout.simple_dropdown_item_1line, operators);
spinner.setAdapter(operatorsAdapter);
That's it. Now if you want to listen to user's selections, add a listener to your Spinner by:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final Operator selectedOperator = operatorsAdapter.getItem(position);
final String selectedOperatorCode = selectedOperator.code;
// TODO: Send the selected operator to the server.
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Spinner, not set value if i dont click(android)

I have a spinner:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int childposition, long id) {
textView.setText(spinner.getSelectedItem().toString());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
textView.setText("");
}
});
Above you'll see that textView - is my text object. I'm displaying a text item spinner in the textView when I click it. If I dont click the spinner then my textView must be textView.setText("");
But the spinner is always set text in my textView, even if I do not choose spinner.
Question
How can I accomplish this?:
If I dont choose item spinner, textView is empty: textView.setText("");
If I do choose the item spinner, textView gets: textView.setText(spinner.getSelectedItem().toString());
String item = parent.getItemAtPosition(childposition).toString(); //Get selected item
if(item.equals("spinner")){ // Check if it equals spinner
textView.setText(item); // Set text to item
}else{
textView.setText(""); // If it doesn't equal spinner set text to ""
}
If I understood the question right, putting this instead of textView.setText(spinner.getSelectedItem().toString()); and deleting content of onNothingSelected should do the trick.
UPDATE
I finally understood what you mean. To do this create your spinner like this and add "" as first choice in your string-array resource :
String[] newArray = getResources().getStringArray(R.array.yourArray);
List<String> myResArrayList = Arrays.asList(newArray);
ArrayList<String> spinnerItems = new ArrayList<String>(myResArrayList);
//Making adapter with ArrayList instead of String[] allows us to add/remove items later in the code
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, spinnerItems);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinnerItems.remove(0);
adapter.notifyDataSetChanged(); // Here we remove the first choice which is "" so the user won't be able to select empty.

How to review the selected item on spinner

Here is my code for my spinner.. i want it to save to my database by using setText but theres no option for setting it into setText.. this is how i want it to save on my database spinpstat.setText(student.status);
this is how i want it to declare in my onClick(View view)
student.status = spinpstat.getText.toString(); but it gives me following error
spinpstat = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapterstat = ArrayAdapter.createFromResource(this, R.array.statuslist, android.R.layout.simple_spinner_item);
adapterstat.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinpstat.setAdapter(adapterstat);
spinpstat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
projstat = spinpstat.getSelectedItem().toString();
spinpstat.setSelection(position);
parent.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
this is the data i saved .. i set the spinner status in old student
but this how the data result in review of the data i saved.. my spinner instead of status is old student it set as new student which is the first value in my spinner list
any help will do.. thanks in advance..
Use Spinner.setSelection to set selected item from Spinner like:
spinpstat.setSelection(adapterstat.getPosition(student.status));
And get selected item from Spinner on onClick as:
student.status = spinpstat.getSelectedItem().toString();

android listview attribute

Is listview has any attribute (e.g. productId, like hidden field of html form) can be set for later use? The following is to build a listview by using an array
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, catlist);
ListView listView = (ListView)findViewById(R.id.ListView01);
listView.setAdapter(adapter);
The listview will display the text inside of the catlist, i want to pass the productid which corresponding the category text to another activity when users click on every item.
listview.setOnItemClickListener(new OnItemClickListner()) {
}
You can do something like this:
You will have two arrays catlist and catIds, catlist will have name to show in ListView and catIds will have ids at same indices, now when the user click any item, you will get its position and grab the id from catIds at that position like this:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> pr, View v, int position, long id) {
String id = catIds.get(position);
//use this id for any purpose.
}
});

How to make spinner return an id in android?

I have made a Spinner in Android:
category = (Spinner) findViewById(R.id.ev_category);
category.setOnItemSelectedListener(new MyOnItemSelectedListener());
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.category_list, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
category.setAdapter(adapter);
The resource for the spinner is:
<string name="category">Choose a category</string>
<string-array name="category_list">
<item>Food</item>
<item>Art And Performance</item>
<item>Classes</item>
<item>Others</item>
</string-array>
See, I am getting my selected Spinner text by implementing the following method. It is returning my selected item text in my Spinner.
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
ev_category=parent.getItemAtPosition(pos).toString();
}
I want the below functionality. If the first item in my spinner is selected I want to get the values as "1" and if the next item in spinner is selected i want to get value as "2".
How can I accomplish this task?
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
pos here is position isn't it?, add 1 to it, if it returns 0 for first item.
if you want string, then convert integer to string using Integer.toString()
in any method u can call getItemPosition = spinner.getSelectedItemPosition();
and you can use (switch) it will return you 0, 1, 2.

Categories

Resources