Odd Android Spinner behavior - android

This is the code to populate my spinner.
The code:
String[] rcs = new String[review_cycles.length];
for (int i = 0; i < review_cycles.length; i++)
rcs[i] = review_cycles[i].ReviewCycleName;
ArrayAdapter<String> rc_spinnerAdapter = new ArrayAdapter<String>(
ActivityManagementReview.this,
R.layout.simple_spinner_item,
rcs);
sp_review_cycle.setAdapter(rc_spinnerAdapter);
sp_review_cycle.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
DtoMrReviewCycleVersion selected_review_cycle_version = review_cycles[position];
if (selected_review_cycle_version != latest_review_cycle_version) {
latest_review_cycle_version = selected_review_cycle_version;
int mr_version_id = latest_review_cycle_version.MrdVersionId;
_URL_version = _url_base + "MrVersion/" + Integer.toString(mr_version_id);
new GetMrVersionInfoAsyncTask().execute();
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
return;
}
});
When I select a value from the spinner, the code is executed twice. Once with the selection I made and then right afterward, the first item in the list is executed again bringing me back to where I started.
How can I prevent this from happening?
Thanks.

When I select a value from the spinner, the code is executed twice.
As I reminded you in the comments, Spinners call onItemSelected() when they load the default value. This feature is useful when you know about it, but since it is not what the average developer expects it is problematic as well.
The Spinner will also call onItemSelected() when the user re-selects the current value... When I want to avoid both of these false alarms I use something like this:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
int previous = -1;
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(previous != position && previous < -1) {
Log.v("Example", "Selected: " + position);
// Do something
}
previous = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
});

Related

Spinner 2 data not populating properly

I am trying to load spinner2 data based on spinner1 item selection. My spinner1 loads without any issues. I have got two categories in spinner1. Before selecting any value on spinner1, my spinner2 is loaded with second categories values.
EDIT:
One thing i realized now. i have got 2 values in spinner1(category). when nothing is selected in spinner1 , spinner2 is loaded with item2's values. if i select item1 in spinner1, it loads properly. If i select item2 in spinner1 nothing is populated in spinner2. Because of my hint addition there is some issue i think.
minimal spinner2 part in MainActivity
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position).toString();
getSpinner2(id);
}
private void getSpinner2(Long id) {
MyRestClient.getForSpinner2(MainActivity.this, "MyRestService/product/"+id,
headers.toArray(new Header[headers.size()]), null, new JsonHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
ArrayList<String> spinnerArray2 = new ArrayList<String>();
final SpinnerAdapter2 spinnerAdapter2 = new SpinnerAdapter2(MainActivity.this, spinnerArray2);
for (int i = 0; i < response.length(); i++) {
try {
JSONObject c = response.getJSONObject(i);
String productArray = c.getString("product");
spinnerAdapter2.add(productArray);
}
catch (JSONException e) {
e.printStackTrace();
}
}
spinnerAdapter2.add("Select One");
spinner2.setAdapter(spinnerAdapter2);
spinner2.setSelection(spinnerAdapter2.getCount());
spinnerAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
and i have got this to show hint on spinneradapter2 class
#Override
public int getCount() {
int count = super.getCount();
return count > 0 ? count - 1 : count;
}
.
.
public View getView(int position, View convertView, ViewGroup parent) {
.
.
if (position == getCount()) {
viewHolder.product.setText("");
viewHolder.product.setHint(products);
} else {
viewHolder.product.setText(products);
}
}
Please try spinnerAdapter2.notifyDataSetChanged(); after adding all elements to your adapter. Maybe it will help. And I thik you should add this items to spinnerArray2 and then create adapter using this object.
You need to know that onitemselected will be called first time without user actions and since you are adding a hint you should check that position is larger than 0 which is the hint position if there is no hint then skip the check of zero position
so your new selection will be like
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position>0){
parent.getItemAtPosition(position).toString();
getSpinner2(position);
}
}
also
spinnerAdapter2.add("Select One");
call this before adding other values to the adapter to make sure it is in first location
Edit:
Put spinner2.setOnItemSelectedListener(MainActivity.this); inside spinner1 setOnItemSelectedListner.
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position).toString();
spinner2.setOnItemSelectedListener(MainActivity.this);
Log.e("Position", ""+position);
if(position > 0){
getSpinner2(position);
} else {
Toast.makeText(getApplicationContext(), "Spinner oth postion is selected", Toast.LENGTH_SHORT).show();
}
}

Spinner Listener not executed in Android

As a continuation of the question here, I found that the listener of the spinner is never executed (or) triggered and I am not able to solve the issue.
Here is the part of the code with the spinner and the listener:
Spinner priority = (Spinner)findViewById(R.id.priority);
priority.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("listener","it works");
Log.d("value",""+parent.getSelectedItem().toString().equalsIgnoreCase("Low"));
/*if(parent.getSelectedItem().toString().equalsIgnoreCase("Low"))
imageId = R.drawable.blue;
else if(parent.getSelectedItem().toString().equalsIgnoreCase("Medium"))
imageId = R.drawable.green;
else
imageId = R.drawable.red;*/
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
In the log, the values of listener and value are not printed at all.
Modified code.
Spinner priority = (Spinner)findViewById(R.id.priority);
priority.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("listener","it works");
//Instead of doing parent.getSelectedItem(), get the adapter from parent and from that adapter get item at position as below.
Log.d("value",""+parent.getAdapter().getItem(position).toString().equalsIgnoreCase("Low"));
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
If your data is string then it will work else you are passing any custom object then you need to cast result from this parent.getAdapter().getItem(position) to that custom object class.
Finally found the solution :
String priortyStr = priority.getSelectedItem().toString();
int imageId = 0;
if(priortyStr.equals("Low"))
imageId = R.drawable.blue;
else if(priortyStr.equals("Medium"))
imageId = R.drawable.green;
else
imageId = R.drawable.red;

Global variable inside Spinner onItemSelected does not work

I've been thinking about this a long while but realy can't figure out what the problem is. The first code piece works in one of my activities. selectedUser is globally declarend and I can use it everywhere.
spUsers.setAdapter(new MyAdapter(this, R.layout.user_sp_row,userArray));
spUsers.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
int index = spUsers.getSelectedItemPosition();
selectedUser = userList.get(index).get(Constants.TAG_UNAME);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
selectedUser = "";
}
});
This code does not work allthough it looks the same like the other code I wrote. selectedCat is also globally declared. The thing is the value is set in the onItemSelected method but as soon as it leaves the method selectedCat is an empty string.
spCat.setAdapter(new MyAdapter(this, R.layout.category_sp_row,tempArray));
spCat.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
int index = spCat.getSelectedItemPosition();
selectedCat = categoryList.get(index).get(Constants.TAG_UPID);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
selectedCat = "";
}
});
I don't know why are using int index = spUsers.getSelectedItemPosition(); to get the index when you can use the position integer provided in onItemSelected() method.
I've got this working by indeed doing everything that needs to be done in the setOnItemSelectedListener().
Ty DrkStr

how to manage two spinner setOnItemSelectedListener android

I used two spinners in same fragment I want when the first spinner will complete its task then only the setOnItemSelectedListener will call for the second spinner.
Issue: But the issue is setOnItemSelectedListener is called on same time for both the spinners.How can I manage that. :(
What I want to do: I want to draw a graph on page load spinner 1 will darw a different graph and spinner 2 will draw a different graphon page load.
Any help will be Appreciated.
Why do you have same setOnItemSelectedListener for both the spinners.
Have structure like this:
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
func1();
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
Toast.makeText(topThis, "herf", Toast.LENGTH_LONG).show();
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
func2();
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
Toast.makeText(topThis, "herf", Toast.LENGTH_LONG).show();
}
});
private void func1()
{
//do task of spinner1
}
private void func2()
{
//your task for second spinner
}
spinnerLeft = (Spinner) getActivity().findViewById(R.id.spinner_left);
dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, arrayList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerLeft.setAdapter(dataAdapter);
spinerRight = (Spinner) getActivity().findViewById(R.id.spinner_right);
rightSpinnerAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, arrayList);
rightSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinerRight.setAdapter(rightSpinnerAdapter);
That is my two spinner that I calle d on onActivityCreated.
spinerRight.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long arg3) {
// here I called a method to download a chart and showing below 1st spinner
AppGlobals.getInstance().checkSpinner = 1;
}
private void addListenerOnSpinnerLeftItemSelection() {
spinnerLeft.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long arg3) {
//here I m called the same method to download and show chart below 2nd spinner
AppGlobals.getInstance().checkSpinner = 1;
}
But the before completing the first spinner functionality the second spinner called. I have but the int value so that when both the spinner's called the int value will tell the difference.
if (AppGlobals.getInstance().checkSpinner == 1) {
chartArea = (LinearLayout) getActivity().findViewById(R.id.chart);
} else if (AppGlobals.getInstance().checkSpinner == 0) {
chartArea = (LinearLayout) getActivity().findViewById(R.id.pie_chart);
}

setting itemListener of variable number of spinners

int numberofSpinner = TransportResult.Transfers.size();
Spinner spin=null;
for(int i=0;i<numberofSpinner;i++)
{
spin = new Spinner(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1);
spinLayout.addView(spin,p);
spin.setId(i);
Transfer transfer = TransportResult.Transfers.get(i);
ArrayList<CharSequence> s = new ArrayList<CharSequence>();
for( Line l : transfer.TransferLine)
{
s.add(l.ShortName+" - "+Helper.FindTransportTypeText(l.LineType));
}
adapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,s);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
}
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(parent.getId()==0){
System.out.println("spin 1 is called");
String str = (String)parent.getSelectedItem();
}else if(parent.getId()==1){
System.out.println("spin 2 is called");
String str = (String)parent.getSelectedItem();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
If the number of spinner is more than 1, only the last spinner is triggered. For example; i have 3 spinner on screen, when i select the item of first or second spinner, the listener is never triggered. Only the third spinner triggers the listener. How can i solve that?
Thank you
UPDATE
when you use more than one spinner then setid for each spinner spin.setId(int) .and you can check id in OnItemSelected method. Mind that when you set OnitemSelected first time onItemSelected is called.
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(parent.getId()==1){
System.out.println("spin 1 is called");
String str = (String)parent.getSelectedItem();
}else if(parent.getId()==2){
System.out.println("spin 2 is called");
String str = (String)parent.getSelectedItem();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

Categories

Resources