Reputation: 45
I've created a popup window and put a spinner inside it. But I can't get values when an item is selected from it.
My code to create popup:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popwindow=new PopupWindow(inflater.inflate(R.layout.addpain, null,false),300,350,true);
popwindow.showAtLocation(this.findViewById(R.id.tabHost), Gravity.CENTER, 0, 0);
XML file of PopUp
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#A0BBBBBB">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#A0BBBBBB" >
<TextView
android:id="@+id/addpaintext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/addpain"
android:textColor="#000000"
android:textSize="18dp"
android:typeface="serif" />
<EditText
android:id="@+id/statustbox"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/addpaintext"
android:layout_marginTop="17dp"
android:hint="@string/addpain"
android:inputType="textMultiLine"
android:textColor="#000000"
android:textSize="20dp"
android:typeface="serif" />
<Button
android:id="@+id/addpainbutton"
android:layout_width="80dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/statustbox"
android:layout_marginLeft="18dp"
android:layout_marginTop="98dp"
android:onClick="statusupload"
android:text="@string/statusupload" />
<Button
android:id="@+id/cancelbutton"
android:layout_width="80dp"
android:layout_height="45dp"
android:layout_alignBaseline="@+id/addpainbutton"
android:layout_alignBottom="@+id/addpainbutton"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
android:onClick="canceladdpain"
android:text="@string/cancel" />
<Spinner
android:id="@+id/spinner_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/addpainbutton"
android:layout_alignRight="@+id/cancelbutton"
android:layout_below="@+id/statustbox"
android:layout_marginTop="20dp"
android:prompt="@string/testtxt"
android:entries="@array/list"/>
</RelativeLayout>
</LinearLayout>
My StatusUpload Function:
public void statusupload(View view)
{
EditText status=(EditText) popwindow.getContentView().findViewById(R.id.statustbox);
Spinner spinner=(Spinner) popwindow.getContentView().findViewById(R.id.spinner_list);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id)
{
category=parent.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
category="Miscellaneous";
}
});
Toast.makeText(getBaseContext(),category, Toast.LENGTH_LONG).show();
}
What happens is the vraible category isnt getting any values !! IT's just blank.
Upvotes: 1
Views: 3587
Reputation: 5636
you're not seeing the results you want because everything hinges on the button to turn on your spinner listener. thus you have have to click the button, make a spinner selection, and then click the button again. I'd seriously reconsider the design. For instance, are you aware that you don't need the listener on to get the item selected? you could just use:
public void statusupload(View view)
{
EditText status=(EditText) popwindow.getContentView().findViewById(R.id.statustbox);
Spinner spinner=(Spinner) popwindow.getContentView().findViewById(R.id.spinner_list);
category = spinner.getSelectedItem().toString();
Toast.makeText(getBaseContext(),category, Toast.LENGTH_LONG).show();
}
Upvotes: 1