Access Spinner in Activity without define in Layout
Spinner spinner = new Spinner(this);
String option[]={"By Date","By Transaction ID","By Customer Phone"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,option);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item );
spinner.setAdapter(spinnerArrayAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
Log.d("in select","yes");
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
Toast.makeText(MainMenu.this,"no",Toast.LENGTH_LONG).show();
Log.d("In No select","No");
}
});
This all are done very fine, a new pop up display of Spinner view is genera开发者_如何学Cte on the previous view and previous view still display but dim, all focus on this current Spinner
But the problem is when I select any item in this spinner then onItemSelected() should be called but it is not called.
but if I write
setContentView(spinner);
then onItemSelected() is called, but then previous view is not displayed now.
so how can i access spinner without this line setContentView(spinner);
I want to get the position of the selected Item in Spinner
Please provide me code or example
i am waiting for your kind reply
You must bind your spinner to your Layout, if you don't wont to use setContentView(spinner);
. Use something like this at the end of your code
LinearLayout line = (LinearLayout) findViewById(R.id.line);
line.addView(spinner);
and your Layout is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/line">
</LinearLayout>
精彩评论