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.
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'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've been trying to solve a problem which i been having for past week , i am trying to a access spinner inside different xml layout, all i want is to access the spinner and add array to the spinner.
This is the code i am using and it doesn't work
setContentView(R.layout.mainreg);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout_id);
View view123 = getLayoutInflater().inflate(R.layout.one2reg, mainLayout,false);
Spinner spin = (Spinner) view123.findViewById(R.id.spinnerproblem);
ArrayAdapter<String> adapter =
new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
mainLayout.addView(view123);
pass true for attach to root while inflating layout, like this.
View view123 = getLayoutInflater().inflate(R.layout.one2reg, mainLayout,true);
I have a spinner and I am trying to populate it manually in the java file, but every time I run the app I get a force close message.
Eclipse does not display any errors.
Here is my code:
String[] items = new String[] {"One", "Two", "Three"};
Spinner testlist = new Spinner(getApplicationContext());
ArrayAdapter <String> adapter =
new ArrayAdapter <String> (BlehActivity.this, android.R.layout.simple_spinner_item,items );
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
testlist.setAdapter(adapter);
ll1.addView(testlist);
Use Activitys context to create the Spinner, as you are attaching Spinner to the Activity Layout.
Spinner testlist = new Spinner(Activity_Name.this);
You should not use getApplicationContext() to initialize your spinner view. Instead you should use Activity's context. for e.g.:
Spinner testlist = new Spinner(BlehActivity.this);
i have a problem with the spinner in a tab host:
in fact i successed to make dynamically using
Spinner spinner = new Spinner(isChild() ? getParent() : this);
but i want it defined by the XML file. like this
Spinner spinner = (Spinner) this.findViewById(R.id.widget10);
I got the problem when i use (OnItemSelectedListener())
when opening the the dialogue of selection??
please i need a help,
think u.
Let us suppose, there are two class1 and class2
In class1 define a method
public static View makeSpinner(Context context) {
View v = LayoutInflater.from(context).inflate(R.layout.spinner, null);
Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item);
adapter.add("one");
adapter.add("two");
adapter.add("three");
spinner.setAdapter(adapter);
return v;
}
and in the class2 where u need to access the spinner define it's setContentView as below:-
setContentView(MainActivity.makeSpinner(getParent()));
I found a different solution.
My problem was that I had a MapView in the same activity as the spinner. Because of this I couldn't use the described technique.
And actually I didn't wanted to redesign my whole project so I just replaced my spinner in the XML-File with an FrameLayout.
Now my code looks like this:
Spinner spinner = new Spinner(isChild() ? getParent() : this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getParent(),
android.R.layout.simple_spinner_item, data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
FrameLayout frame = (FrameLayout)findViewById(R.id.frameLayoutSpinnerTarget);
frame.addView(spinner);