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.
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.
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
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);
I have a listview that shows the contents of an arraylist. I'm using a simple adaptor to make this possible like so.
public static ArrayList<String> homeScreenContacts = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.home_screen_contacts_view, NewContact.homeScreenContacts);
The second line is giving me a null pointer exception. I thought about it and I decided it was because the arrayList is empty. So I added the following line between the arraylist declaration and the arrayadaptor declaration...
NewContact.homeScreenContacts.add("A Contact");
This solved the problem and my code worked fine but Now the list view shows "A Contact" and I dont want it to. Is there anyway to get rid of the null pointer exception problem but still have the arraylist empty? Because I want to populate it with user made contacts, not hard-coded, random strings. Thank you.
EDIT: Sorry, The arraylist is located in another class called NewContact, also, I am very beginner Android Programmer I just started.
Simple solution just don't initialize the ListView if there is no element in the ArrayList or the ArrayList is null.
if(NewContact.homeScreenContacts != null && NewContact.homeScreenContacts.size() > 0){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.home_screen_contacts_view, NewContact.homeScreenContacts);
listView.setAdapter(adapter);
}
Also you need to remember that if you you haven't initialize Adapter then dont initialize the ListView and before any operation on list view you should check is it null or not.
As you have said that you want to populate when user add some contact in the application then on add event only you need to populate or update the ListAdapter.
Hope this solution will resolve your problem.
Try this code
public class YourActivity extends Activity
{
private ListView lv;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.your_layout);
lv = (ListView) findViewById(R.id.your_list_view_id);
// Instanciating an array list (you don't need to do this, you already have yours)
ArrayList<String> your_array_list = new ArrayList<String>();
your_array_list.add("foo");
your_array_list.add("bar");
// This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, your_array_list);
lv.setAdapter(arrayAdapter);
}
}
Works fine for me:
if(arrayList.isEmpty())
{
listView.setAdapter(null);
}
else
{
listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(listAdapter);
}
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);