简体   繁体   中英

Click on spinner element android

I created a spinner in my main.xml:

<Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/categoria_arrays"
        android:prompt="@string/categoria_prompt" />

And in the strings.xml the values:

<string name="categoria_prompt">Choose</string>
   <string-array name="categoria_arrays">
        <item>All</item>
        <item>One</item>
        <item>Two</item>
        <item>Three</item>
    </string-array>

I can display it normally but actually there are no interactions.. I need that onClick over a item open a new activity for example. So if i click the item at position 2 i need go in the activity One . Is it possible?

I tried to create a toast when i click a item but not works:

public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
        Toast.makeText(parent.getContext(), 
            "OnItemSelectedListener : " + 
                                   parent.getItemAtPosition(pos).toString(),
            Toast.LENGTH_SHORT).show();
      //HERE CHANGE ACTIVITY
      }

      @Override
      public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
      }

I How could i do?

Yes it is possible. Do it like this

public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
        if (pos == 1){
                Intent i = new Intent(currentActivity.this, One.class);
                startActivity(i);

        }else if (pos == 2)
        {
                Intent i = new Intent(currentActivity.this, Two.class);
                startActivity(i);

        }else if (pos == 3){
                Intent i = new Intent(currentActivity.this, Three.class);
                startActivity(i);
      }
      }

and so on...

You need to set item selected listener like this:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        // your code here
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
    }

});

Simply call something like this in your onCreate after your setContentView:

Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
                Toast.makeText(parent.getContext(), "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
                Toast.LENGTH_SHORT).show();
                //HERE CHANGE ACTIVITY

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }

});

You need to explicitly set the listener for the spinner.

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> arg0, View arg1,
        // your code here
    }
    public void onNothingSelected(AdapterView<?> arg0) {
        // your code here
    }
}); 

Did you just added onItemSelected method, or setted onItemSelectListener? onItemSelected and onNothingSelected are just methods, you need to use onItemSelectListener.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM