Warning unchecked call to 'getposition(T)' - android

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.

Related

Update String Array with Subsequent Update of ListView in Android

I am trying to update a String in a ListView, and have the update be reflected in the ListView. Here are the basics of what's going on:
testStrings = {"String", "String", String"};
ListView l = (ListView) findViewById( R.id.listView1 );
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, testStrings);
l.setAdapter(adapter);
Later, testStrings is update like so:
testStrings[1] = "new string";
Note, just the text is changing, not the cardinality of the String array.
On a Samsung Galaxy Nexus with Android 4.3, the text in the ListView is updated as expected, with no additional calls to the adapter.
On a Google Nexus 7 running Android 4.4.2, nothing happens. On the Nexus 7, I've tried calling notifyDataSetChanged(); on the adapter, and that doesn't do anything either.
I've seen a number of questions about ListViews not updating, but those examples seem to be doing something more complicated than what I'm trying to do. Why is one device working fine, but not the other? How do I get the ListView on the Nexus 7 updating correctly?
I think you need to be more direct about modifying the adapter. When I walk through the Android code that constructs ArrayAdapter using a String array, it first makes an ArrayList from your String[], and then makes the ArrayAdapter from the ArrayList. While the source does seem to point to the ArrayList and then the array, it does seem a risky proposition to expect ArrayAdapter to be aware that you have modified the String[] deep inside it. It is interesting that calling notifyDataSetChanged works on one device and not another. But I think a much better approach is to remove and then add the strings from the adapter. This way the adapter will know that something is happening to it.
adapter.remove(adapter.get(1));
adapter.insert("new string", 1);

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

How can i check boxes from simple_list_item_multiple_choice via retrieving data from a sharedPreferences?

i'm pretty new to android-development and im trying to build a ListView with 8 items, that each have checkboxes. is set them up already, but i dont know how i can set one/several box beeing checked via retrieving key.value-pairs from a SharedPreferences.
i think im pretty close, but there is one or two lines of code missing :(
this my code in the onCreate-method:
lv = (ListView) findViewById(R.id.lvMain);
getPreferences(MODE_PRIVATE).edit().putString("Spiel4", "yes").commit();
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, lvItems));
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
for (int i=1;i<9;i++) {
tmpString = null;
tmpString = getPreferences(MODE_PRIVATE).getString("Spiel"+i,"");
if (tmpString.equals("yes")) {
// -----> MISSING LINE HERE <-----
Log.i(tag,"Spiel"+i+"-value is YES!");
}
}
p.s.: my first post, formatting tricked me :(
you can use
lv.setItemChecked(i, true);
add this line on missing line :)
Have you considered the PrefenceActivity or PreferenceFragement?
Take a look at:
http://developer.android.com/guide/topics/ui/settings.html
Another option is write your own Adapter, that is not hard too. Look that:
http://www.vogella.com/articles/AndroidListView/article.html#adapterown_custom

How to remove the filter on an ArrayAdapter used in an AutoCompleteTextView?

As the title, how can I remove the filtering on an ArrayAdapter used by an AutoCompleteTextView to get the original list back?
A little more background:
It all started from the sad fact that the "position" value passed in to onItemClick() is useless. The "position" refers to the position AFTER the array has been filtered, but I need to know its REAL position. So, what I'm trying to do is when I've got the text of the selected item (by using getItemAtPosition(position)), I compare it one-by-one with the original string array that backs the ArrayAdapter. However, I found that when onItemClick() is called, the adapter is already filtered, I no longer have access to the original array. So I thought if I can remove the filter, maybe I can get back the original array and look for the selected item in it.
ArrayAdapter<String> mAdapter;
public void onCreate() {
// Create an adapter and remembere it as a class member.
mAdapter = new ArrayAdapter<String>(this, layoutId);
// Add 100 strings to it and attach it to an AutoCompleteTextView
for (int i = 0; i < 100; i++)
mAdapter.add("random text");
((AutoCompleteTextView)findViewById(id)).setAdapter(mAdapter);
}
#Override
public void onItemClick(AdapterView<?> actv, View view, int position, long id) {
if (actv.getAdapter().equals(mAdapter))
Log.d("The adapter contained in actv is the same one I created earlier.");
// And, I can get the text of the item the user selected
String selected = (String)actv.getItemAtPosition(position);
// However, although the adapter passed in is still the same one, but the
// number of items in it is only 1! Because the array has been filtered.
int numItems = actv.getAdapter.getCount();
// So, I'm thinking if I can somehow remove the filtering here, then I can
// get back those 100 items, and do a search like following:
for (int i = 0; i < actv.getAdapter.getCount(); i++)
if (selected == actv.getAdapter.getItem(i))
break; // Eureka!!!
}
To tackle the problem of obtaining the REAL position of the selected item:
Is there a way to utilize the "id" value? Like, assign each item an id, then hopefully onItemClick() would pass back the correct id.
Like I said above, remove the filter (is it possible), get back the original 100 items, and perform a one-by-one search.
This is the last resort, I know it'll work, but I don't want to do it: Once I get the text of the selected text, I go back to the source of the data (from a database), query those 100 items out, and perform the search.
Another lame last resort: To avoid the overhead on accessing the database again as in #3, when in onCreate(), while creating the ArrayAdapter, I use an ArrayList of my own to remember all those 100 strings.
Am I doing it all wrong? What's the "right" way of obtaining the real position of the selected item from an AutoCompleteTextView?
Thank you very much!
(I read somewhere, some buy that seemed to be from Google Android team, said that one should use getFirstVisiblePosition() to resolve the position. But I can't figure out how.)
I don't know if you're still interested, but I found this answering a similar question: Problem with AutoCompleteTextView and Spinner using the same Adapter
Copying the method in the AutoCompleteTextView source code:
Filter filter = mAdapter.getFilter();
filter = null;
See my response in the above question for the grepcode link.
This is actually pretty simple to solve.. Instead of adding each element to the adapter as you get it (I'm assuming your random text part is just for example purposes), instead use the following:
First build your array into a variable, call it myArray..
then initialize your adapter like this:
mAdapter = new ArrayAdapter<String>(this, layoutId, myArray);
Now make sure that myArray is a class variable so you can reference it from anywhere else in the class.. Of course if you need access to this from another class you'd want to make a getter for it... Then you can easily iterate over the array to see if the value selected is in the array.. You'll have the whole set of data there instead of trying to get it from the adapter.
Here is a good example on using a validator for a similar looking use case:
Android, Autocomplettextview, force text to be from the entry list
In my case, I have address that can be set either by autocomplete or clicking on the map. If user click on the map, editText text should be set to address selected from the map, and in that case filtering should be disable temporarily.
I code like this:
public void OnAddressFound(String address) {
// Temporary disable autocomplete
editTextSearch.setAdapter(null);
editTextSearch.setText(address);
// Enable autocomplete again
setAutoCompleteAdapter();
}
where setAutoCompleteAdapter() is called during onCreate, and again in temporary disable/enable filter:
void setAutoCompleteAdapter() {
PlacesAutoCompleteAdapter adapter = new PlacesAutoCompleteAdapter(this, R.layout.item_autocomplete_map_search, autoCompleteList);
editTextSearch.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
Hope its help you also.
I found the solution, kinnda tricky but its works
after i looking inside of the source, i found that th treshold variable used as filtering validation, here we just need to set the treshold to maximum int so filtering never perform.
threshold = Integer.MAX_VALUE
or
setThreshold(Integer.MAX_VALUE)

Passing information between views

I am new to android programming, but I am trying to learn. I have written some code that takes in some parameters through a "normal" view with checkboxes and textviews. Then I use this information to generate a lot of numbers that I want to display in a listview. I have managed to create a listview when I press a run button, but how do I pass the information from the main view to the listview. Is it best to pass the information one number at the time or a large array with all the numbers. The list of numbers can be really large.
What you probably what to do is create an adapter with the numbers as the data source. If the numbers are in an array you can create a new ArrayAdapter and set the ListView adapter as that adapter:
ArrayAdapter adapter = new ArrayAdapter<Double>(getApplicationContext(), R.id.id_of_textbox, arrayOfDoubles);
listView.setAdapter(adapter);
In this code I've assumed the numbers are doubles, however ArrayAdapter is a generic class so it can be any object contained in the array. The array can also be presented as a List (like an ArrayList).
Hope that helps you out. Here are some bit of documentation to read and some good video sessions to watch:
ArrayAdapter
ListView.setAdapter()
The World of ListView Google I/O 2010 Session
How big is the array can get?
Most likely that displaying the list as another activity and passing the data as intent's extra will be the solution.

Categories

Resources