why does my android spinner display the radio button in the textview? - android

i have a spinner with an arrayadapter that is dynamically managed. when it gets displayed, the spinner text also displays the radio button. how do i get rid of this radio button? the drop down arrow is all strecthed and yucky... thats my problem.
NOTE: i'm not talking about the radio buttons that appear in the list that is displayed when i select the drop down on the spinner.
here are the appropriate code snippet... couple of points:
this code is in the constructor of widget which is a subclass of Spinner
value is an array of Object instances (passed when the widget gets created)
there are no XML resources; all widgets are dynamically created
thinking i need to "manipulate" the prompt, i added setPrompt(...) in the constructor and also in the onitemclicked event listener... this had no effect.
Q: what am i missing? seems to me i'm missing some attribute of the Spinner which is causing the radio button to also display in the text part of the spinner.
-- snip code --
public class ChoiceGroupImpl extends Spinner implements OnItemSelectedListener {
public ChoiceGroupImpl(Activity activity, WidgetContainer container, Value widget, AttributeImpl attributes, Object[] value, int selected) {
...
adapter = new ArrayAdapter<CharSequence>(activity, R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
for (int i = 0; i < value.length; i++)
adapter.add(value[i].toString());
if (attributes.isReadonly())
setEnabled(false);
setAdapter(adapter);
setSelection(selected);
setPrompt(adapter.getItem(selected));
setOnItemSelectedListener(this);
...
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setPrompt(adapter.getItem(position));
((ToolkitImpl) Toolkit.getInstance()).hiddenCommand(container, "SelectionChanged");
}
...
-- end snip code --

If you want to keep radio button in the spinner, but not in the textview then do this:
adapter = new ArrayAdapter<CharSequence>(activity, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

use simple_spinner_item instead of simple_spinner_dropdown_item while creating your adapter
adapter = new ArrayAdapter<CharSequence>(activity, R.layout.simple_spinner_item);

Related

Listen for array adapter onClick

I have a MultiAutoCompleteTextView and when you click on an item in my list of AutoComplete words, it is inserting the word I want correctly, however, when I click on the word I want to insert, I want to move my cursor to a different position than the end of the edittext's text. How can I set a listener for my adapter? My code is as follows:
setContentView(R.layout.activity_main);
String[] suggestions = getResources().getStringArray(R.array.list_of_suggestions);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,suggestions);
MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView)findViewById(R.id.code_text);
textView.addTextChangedListener(mTextEditorWatcher);
textView.setAdapter(adapter);
textView.setTokenizer(new SColonTokenizer());
I can't set it in my text changed listener as this is called every time the user enters a character. How can I listen for an onClick on my array adapter? I know it is possible to do this using a ListView, but my suggestions are handled by the adapter and not making use of a listView to do this.
After clicking, I want to move the cursor inside the parenthesis, I know how to do this using
textView.setSelection(textView.getText().length());
just I am unsure how to call this after clicking on an array adapter item
You can use OnItemClickListener like
textView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
textView.setSelection(textView.getText().length());
}
});

Android How to retrieve value of Dynamically added Spinner

I am dynamically adding the Spinners in my application by parsing the XML file.
I have done using the below code
List<Spinner> allspin = new ArrayList<Spinner>();
Spinner spin = new Spinner(getParent());
allspin.add(spin);
spin.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getParent(),
android.R.layout.simple_spinner_item, selectval);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
Spinners are displayed correctly but i don't know how to retrieve the value of selected spinner. If there is one spinner i can retieve, but there are multiple how should i do?
You can get the reference of the spinner from your arraylist like this :
Spinner spn = allspin.get(index);
After that you can get the selected item by simply calling:
spn.getSelectedItemPosition();
You can set id of spinner dynamically.
For that,you can use
spin.setId(i); //if you use i of for loop for creating multipal spin at a tym or you can use a global variable i,incremented by one each time you create a spinner
and further,you can use those ids to get values from particular spinner.
Example:
for(int i=1;i<4;i++)
{
Spinner spin=new Spinner(getApplicationCotext());
spin.setId(i);
...
//other code
...
mLayout.add(spin);//add this spinner to your layout(mLayout is object of your layout in xml)
}
Now,
for(int i=1;i<4;i++)
{
Spinner sp=(Spinner)findViewById(i);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
System.out.println(sp.getText().toString());//prints values of a pinner when it is changed/selected
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
If you need multiple spinners (I'm not sure I understand your use case), then, as you're creating each of them, you need to also create the appropriate listeners. You shouldn't have to retrieve values of spinners yourself; handling these selection events should be done in your listeners. Perhaps if you explained what you're attempting to do a bit better it would be easier to help...

How to dynamically change the Spinner's items

I have two spinners. Country and City.
I want to dynamically change the City's values upon Country selection.
I know how to create and initialize the values of Country but don't know how to set and change the City's values.
Any guidance is appreciated.
UPDATES1
The problem is, I don't have idea on how to update the content of City Spinner. I know listener and other basics on how to create a fixed spinner.
For the second spinner, use an Adapter over a List<String> (or whatever your City representation is). After changing the contents of the list, call notifyDataSetChanged on the adapter, and that will do.
You need to get a programmatic reference to the spinner, something like this:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Then to update your city's values, use an OnItemSelectedListener, like this:
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//update content of your city spinner using the value at index,
// id, or the view of the selected country.
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
Finally, you need to bind the listener to the country spinner like this:
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
see here for reference: http://developer.android.com/resources/tutorials/views/hello-spinner.html

How to set onClickListener() for view of Spinner?

I want to catch user interaction with a spinner like onCLickListener. Do to the 'don't call onClickListener() on AdapterView' error I found recommendations that you should override a constructer with a custom spinner to set onClickListener() on the view the spinner creates.
Tried that:
public class MySpinner extends Spinner {
public static final String TAG = "MyApp";
public MySpinner(Context context, AttributeSet attrs) {
super(context, attrs);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.temp_systems, android.R.layout.simple_spinner_item);
TextView spinner_text = (TextView) findViewById(android.R.id.text1);
OnClickListener spinnerOnClickListener = new OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "Should do something!");
}
};
spinner_text.setOnClickListener(spinnerOnClickListener);
setAdapter(adapter);
}
}
but when I try to include this in a layout I get a crash on failing to inflate this item.
To clarify here, onItemClickListener fires when the user clicks an item in the dropdown menu, not when the spinner is collapsed. I need to intercept after the initial spinner is clicked but before it creates the dropdown menu.
What they might mean is to Override the setOnItemClickListener and then call that in the constructor. So in your mySpinner class you would need to add: [notice it is called on ITEM click listener, that might also be part of your issue]
#Override
public void setOnItemClickListener(
android.widget.AdapterView.OnItemClickListener l) {
super.setOnItemClickListener(l);
//... do action here that you want to happen when item in spinner is clicked
}
Dunno if that will fix your issue but I hope it helps. Good Luck.
also it might be worthwhile to possibly use
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//do stuff
}
Not sure why you are needing to extend Spinnner or why you are assigning a click listener on the TextView. You should be assigning a selection listener on the spinner. As per the Spiner example consider:
1) Having a layout with a Spinner control
2) Having a 'Spinner' member of your activity
3) Inflate the layout via setContentView and then assign the spinner member via findViewById
4) Set the adapter for the spinner and call setOnItemSelectedListener on your spinner to assign a selection listener
You can use OnTouchListener instead of OnClickListener, which is not available for Spinner.

Android: Spinner with changing ArrayAdapter - how to identify?

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) { }
});

Categories

Resources