Spinner, string-array onclick performance - android

I am making a self service form with a set of questions during user registration.
Here is the string.xml file
<resources>
sources>
<string name="question1">What was your childhood nickname?</string>
<string name="question2">What is the name of your favorite childhood friend?</string>
<string name="question3">In what city or town did your mother and father meet?</string>
<string name="question4">What is your favorite team?</string>
<string name="question5">What was your favorite sport in high school?</string>
<string name="question6">What was your favorite food as a child?</string>
<string name="question7">What was the make and model of your first car?</string>
<string name="question8">What is you mother\'s maiden name?</string>
<string name="question9">In which city/town were you born?</string>
<string name="question10">In what town/city was your first job?</string>
<string-array name="questions">
<item>Select a question</item>
<item>#string/question1</item>
<item>#string/question2</item>
<item>#string/question3</item>
<item>#string/question4</item>
<item>#string/question5</item>
<item>#string/question6</item>
<item>#string/question7</item>
<item>#string/question8</item>
<item>#string/question9</item>
<item>#string/question10</item>
</string-array>
I have pulled these questions into 3 different spinners in the activity xml file.
activity_self_service.xml
Spinner spinner1 = (Spinner) findViewById(R.id.spinner_q1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner_q2);
Spinner spinner3 = (Spinner) findViewById(R.id.spinner_q3);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.questions, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.questions, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this, R.array.questions, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setPrompt("Select a security question");
spinner2.setPrompt("Select a security question");
spinner3.setPrompt("Select a security question");
// Apply the adapter to the spinner
spinner1.setAdapter(adapter1);
spinner2.setAdapter(adapter2);
spinner3.setAdapter(adapter3);
Say the user selected question 3, question 5 and question 6 in 3 different spinners, I want to store the indexes of these questions and the answers in the database.
So, I want something like (3,answer 1) ,(5, answer 2), (6, answer 3) in the DB.
I tried to get the indexes but couldnt find a way.
Please help.

Set "OnItemSelectedListener" on all your spinners, it will give you the index when an item is selected.

Related

One spinner showing correctly a default value and the other not

I have got two spinners that receive a default value as a variable from another view. The first one is working fine but the second one not (it shows position 0 as the default value). Here is my code:
Spinner spinner = (Spinner) findViewById(R.id.lista_origen);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getPosition(l_source));
Spinner spinner2 = (Spinner) findViewById(R.id.lista_destino);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setSelection(adapter2.getPosition(l_target));
l_target and l_source are the variables and they are correctly sent from the previous view to this one.
I would be grateful if you could find what and why is failing.
Thank you for your time.

How to programatically set entries of spinner in android?

I have made various string-arrays in the string.xml file and I have to set different arrays as entries for the spinner according to certain condition in Java. Is it possible or is database the only way to do so. Thanks in advance.
You need to use an adapter and populate with tha array in xml file.
Specify the name of your array in xml at createFromResource method (second parameter).
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.my_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);
You have to extract your data from file:
String[] testArray = getResources().getStringArray(R.array.testArray);
Then, you have to inflate in the spinner:
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, testArray );
mySpinner.setAdapter(spinnerArrayAdapter);
You can start with using ArrayAdapter, it is a simple class to populate spinner items programmatically.
String data[];
//... do your stuff to get populate this array
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, data);
mySpinner.setAdapter(spinnerArrayAdapter);
You can also modify the view of dropdown items and customize them further by overriding this class.

Spinner ArrayAdapter using Resource from R.array

final String[] sortText = new String[]{"Date Uploaded", "File Name", "Up-loader", "File Size"};
Spinner sort = (Spinner) v.findViewById(R.id.sort);
sort.setAdapter(new ArrayAdapter<>(context, R.layout.sort_row_layout, R.id.sortTV, sortText));
The issue I'm having is that its unable to create constructor when I try to setup the adapter when pulling the array from resources so I can implement internationalization and have the text shown in different languages as so..
<resources>
<array name="sort">
<item>Date Uploaded</item>
<item>File Name</item>
<item>Up-loader</item>
<item>File Size</item>
</array>
</resources>
sort.setAdapter(new ArrayAdapter<>(context, R.layout.sort_row_layout, R.id.sortTV, R.array.sort));
What could the issue be here?
ArrayAdapter doesn't have a constructor you can pass a reference to an array resource to like that. See the docs
You want to use createFromResource() and setDropDownViewResource() like this:
ArrayAdapter<CharSequence> sortAdapter = ArrayAdapter.createFromResource(context, R.array.sort, R.layout.sortTV);
sortAdapter.setDropDownViewResource(R.layout.sort_row_layout);
sort.setAdapter(sortAdapter);
EDIT
For a standard, system default styled Spinner just use the built in layouts:
ArrayAdapter<CharSequence> sortAdapter = ArrayAdapter.createFromResource(context, R.array.sort, android.R.layout.simple_spinner_item);
sortAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sort.setAdapter(sortAdapter);

ArrayAdapter.createFromResource issue

I am trying to make "Select one" on Spinner. I saw all answer regarding this subject but I am still having some issues. Usual way of making custom spinner is:
ArrayAdapter<CharSequence> dataAdapter1 = ArrayAdapter.createFromResource(this, R.array.entries,
android.R.layout.simple_spinner_item);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter1);
spinner1.setAdapter(
new NothingSelectedSpinnerAdapter(
dataAdapter1,
R.layout.contact_spinner_row_nothing_selected,
this));
In this code I have to define R.array.entries in Strings.xml, but my app is populating spinner from MySQL and I am having a list grad[i]=json.getString("Grad");. How can I create this ArrayAdapter.createFromResource with that list instead of Entries that are defined in Strings.xml? Tnx
Query the data, put it in a List or Array and use this constructor of Array Adapter
ArrayAdapter<CharSequence> dataAdapter1 = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, yourArrayOrList);
More here: http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context, int, java.util.List)

Data not loading into my Spinner

I'm trying to load an array of data into a Spinner component and it's throwing a NullPointerException. The code I'm using is below and it all seems okay.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<?> spin_adapter = ArrayAdapter.createFromResource(
this, R.array.letters_array, android.R.layout.simple_spinner_item);
spin_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spin_adapter);
The string-array looks like this
<string-array name="letters_array">
<item>A</item>
<item>B</item>
<item>C</item>
<item>D</item>
<item>E</item>
<item>F</item>
<item>G</item>
<item>H</item>
<item>I</item>
<item>J</item>
<item>K</item>
<item>L</item>
<item>M</item>
<item>N</item>
<item>O</item>
<item>P</item>
<item>Q</item>
<item>R</item>
<item>S</item>
<item>T</item>
<item>U</item>
<item>V</item>
<item>W</item>
<item>X</item>
<item>Y</item>
<item>Z</item>
</string-array>
Is there a limit to the number of items in a Spinner or am I doing something else wrong?
Try this:
ArrayAdapter<CharSequence> spin_adapter = ...
In which xml-File is your Array stored and where is it located?

Categories

Resources