What does <variable> means? [duplicate] - android

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..

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.

declare Array adapter in the non-activity class to use in the fragment

So here is my problem. I'm using a dialog fragment to display some info. In that fragment, i have a spinner view that is filled up by the Array list. Normally I create that adapter in my fragment like this:
ArrayAdapter<String> teamsAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, Variables.teamNames);
teamsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Variables.spinner.setAdapter(teamsAdapter);
Variables is my user-defined class where I keep all variables.
Now here is my question. I have around 10 DialogFragments and almost all of them have spinner. I would like to define that Adapter in my Variables class and whenever i need it, i would just call it from my variables class. Unfortunately to do that, i need getActivity() in my adapter constructor. I cannot go around it.
BTW, is it a good idea or I should use interface?
It would be awesome if somebody could actually tell me why would I use java interface in android?
Your fragment would still need to call some Variables method to retrieve the spinner and to add it to your layout, right? So when you do that call, you can pass the Activity as Context, something like this:
ArrayAdapter<String> teamsAdapter = Variables.getAdapterForTeams(getActivity());
The question about the Interface isn't an Android but a Java question. If it makes sense to use an Interface in Java it will make sense to use it in Android. There's plenty of literature and online resources about Java that will explain the why and when Interfaces should be used. I'm sure if you search online you'll find plenty of resources about it.

Create String array (in xml) from java array? (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.

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

What is the alternative for android:entries in code?

How to I send an String array to a listView from the code and have the emulator display it?
That is a very general question... but the basic idea would be to create one of the usual Adapters, ArrayAdapter possibly in this case, initialize it with the String array and then call the ListView's setAdapter() method with that ArrayAdapter.

Categories

Resources