Home Having a little issue with an action of my spinner
Reply: 3

Having a little issue with an action of my spinner

Piezord
1#
Piezord Published in 2013-12-28 14:53:12Z

I wanted to add some actions to my spinner (as you can see in my code below, I added some Toasts).

MainActivity.java file:

public class MainActivity extends Activity  implements AdapterView.OnItemSelectedListener{

    String[] items = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner1.setOnItemSelectedListener(this);

        ArrayAdapter AA = new ArrayAdapter(this, android.R.layout.simple_spinner_item, items);

        AA.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(AA);

    }

        @Override
        public void onItemSelected(AdapterView parent, View view, int position, long id) {

            String selectedItem = parent.getItemAtPosition(position).toString();
            if(selectedItem.equals("Item 1")) {         
                Toast.makeText(getApplicationContext(), "Item 1", Toast.LENGTH_LONG).show();
            }

            String selectedItem2 = parent.getItemAtPosition(position).toString();
            if(selectedItem.equals("Item 2")) {         
                Toast.makeText(getApplicationContext(), "Item 2", Toast.LENGTH_LONG).show();
            }

            String selectedItem3 = parent.getItemAtPosition(position).toString();
            if(selectedItem.equals("Item 3")) {         
                Toast.makeText(getApplicationContext(), "Item 3", Toast.LENGTH_LONG).show();
            }

            String selectedItem4 = parent.getItemAtPosition(position).toString();
            if(selectedItem.equals("Item 4")) {         
                Toast.makeText(getApplicationContext(), "Item 4", Toast.LENGTH_LONG).show();
            }

            String selectedItem5 = parent.getItemAtPosition(position).toString();
            if(selectedItem.equals("Item 5")) {         
                Toast.makeText(getApplicationContext(), "Item 5", Toast.LENGTH_LONG).show();
            }

        }

        @Override
        public void onNothingSelected(AdapterView parent)  {


        }
}

activity_main.xml file:


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="89dp" />

    </RelativeLayout>

So, actually this code works fine BUT if I open the app, the first Toast (with the text "Item 1") is already visible. But I don't want that. So, what do I need to do, that this doesn't happen?

Thanks in advance!

Sush
2#
Sush Reply to 2013-12-28 14:59:21Z

try this

public class MainActivity extends Activity  implements AdapterView.OnItemSelectedListener{

String[] items = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
boolean app_started = true;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 

    Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(this);

    ArrayAdapter AA = new ArrayAdapter(this, android.R.layout.simple_spinner_item, items);

    AA.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(AA);

}

    @Override
    public void onItemSelected(AdapterView parent, View view, int position, long id) {

        String selectedItem = parent.getItemAtPosition(position).toString();
  if(!app_started )
{


        if(selectedItem.equals("Item 1")) {         
            Toast.makeText(getApplicationContext(), "Item 1", Toast.LENGTH_LONG).show();
        }

        String selectedItem2 = parent.getItemAtPosition(position).toString();
        if(selectedItem.equals("Item 2")) {         
            Toast.makeText(getApplicationContext(), "Item 2", Toast.LENGTH_LONG).show();
        }

        String selectedItem3 = parent.getItemAtPosition(position).toString();
        if(selectedItem.equals("Item 3")) {         
            Toast.makeText(getApplicationContext(), "Item 3", Toast.LENGTH_LONG).show();
        }

        String selectedItem4 = parent.getItemAtPosition(position).toString();
        if(selectedItem.equals("Item 4")) {         
            Toast.makeText(getApplicationContext(), "Item 4", Toast.LENGTH_LONG).show();
        }

        String selectedItem5 = parent.getItemAtPosition(position).toString();
        if(selectedItem.equals("Item 5")) {         
            Toast.makeText(getApplicationContext(), "Item 5", Toast.LENGTH_LONG).show();
        }

 }
   app_started = false;

    }

    @Override
    public void onNothingSelected(AdapterView parent)  {


    }
}
Community
3#
Community Reply to 2017-05-23 10:25:30Z

Unfortunately, google hasn't provided the feature of having a 'null' value at the start. You can check this link for more details. Hope it helped. :)

Kanwaljit Singh
4#
Kanwaljit Singh Reply to 2013-12-28 15:00:44Z

Try this-

String selectedItem = parent.getItemAtPosition(position).toString();
if(selectedItem.equals("Item 1")) {         
Toast.makeText(getApplicationContext(), "Item 1", Toast.LENGTH_LONG).show();
}
else if(selectedItem.equals("Item 2")) {         
Toast.makeText(getApplicationContext(), "Item 2", Toast.LENGTH_LONG).show();
}
else if(selectedItem.equals("Item 3")) {         
Toast.makeText(getApplicationContext(), "Item 3", Toast.LENGTH_LONG).show();
}
else if(selectedItem.equals("Item 4")) {         
Toast.makeText(getApplicationContext(), "Item 4", Toast.LENGTH_LONG).show();
}
else if(selectedItem.equals("Item 5")) {         
Toast.makeText(getApplicationContext(), "Item 5", Toast.LENGTH_LONG).show();
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306875 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO