How to set ArrayAdapter properly in Android - 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

Related

Android Custom ListView/Adapter

I am wanting to change my listView from the normal ArrayAdapter (simple_list_item_1) to something more like this:
Name..... Score(center right).
Date (under name)
So There's 3 Views...
name
date
score
I've looked up how to make custom adapters and layouts but they are all very confusing. I just want a simple fix that I can add to an existing project.
Here's my code for my list right now:
//update listView
listAdapter = new ArrayAdapter<String>(GradesActivity.this, android.R.layout.simple_list_item_1, names);
mListView.setAdapter(listAdapter);
Help is appreciated!
I just had the SAME problem last week. My solution was a tutorial on the internet: http://androidtuts4u.blogspot.com.br/2013/02/android-list-view-using-custom-adapter.html
Just copying and pasting the code onto a temporary project (it will take less than 15 minutes) will help you understand how simple it is and also make it very clear so you can implement it into your original project. I hope it helps!
Peace!
ArrayAdapter can't auto binding three view
You can try SimpleAdapter or extend BaseAdapter implements it youself
This is Google's training
https://developer.android.com/training/material/lists-cards.html
https://developer.android.com/training/wearables/ui/lists.html
And here is a sample of SimpleAdapter http://www.java2s.com/Code/Android/UI/UsingSimpleAdaptertofilldatatoListView.htm
There are many fixes you need to do
1. Create a class that extends BaseAdapter.
Override all the methods create a constructor of that class that will initialize listitems
You are just passing names
listView listAdapter = new ArrayAdapter(GradesActivity.this, android.R.layout.simple_list_item_1, names);
Instead of just names create a class that contains three elements you need I.e name date and score and make there get and set methods
And the object of the class will be saved in a list.
4. In getview method you have to set these variables

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

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.

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