I've had a look at the documentation, but I'm not sure what would be the best way to approach this.
This is what I'm doing:
mActionBar = getActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.platform_list,
android.R.layout.simple_spinner_dropdown_item);
What I'm trying to do is give the dropdown list a default value - "Please Select a Type", when the application loads up and nothing is selected yet. This is because the selection will trigger a change in the listview fragment displayed below, and without a selection (at startup) i would like to display a simple placeholder image or something.
Any ideas? Thanks a lot in advance!
Are you sure that what you want is a SpinnerAdapter? I spent a while today trying to figure out the same problem until I realised that what I actually wanted was just a SubMenu.
You can set the selection of the Spinner to a particular position which can display this text
public void setSelection (int position)
Since: API Level 1
Sets the currently selected item. To support accessibility subclasses that
override this method must invoke the overriden super method first.
Parameters
position Index (starting at 0) of the data item to be selected.
http://developer.android.com/reference/android/widget/AbsSpinner.html#setSelection(int)
Its not clear you problem to me.But i guess you want to view a default view and after select anything you want to change the below list view.If i am correct you can do this like this.
create onItemSelectedListener
class Ranger implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
item= parent.getItemAtPosition(pos).toString();
//if the selected item is default do default things
//or else do the list view update
}
}
Related
When I'm using parent.getChildAt(position).setBackgroundColor(Color.GRAY); in my public void onItemClick(AdapterView<?> parent, View view, int position, long id) it colorizes, but it works strange.
When I click first or second item it colorizes it... and every item away ~five records. Sometimes I've got NullPointerException. Completely weird, because position is unique and it should recieve me appropriate View, but it doesn't.
I saw solution with overriding getView method, but I'm using this adapter in different places. I just want to color clicked item. How to get reference to selected view?
EDIT:
In my adapter class I created:
public static int selectedItem = -1;
I added this to my overrided getView method:
if(selectedItem == position){
parent.getChildAt(position).setBackgroundColor(Color.GRAY);
}
In my activity I added that:
myAdapter.selectedItem = position;
myAdapter.notifyDataSetChanged();
And It doesn't work. Where I do a mistake?
It's not a bug - it's the way ListView re-uses the views to save resources.
So to avoid this behavior you should on every getView() set all used attributes for all your views.
Updated - to be quite clearIn your particular case it means that you should set color like this:
1) In onItemClick() - in your actitivity - you should remember given position as selected:
myAdapter.selectedItem = position
2) In getView() - in your adapter:
if(selectedItem == position)
parent.getChildAt(position).setBackgroundColor(Color.GRAY);
else
parent.getChildAt(position).setBackgroundColor(0);//or whatever defauld color
Update 2
If you want to select many items you should use some structure (like HashSet) to hold all the selected items:
1) In your activity class add member:
public static HashSet<Integer> mSelectedItems = new HashSet<Integer>();
2) In onItemClick() use following to flip selected state:
if(mSelectedItems.contains(position))
mSelectedItems.remove(position);
else
mSelectedItems.add(position);
3) In getView():
if(MainActivity.mSelectedItems.contains(position))
parent.getChildAt(position).setBackgroundColor(Color.GRAY);
else
parent.getChildAt(position).setBackgroundColor(0);//or whatever defauld color
At first read this article;
Then use ViewHolder pattern;
And try to setBackgroundColor() in onItemClick() like this:
view.setBackgroundColor(0);//or whatever defauld color
I have a list view with multiple items, where i need to select and deselect the list items, and also delete the selected items.
So i have looked into the example in the below link but its for android:minSdkVersion="11"
but i am working on minSdkVersion="10".
Link : http://www.androidbegin.com/tutorial/android-delete-multiple-selected-items-listview-tutorial/
And yes we can do with checked text view, check box and radio button, but the requirement is like that i cannot use that.
Is there any other way that we can acheive this?
Make custom list adapter and get the click of that each view and maintain flag in adapter. If flag is true that means item selected otherwise item deselected, according to that you can change item view like disable that particular item or show some check box.
What I did was created an ArrayList that stores all the position of selected items, and toggle the background colors on clicks.
In my Adapter I define:
public ArrayList<Integer> selectedIds = new ArrayList<Integer>();
With the following method :
public void toggleSelected(Integer position)
{
if(selectedIds.contains(position))
{
selectedIds.remove(position);
}
else
{
selectedIds.add(position);
}
}
which addes\removes items from the ArrayList
In my getView method :
if (selectedIds.contains(position)) {
convertView.setSelected(true);
convertView.setPressed(true);
convertView.setBackgroundColor(Color.parseColor("#FF9912"));
}
else
{
convertView.setSelected(false);
convertView.setPressed(false);
convertView.setBackgroundColor(Color.parseColor("#000000"));
}
This checks if the position is storred in the ArrayList. if it does, paint it as selected. if not, the opposite.
all is left is the OnItemClick listener, i added :
((YourAdapter)list.getAdapter()).toggleSelected(new Integer(position));
When YourAdapter is the adapter of your ListView
Hope this helps anyone, as it's a generic answer :)
Thanks to eric.itzhak here : How to change background color of selected items in ListView?
I have an intent launched activity with a number of spinners on the page. I've just set up my spinner's ItemSelected listeners, following this guide. The problem is, the first item in each Spinner is basically "Please select", just so it's not a blank box: so my ItemSelected Listener detects the fact that Please select is in the spinner and seems to assume that it was selected rather than loaded by default. Ideally I want the listener to only detect when an actual choice is made. What is the best way to ignore the default selection?
Here's the relevant code:
ageSpinner = (Spinner) findViewById(R.id.ageSpinner);
ageAdapter = ArrayAdapter.createFromResource
(this, R.array.ageArray, android.R.layout.simple_spinner_item);
ageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ageSpinner.setAdapter(ageAdapter);
ageSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
The problem is that the selection is done on the first layout phase. So the first time your layout is computed, the Spinner raises onItemSelected. Its quite annoying, but its the way it works. You could try a couple different things, but given you already have Please Select Something items, the best you could do is ignore the event when the selected item is 0.
Set a boolean to false, check it in the onItemSelected and if false, set it to true and do nothing else. Next time, when it's true do as you normally would.
There are lots of approaches possible here. For example, you can override the normal behaviour of a Spinner using reflection, or simply switch to a Button imitating a Spinner, which on clicking on it pops up an AlertDialog.
However, other solutions can easier to implement. Barak has named one, while an alternative would be to simply check the selected position in the Spinner, assuming that "Please select" will always be the first item (and hence, at position 0) and you're not doing any sorting on the items.
I'm sure you'll find one of the possibilities suitable for your problem, but perhaps you can also a elaborate a bit more on why exactly it's problematic onItemSelected(...) gets fired for the initial selection?
Solution by OP.
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
if (pos != 0) {
//this if makes sure it ignores 0, which is
//Please Select in the drop down
Toast.makeText(parent.getContext(), parent.getTag().toString() +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
So I have a spinner (spinner2 here) which is populated via an ArrayAdapter from an SQLite table. On selecting an item I want it
deleted from the DB
deleted from the spinner
The code below actually works. Except when the spinner has only one item. When that happens
it seems onItemSelected is not called at all.
I get the following LogCat
10-01 22:30:55.895: WARN/InputManagerService(1143): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#45a06028
Oh and when two items are populating the spinner, spinner.getcount() shows two items, so it's not some strange case of the system thinking the spinner is empty or something like that.
This is the code:
public class SpinnerItemSelectListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(parent == spinner2){
if(autoselected){
autoselected=false;
}
else{
//uniqvalarray is the arraymade from pulling data from SQLite and populaitng array adapter
Integer i = uniquevalarray.get(pos);
deleteRow(i);//deletes the row from the database and repopulates the above array.
autoselected=true;//just a boolean to stop autoslecting in onCreate()
//makeAlert(i);initially wanted to make alert box.
loadSpinner2();//reloads the spinner with new data
}
}
}
public void onNothingSelected(AdapterView parent) {
//TODO
}
}
The spinner runs this way : Only fire when you change the selected item . If you dont change that element , cause its the only one that exist , it can't change .
The solution i think you must use is using a button next to the spinner to throw the delete funcions.
You must think that Spinner is not made to be with an unique element , cause only changes usually when you change the selected one . then the natural solution can be that .
I want to have three Spinners with contents which depend on each other.
E.g. Spinner1 displays {item1, item2} and Spinner2 either {item3, item4} or {item5, item6} depending on whether item1 or item2 is selected on Spinner1.
The same I want for Spinner 3, which reacts to changes of Spinner1 and/or Spinner2.
For the latter, I have to determine first which of the possible value sets is shown atm in Spinner2.
My question is kind of similar to this question, but I don't know what to do after getting the adapter.
That's what I have so far:
ArrayAdapter adapter1 = (ArrayAdapter) spinner2.getAdapter();
if(items_spinner1[0].contentEquals(adapter1.getItem(0)))
{
//...
}
I get the adapter, ask for the first value and compare it to the first String value of my Array to identify it. It doesn't at all seem elegant to me. Is there an easier solution?
You say the contents of the later spinners depend on the selection in the earlier ones, but the code you posted depends only on the contents of a spinner.
adapter1.getItem(0) returns the first item in the list, not the currently selected item. To get the currently selected item, use the spinner's (not the adapter's) getSelectedItem() method.
You could, for example, put something like this in your first Spinner's onItemSelectedListener (edited based on your comment below):
public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
Object selectedItem = parent.getSelectedItem();
// Do this if the first Spinner has a set of options that are
// known in advance.
if (/*selectedItem is something*/) {
// Set up the second Spinner in some way
} else if (/*selectedItem is something else*/) {
// Set up the second Spinner in another way
}
// OR, if you need to do something more complex
// that would cause too much clutter here, do this.
fillSecondSpinner(selectedItem);
}
Then place something similar in the second Spinner's onItemSelectedListener. Get the selected items from the first and second Spinners using getSelectedItem() (or the item positions using getSelectedItemId() for the first and the position parameter for the second). Use the selected items to set up the third.
Edit: The OnItemSelectedListener for the second Spinner would look something like this.
// This must be defined in the enclosing scope.
final Spinner firstSpinner; // Must be final to be accessible from inner class.
Spinner secondSpinner;
// ...
secondSpinner.setOnItemSelectedListener(new OnItemSelectedListener {
public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
// Again, usually the selected items should be of
// a more specific type than Object.
Object firstSelection = firstSpinner.getSelectedItem();
Object secondSelection = parent.getSelectedItem();
fillThirdSpinner(firstSelection, secondSelection);
}
public void onNothingSelected (AdapterView<?> parent) { }
});