Android ArrayAdapter<String> Error - android

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

Related

ListView is not showing up on app

A quick problem, my Listview is not showing up in my app. I just deleted and redownloaded android studios, so I'm on the newest version.
public class ToDoList extends AppCompatActivity {
ArrayList<String> notes = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do_list);
ListView listView = (ListView) findViewById(R.id.listView);
notes.add("Here's an example");
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);
listView.setAdapter(arrayAdapter);
}
}
You have to pass your arrayList to arrayAdapter, then set your arrayAdapter in your listview.
Use this
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,notes);
listView.setAdapter(arrayAdapter);
You didn't set adapter to listview
add this line
listView.setAdapter(arrayAdapter);
You should have to use this, pass notes to your adatpter
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, notes);
listview.setAdapter(arrayAdapter);
1.Define a new Adapter as follow
First parameter - Context
Second parameter - Layout for the row
Third parameter - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
2. Assign adapter to ListView
listView.setAdapter(adapter);
Use this instead
ArrayApater adapter=new ArrayAdapter(getApplicationContext,android.R.layout.simple_list_item_1,notes);
listView.setAdapter(adapter);
You must have to pass your arrayList to arrayAdapter, then after set your arrayAdapter in listview.
Use Like This
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notes );
listView.setAdapter(arrayAdapter);

Passing empty Array to ArrayAdapter.createResource

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.

create spinner from list of strings

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

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.

How to change multi Spinner's value

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.

Categories

Resources