not accessing the values of spinner | android - android

Spinner spin = (Spinner) findViewById(id);
String grades = spin.getSelectedItem().toString();
I have two activities, in both there are two spinners to which i am assigning entries and Id's programmatically. But later while getting the selected value in ONE activity my app crash. in other activity the code is running fine.
here is the code.
List<Integer> creditSpinnerArray = new ArrayList<Integer>();
for (int i = 1; i < 6; i++)
creditSpinnerArray.add(i);
Spinner creditSpinner = new Spinner(this);
ArrayAdapter<Integer> creditAdapter = new ArrayAdapter<Integer>(this, R.layout.support_simple_spinner_dropdown_item, creditSpinnerArray);
creditAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
creditSpinner.setAdapter(creditAdapter);
creditSpinner.setId(Id][0]);

you question is not quite clear but as you are stating same code is working in another activity but not here than may be you are running in to issue with the type.
try this code to get the String like this
((Integer)spinner.getSelectedItem()).toString();
this is casting your selected item as the the type of the Object you have assigned to ArrayAdapter<Integer>
So in another words you should cast your spinner object depending on the Type of Object you have provided to the ArrayAdapter<T>
for Example
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item);
((String)spinner.getSelectedItem()).toString();
or
ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(getApplicationContext(), android.R.layout.simple_spinner_item);
((Object)spinner.getSelectedItem()).toString();
the toString() just called to let you know what you can do with respective Object which you will get by no means I wrote a standard code
Update
I can't see if you are adding your Spinner Object to the Activity UI, what am I missing here

Related

Create Spinner and Adapter without any XML

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.

Android Spinner Adapter Setting 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.

ArrayAdapter + Spinner

I'm working with spinners plus the arrayadapters that come with them but I just can't get my head around what setDropDownViewResource() does. How is it related to the layout you can pass when contructing the arrayadapter (i.e. Context context, int textViewResourceId, List objects))?
ArrayAdapter<String> adp1;
ArrayList<String> arrayList1 = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.sp1_array)));
Spinner sp1 = (Spinner) view.findViewById(R.id.spinner1);
adp1 = new ArrayAdapter<String> (mcntxt, R.layout.my_simple_list_item, arrayList1);
adp1.setDropDownViewResource(R.layout.my_simple_dropdown_item_1line);
sp1.setAdapter(adp1);
The ID you are using on the constructor will be used for the Spinner main window, or prompt, as the documentation states.
When you call setDropDownViewResource(), you are establishing the layout to be used for the itens on the spinners dropdown list. Note that this applies for spinners used as dropdown.

populating a spinner manually in.java file

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);

Android application crashes when populating spinner

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.

Categories

Resources