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);
Related
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.
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.
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)
I'm trying to populate the spinner, but the application crashes on spiner.setAdapter(adapter)...
final Context c=this;
ArrayList<CountryItem> countriesArray = GetCountries1();
ArrayAdapter<CountryItem> adapter = new ArrayAdapter<CountryItem>(
c,android.R.layout.simple_spinner_item, countriesArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final Spinner spiner=(Spinner)findViewById(R.id.spinner1);
spiner.setAdapter(adapter);
I can't find any errors here, and eclipse debug mode does not show anything usefull...
Here is the example from google...is it so much different than my code?
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
What error do you get?
Did you check if the countriesArray has elements or if it is empty?
Is it working when you use, e.g., a simple string array? If yes, then the problem lies with the CountryItem objects.
Other than that, the only difference I see is that you pass the context as a final variable, but I don't know if this causes the error. Try to pass just this and see if it works.
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = this.getPackageManager().queryIntentActivities( mainIntent, 0);
final String[] apps = (String[]) pkgAppsList.toArray();
Spinner appSpinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> appAdapter = new ArrayAdapter(this, apps, android.R.layout.simple_spinner_item);
appAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
appSpinner.setAdapter(cloudAdapter);
The previous code is throwing out errors for me in eclipse. I understand how to get the list of installed apps, and I understand how to populate a spinner using the createFromResource method. However I've never attempted to do so in this manner? Anyone able to direct me in the right direction?
Create a file named arrays.xml inside values folder.
Inside that give:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="array_name">
<item>value1</item>
<item>value2</item>
</string-array>
</resources>
Then inside your spinner,give:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.array_name, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
Hope this may work for you.
Check : How to get a list of installed android applications and pick one to run
Eventough it is late, you should use this constructor of ArrayAdapter instead :
ArrayAdapter<String> appAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, apps);
appAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
appSpinner.setAdapter(appAdapter);