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.
Related
I can create a very simple Spinner with code like this
final List<String> categories = new ArrayList<>();
categories.add("First");
categories.add("Second");
categories.add("Third");
final Spinner spinner = new Spinner(context);
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(context, R.layout.spinner, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
however, this requires R.layout.spinner in my app. How can I avoid this XML with own, pure Java code, e.g. by implementing an own ArrayAdapter-like class?
Extend you custom adapter from ArrayAdapter and try to override getView() with your own code. You should be able to create the adapter with a null resource id. You will have to build you own views since you will no longer be using a resource.
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 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 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.
In my main activity in the onCreate method I am trying to add a string array from the R.array file to an array adapter. When i go to launch the app the drop down box is empty. If I manually create a String array via code the drop down is fully populated. Here is the code below.
ArrayAdapter<String> dateAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, R.array.reminderDays);
dateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dateRangeSpinner.setAdapter(dateAdapter);
dateRangeSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
Try,
ArrayAdapter<CharSequence> dateAdapter =ArrayAdapter.createFromResource(this, android.R.layout.simple_spinner_item, R.array.reminderDays);
This is working fine for me.