Spinner is activated at the start of my activity - android

I'm trying to use the spinner object in my app, but I'm having trouble, in fact, it is activated at the start of my activity. While I wish it was enabled when was clicked and selected the desired item.
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.ElementaryOperations, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
index1 = s1.getSelectedItemPosition()+1;
if (index1 == 3){
finish();
startActivity(prod);//
}
}
public void onNothingSelected(AdapterView<?> arg0) {}
});

spinner listener OnItemSelectedListener is called when you start activity due to get default value.i mean first element and zeroth index. so you can check if position is zero then dont anything.but mind that you cant achieve default selected value.if you put condition for zero.
if cant work proper than see this question

Related

Get last selected Spinner value before change

I have a spinner and I want to get the last selected value of it when I click on the spinner (basically just before the items in the list are being shown).
How can I achieve this?
Update:
Example: There are a dynamic amount of spinners. User clicks in spinner 1 and selects a value, let's say "house". Then clicks somewhere else. Then clicks in spinner 1 again. What I now need is to return the value "house" that was previously was selected before the user selects a different value i.e. "car" in that spinner. I can not use the local storage to save that value beforehand because it's going to be a dynamic amount of spinners to be added.
And yes, I did in fact read the documentation at http://developer.android.com/guide/topics/ui/controls/spinner.html already but what I need isn't explained there.
Maybe you can declare a String array and then use it to storage the items values. Showing this in a TextView is a way to see if it works.
Something like this:
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
selection.setText(items[arg2]);
}
I already have faced a similar situation, but I don't remember exactly how I solved it. Tell me if it gives a better idea to you, I'm new participating here.
Spinner mySpinner=(Spinner) findViewById(R.id.your_spinner);
String text = mySpinner.getSelectedItem().toString();
When the user selects an item from the drop-down, the Spinner object
receives an on-item-selected event.
To define the selection event handler for a spinner, implement the
AdapterView.OnItemSelectedListener interface and the corresponding
onItemSelected() callback method. For example, here's an
implementation of the interface in an Activity:
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
The AdapterView.OnItemSelectedListener requires the onItemSelected()
and onNothingSelected() callback methods.
Then you need to specify the interface implementation by calling
setOnItemSelectedListener():
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
IN YOUR CASE:
TO se if the user clicked on the spinner, do this:
if (spin.getSelectedItemPosition() < 0) {
Spinner mySpinner=(Spinner) findViewById(R.id.your_spinner);
String text = mySpinner.getSelectedItem().toString();
}
This means user clicked the spinner, but not any items of it.
EDIT AGAIN
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
> ...
>
> public void onItemSelected(AdapterView<?> parent, View view,
> int pos, long id) {
> parent.getItemAtPosition(pos) //THIS GETS YOU THAT VALUE THAT THE USER CLICKED ON DYNAMICALLY
> }
>
> public void onNothingSelected(AdapterView<?> parent) {
> // Another interface callback
> }
> }
http://developer.android.com/guide/topics/ui/controls/spinner.html
Solution is to add a TextView widget with each added Spinner. Set the TextView to "gone" (not visible). When the spinner changes set the value to that TextView. This way you can store the last selected spinner value in the TextView. When the user clicks on the spinner and changes the value you can still access the previously selected/unselected value from the spinner through the TextView widget.
// Use this TextView to temporarily store the spinner value
final TextView hiddenTextView = new TextView(getContext());
hiddenTextView.setVisibility(View.GONE);
final Spinner spinner = new Spinner(getContext());
spinner.setPrompt(getString(R.string.email_other));
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(), R.array.email_types, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Log.d(TAG, "TextView Value before spinner change: " + hiddenTextView.getText());
String spinnerValue = String.valueOf(spinner.getSelectedItem());
hiddenTextView.setText(spinnerValue);
Log.d(TAG, "TextView Value after spinner change: " + hiddenTextView.getText());
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
return;
}
});

Android Spinner taking time, after select value from adapter

In my application there are lot's of views(spinner, buttons, editText, textView, ImageButton etc).
After selecting one value in the spinner it not reflect the value which I selected. The old value is till there on the spinner. But when I tap on other views like editTest then spinner value automatically updated.
I think no need to mention the code because normal spinner is there which populating data from ArrayAdapter and I am doing small task after selecting value.
CODE:-
spinnerWard = (Spinner) findViewById(R.id.spinnerWard);
aAdapterWard = new ArrayAdapter<WardList>(this,
android.R.layout.simple_spinner_item, listWard);
aAdapterWard.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerWard.setAdapter(aAdapterWard);
spinnerWard.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v,
int pos, long id) {
intWardPosition = pos;
intFinalWardId = listWard.get(pos).getId();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
});

Spinner does not get selected after setting adapter

I have two spinner in dialog. Second one is dependent on first one. I want to bind it and then select it in edit mode. All works fine but second spinner does not get selected. However it get selected when I open my dialog next time.
Here is a part of my code.
ArrayAdapter<String> myAdap1 = (ArrayAdapter<String>) spnForeignKeyTable
.getAdapter();
int spinnerPosition1 = myAdap1.getPosition(objcolumn_schema
.getForeignKeyTable());
spnForeignKeyTable.setSelection(spinnerPosition1);
// Bind Column Spinner.Second spinner
dblist = DBAdapter.getColumns(pf.getString("dbid", ""),String.valueOf(objcolumn_schema.getForeignKeyTableID()));
ArrayAdapter<String> adpf = new ArrayAdapter<String>(
column.this, android.R.layout.simple_spinner_item,
dblist);
adpf.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnForeignKeyColumn.setAdapter(adpf);
int spinnerPosition2 = adpf.getPosition(objcolumn_schema.getForeignKey());
spnForeignKeyColumn.setSelection(spinnerPosition2);
for changing selected item in second spinner when first spinner selection change you will need to set setOnItemSelectedListener for first spinner as:
spinnerPosition1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View v,
int position, long id)
{
// change second Spinner selection here
}
public void onNothingSelected(AdapterView<?> arg0)
{
//
}
});

How to update an Spinner dynamically correctly?

I have a spinner with a custom adapter displaying objects from a database.
When the object list changed I create a new adapter with the List and apply it on the spinner. Afterwards the first item is selected, so I tried this:
// 5th item selected
int pos = spinner.getSelectedItemPosition();
spinner.setAdapter(newAdapter);
// 0th item selected
spinner.setSelectedItem(pos);
// 5th item is selected
But the GUI does still show the first item?
spinner.invalidate() did not help.
Is this the correct way to achieve what I want? I really could not find any information on this behavior.
Solved: I guess the main problem was the custom spinner adapter. This works fine now
if (spinner.getCount() > 0) {
pos = spinner.getSelectedItemPosition();
}
MySpinnerAdapter adapter = new MySpinnerAdapter(context, myNewObjects);
spinner.setAdapter(adapter);
spinner.setSelection(pos); // needed
adapter.notifyDataSetChanged();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
spinner.setSelectedItem(5);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Did u tried notifydatasetChanged() or notifydatasetInvalidate() method of adapter.
newAdapter.notifydatasetChanged()

How to make spinner return an id in android?

I have made a Spinner in Android:
category = (Spinner) findViewById(R.id.ev_category);
category.setOnItemSelectedListener(new MyOnItemSelectedListener());
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.category_list, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
category.setAdapter(adapter);
The resource for the spinner is:
<string name="category">Choose a category</string>
<string-array name="category_list">
<item>Food</item>
<item>Art And Performance</item>
<item>Classes</item>
<item>Others</item>
</string-array>
See, I am getting my selected Spinner text by implementing the following method. It is returning my selected item text in my Spinner.
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
ev_category=parent.getItemAtPosition(pos).toString();
}
I want the below functionality. If the first item in my spinner is selected I want to get the values as "1" and if the next item in spinner is selected i want to get value as "2".
How can I accomplish this task?
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
pos here is position isn't it?, add 1 to it, if it returns 0 for first item.
if you want string, then convert integer to string using Integer.toString()
in any method u can call getItemPosition = spinner.getSelectedItemPosition();
and you can use (switch) it will return you 0, 1, 2.

Categories

Resources