I have a list of strings that I'm trying to pass to a spinner
my code is this :
Spinner spinner = (Spinner) findViewById(R.id.spinner_details);
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, spinnerTitles);
where "spinnerTitles" is my List of strings
the problem is I'm getting an error of "the constructor is undefined for..."
how to solve this?
try this:
ArrayList items=new ArrayList();
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(Activity.this,android.R.layout.simple_spinner_item, items);
check the constructor Context this maybe it refers to your Fragment. change it to :
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(YourActivity.this,android.R.layout.simple_spinner_item, spinnerTitles);
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'm using eneter framework to process communication in my android application;
the problem is when I'm trying to populate a spinner, setting the adapter to the spinner cause an undefined exception
Here the code
public void populateSpinner(TypedResponseReceivedEventArgs<String> arg1){
List<String> list = new ArrayList<String>();
String listf = arg1.getResponseMessage();
//sendToDebug(listf);
StringTokenizer tokenizer = new StringTokenizer(listf,",");
while(tokenizer.hasMoreElements()){
list.add((String)tokenizer.nextElement());
}
//EditText text = (EditText)findViewById(R.id.number2EditText);
//text.setText(list.size());
//text.setText(listf);
Spinner forfait = (Spinner)findViewById(R.id.forfaitsSpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
forfait.setAdapter(adapter);
}
you are passing this in the following piece of code,
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,list);
Not sure in which block this code lies or which class, but ensure that this refers to ActivityName.class or the context
It is most likely because you are using an ArrayAdapter rather than a SpinnerAdapter. ArrayAdapter is an indirect implementer of the SpinnerAdapter interface rather than one which declares that it implements the interface. Check the undefined exception. It is likely telling you that setAdapter(ArrayAdapter) is not defined for Spinner.
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 need to create a spinner from a string. Until now I use an ArrayAdapter, but it's not working. This is my code
ArrayList<String> aus=new ArrayList<String>();
if(results.length!=0){
for(int i=0;i<results.length;i++)
aus.add(results[i].get_nome());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, aus);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
locale.setAdapter(adapter);
}
}
locale is a Spinner type
results it's a my class and get_nome return a string
I have this error: "The costructor of ArrayAdapter(new Handler(){},int, ArrayList is undefined"
It seems that you're creating the adapter in an inner class.. You should do
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyActivityClass.this,android.R.layout.simple_spinner_item, aus);