Create String array (in xml) from java array? (Android) - android

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(RoutesActivity.this, directionArray, R.id.route_direction_spinner);
So, I'm trying to make a spinner, and I have a java String array (directionArray). My problem is Eclipse returns this error when hovering over "createFromResource":
"The method createFromResource(Context, int, int) in the type ArrayAdapter is not applicable for the arguments (RoutesActivity, String[], int)"
And gives me this Quick Fix:
"Change type of 'directionArray' to 'int'."
Basically, I have an array in Java that I need to use in an ArrayAdapter, but apparently it won't let me. Is there a way to create an xml resource from within java, and/or a way to bypass the above error?

ArrayAdapter<String> adapter = new ArrayAdapter<String>(RoutesActivity.this, android.R.layout.simple_spinner_item, directionArray);
Use this then your standard:
Spinner spinner1 = (Spinner) findViewById(R.id.route_direction_spinner);
spinner1.setAdapter(adapter);

No, XML resources are parsed into binary format at compile time. You cannot create new XML resources at runtime.

Related

Warning unchecked call to 'getposition(T)'

I'm retrieving some default from a database on application start and I'm using it to set the selected value of a spinner item in my activity.
if( key.equals("default Altitude Units")) {
Spinner s = ((Spinner)findViewById(R.id.spinAltitudeUnits));
ArrayAdapter a = (ArrayAdapter) s.getAdapter();
s.setSelection( a.getPosition(result.getString(2)));
}
The code works fine however the problem is that i'm getting a warning in Android Studio that says Warning unchecked call to 'getposition(T)' as a member of raw type 'android.widget.ArrayAdapter' on the a.getPosition call.
I'm happy that it's working but being new to android and Java I want to understand and eliminate as many warnings as possible from my code so any help in getting rid of this warning would be most welcome.
You are getting the position from some string. sting 2 in this case. A ArrayAdapter can contain many types. Integers for example. In your case it is not clear what your ArrayAdapter is containing.
Use this instead:
ArrayAdapter<String> a = (ArrayAdapter<String>) s.getAdapter();
I believe you need to paramaterize your ArrayAdapter. Do so with the following code:
ArrayAdapter<String> a = (ArrayAdapter<String>) s.getAdapter();
That is, if your ArrayAdapter contains strings.
EDIT: woops, got beat by a few seconds.

What does <variable> means? [duplicate]

This question already has an answer here:
What does Set<element> mean?
(1 answer)
Closed 9 years ago.
I was looking for some codes to make a Spinner then I noticed that they use an ArrayAdapter like this:
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(context,android.R.layout.simple_spinner_item );
My question is, what does the <> do? I investigate a bit and its not a cast (I think) adn i havent found something yet.
In short, ArrayAdapter<CharSequence> adapter; means ArrayAdapter is of the type CharSequence. You can have adapter of any kind, say, for example, ArrayAdapter<String>, here ArrayAdapter is of type String.
You can define your custom ArrayAdapter too, by creating a class that extends ArrayAdapter and override its methods as you want.
Assuming you know c and c++ we used arrays of data type char to store a stirng
Same way when you want to store array or string you create something like this
String[] myArray = {"abc", "def", "ghi"} ;
Overloading in java helps is using same function defined in a class with different parameters to do different set of instructions which are changed as per the arguments as received via calls.
Same way when ArrayAdapter is called the dataType defines the type of array Adapter
As you might want to think it as
String ArrayAdapter variableName = new String ArrayAdapter () ;
But as u can notice we are trying to define two things i.e. The variable is a string but the variable is also an ArrayAdapter, this creates mess for java jdk developers to change the rules of compiling java files.
So they tried to make something like
ArrayAdapter<String> variableName = new ArrayAdapter<String> () ;
Hope i helped..

Is there a simple way to populate a Spinner with an ArrayList?

I have a bunch of ArrayLists filled with data. All I want to do is populate the spinners using the arraylists. In other SO questions people create their own Adapters and store the ArrayList as a field, but I don't need to do that.
I just want to populate the spinner. I don't think theres something this easy, but is there anything resembling
mySpinner.populate( myList );
Simply use the built-in ArrayAdapter, you don't have to write a custom Adapter.
List<String> list = new ArrayList<String>();
// Fill list with strings
...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
You don't specify what data types you are using, but it doesn't matter: you can easily change the ArrayAdapter's subtype to <Integer>, <Double>, or even a custom class <Kittens> (as long as you define the custom classes toString() method.)
ArrayAdapters have an add() method to store things to its internal list. No need to create your own. Try something like this:
for(Object ob : yourArrayList)
<arrayAdapter>.add(ob);
<arrayAdapter>.notifyDataSetChanged();
I would change Object to be whatever collection your List is using.

How to set ArrayAdapter properly in Android

I'm trying to make a ListView in Android with a dynamic field. First, I tried to set it up using static field which is shown below (String[] chars)
String[] chars = {"TEST"};
setListAdapter(new ArrayAdapter<String>(this,R.layout.resultlist,chars));
it gives me error The constructor ArrayAdapter<String>(ViewTranslationsList.GrabURL, int, int) is undefined. Where's the problem?
I want to add items to the chars array before I set up ArrayAdapter in for cycle.
Thanks
It looks like you're doing this inside an inner class (since the error is mentioning ViewTranslationsList.GrabURL. If that is truly the case, try doing this:
setListAdapter(new ArrayAdapter<String>(ViewTranslationsList.this,R.layout.resultlist,chars));
It looks like you are not inside your Activities context when you are calling your ArrayAdapter constructor. Try to change it like this:
setListAdapter(new ArrayAdapter<String>(yourActivityName.this,R.layout.resultlist,chars));
Though that may not be your only problem because based on the error it seems to think that chars is an int for some reason.
here this is ViewTranslationsList.GrabURL
ListView l = (ListView)findViewById(R.id.some)
l.setListAdapter(new ArrayAdapter<String>(SomeActivity.this,R.layout.resultlist,chars));
you have to pass Context, ViewTranslationsList.GrabURL is not Context
im not about your problem, but if you dont now about standard layout in android.
l.setListAdapter(new ArrayAdapter<String>(SomeActivity.this,android.R.layout.simple_list_item_1,chars));
or
l.setListAdapter(new ArrayAdapter<String>(SomeActivity.this,android.R.layout.simple_list_activated,chars));
or
l.setListAdapter(new ArrayAdapter<String> (SomeActivity.this,android.R.layout.simple_list_item_multiple_choice,chars));
l.setOnChoise(LISTVIEW.SET_MULTICHOISE_MODE);
read about standard layout. http://developer.android.com/reference/android/R.layout.html

ArrayAdapter type cast from android docs example

I am trying to create a list of strings from my arrays.xml file, as found in the android docs too. This is what they use:
ArrayAdapter adapter = ArrayAdapter.createFromResource(context, R.array.colors, android.R.layout.simple_spinner_item);
eclipse is warning that the ArrayAdapter instance is a raw type. Should it really be:
ArrayAdapter<CharSequence> adapter = ...;
?
Thanks
HiIt's always better to leave type checking to java compiler, so it would be better to declare your adapter as
ArrayAdapter<CharSequence> It probably doesn't change anything in your code, especially when you are sure that you are not going to put anything else but strings to your adapter. But it's definately a good practice to use generics to avoid runtime exceptions.Regards!

Categories

Resources