Android spinner performClick onItemSelected

I have a little problem with spinner.

I create a button spinnerthat the user clicks on the button. spinnerdisplayed as it should, but onItemSelectednothing happens when called .

Here is the code

public void setUpSpinner(){
    spinner = new Spinner(this);
    CustomArrayAdapter<String> adapter = new CustomArrayAdapter<String>(this,     android.R.layout.simple_spinner_item, getAsStrings());
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);
}

public void onClick(View view) {       
    spinner.performClick();
}

public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {        
    String getName = (String) spinner.getSelectedItem();
    getListFromName(getName);
}

Does anyone know what's wrong here?

Thanks guys.

+5
source share
3 answers

Solved the problem by adding Spinnerto mine xmlwith a height and width set to zero.

+16
source

It looks pretty much like a turior, so back to that. See below:

, OnItemSelectedListener? , Spinner onCreate() .

onCreate();

spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
0

Why do you have spinner = new Spinner(this)to configure?

you probably already have a Spinner in the XML of your layout, then you just do it spinner = (Spinner) findViewById(R.id.WHATEVER_THE_ID_IS_IN_THE_XML);, so you don’t neednew

PS this is how I define Spinner in an XML layout

<Spinner
android:id="@+id/SPINNER_ID"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_horizontal" />
0
source

All Articles