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)
Related
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 have am initializing an array adapter like so:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), -1, android.R.layout.simple_spinner_item);
But unfortunately, I keeping receiving the error:
java.lang.NullPointerException
at android.content.res.AssetManager.getResourceTextArray
I think this is because I am passing -1 as the lookup for my Array. The problem is I am creating the array programmatically AFTER this so I actually don't have a Array defined in R.Arrays.
Does anybody know how I can initialize the ArrayAdapter without having a defined Array XML file?
List<String> listLoadToSpinner = new ArrayList<String>();
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(
mContext,
android.R.layout.simple_spinner_dropdown_item,
listLoadToSpinner);
P.S : It's better to initialize the Adapter once you have the Array and set that Adapter to Spinner.
I have reviewed several examples of how to set up a spinner in my program and they all use the same format that I am using, yet eclipse says that it is a syntax error, specifically: "Syntax error on token "setDropDownViewResource", identifier expected after this token. Additionally it says that the statement that sets the spinner adapter expects a variable declaratorid after the token "adapter"
here is the code block in question:
Spinner tipPercent = (Spinner) findViewById(R.id.tip_spinner);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this,
R.array.tip_percentage, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
What am I missing?
Initialize adapter ArrayAdapter instance as:
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this,
R.array.tip_percentage, android.R.layout.simple_spinner_item);
I use two Spinner and get data from asyncTask when I get first spinner and want get the 2th spinner but when setadapter .. two spanner's data are the same...
This is i setadapter first time.
List<String> none=new ArrayList<String>();
none.add("none");
ArrayAdapter<String> adapterchoseTime =
new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, none);
adapterchoseTime.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapterchoseProm =
new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, none);
adapterchoseProm.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
choseTime.setAdapter(adapterchoseTime);
choseProm.setAdapter(adapterchoseProm);
I get Spinner 1's data
ArrayAdapter<String> adapterchoseTime=(ArrayAdapter<String>)choseTime.getAdapter();
adapterchoseTime.clear();
adapterchoseTime.addAll(time);
choseTime.refreshDrawableState();
and i get spinner2's data
ArrayAdapter<String> adapterchoseTime = (ArrayAdapter<String>)choseTime.getAdapter();
adapterchoseTime.clear();
adapterchoseTime.addAll(time);
ArrayAdapter<String> adapterchoseProm = (ArrayAdapter<String>)choseProm.getAdapter();
adapterchoseProm.clear();
adapterchoseProm.addAll(prom);
choseTime.setAdapter(adapterchoseTime);
choseProm.setAdapter(adapterchoseProm);
And then I get two same spinner...
It looks like you're referencing the same spinner from your setDropDownViewResource.
adapterchoseTime.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
One of these needs to reference the second spinner.
i got 2 classes. A class with a spinner in it and a preference class. The spinner is set up with a array from strings.xml` s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
this, R.array.height_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter1);`
The second class is the preference class with a ListPreference(think it is a a ListPreference)
/kind of a spinner. If u click it and select any of the items from the ListPreference i want the spinner to use another array. Change R.array.height_array to R.array.height2_array or something like that. Possible ?
yes, something like this should work. If you need more than 2 choices, use more ifs
if (prefs == choiceOne){
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
this, R.array.height_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter1);
}else{
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
this, R.array.height2_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter1);
}