Android:如何获取微调器值并检查条件

时间:2013-02-12 13:12:22

标签: android android-spinner

在我的应用程序中,我有一个带有两个项目的微调器..我必须选择相应的操作项目

    dialogButton.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {

                        String Comment = edittext.getText().toString();
                           String choose = spinner.getSelectedItem()
                                    .toString();
                             if (Comment != null && Comment.equalsIgnoreCase("")){
                            post(Comment,choose);
                            getActivity().finish();

                             }else{
                                 showToast("Please enter you comment!");

                             }  
                    }
                });
                dialog.show();
                dialog.getWindow().setAttributes(lp);

            } catch (Exception e) {
                e.printStackTrace();
                getActivity().finish();
            }
        }

        private void post(String comments, String choose) throws Exception{

            StringBuffer finalMessage = new StringBuffer("\nRecomends " + Data.getfName());
            if(Data.getAddress() != null && !Data.getAddress().isEmpty()){
                finalMessage.append("\n" + Data.getAddress());
            }

----------------->      if(spinner.getSelectedItem().toString().equals("Post to personal directory & FB")){
        Bundle params = new Bundle();
        params.putString("message",finalMessage.toString() );
        publishStory(params,comments);

    }else {
         try {
                new FBUtils(activity).sharePlaces(attractionData, comments, null);
            } catch (Exception e) {
                Log.e(TAG,"sharePlaces error ",e);
            }
    }
        }

        private void publishStory(Bundle postParams,final String comments,final String choose) {
            Session session = Session.getActiveSession();

            if (session != null){
        List<String> permissions = session.getPermissions();
                    if (!isSubsetOf(PERMISSIONS, permissions)) {
                        Session.NewPermissionsRequest newPermissionsRequest = new Session
                                .NewPermissionsRequest(this, PERMISSIONS);
                    session.requestNewPublishPermissions(newPermissionsRequest);

                    }
                    Request.Callback callbackRequest= new Request.Callback() {
                        public void onCompleted(Response response) {
                            if (response == null || response.equals("")
                                    || response.equals("false")) {
                                showToast("Blank response.");
                            } else  


                                new fbUtils(activity).share(Data, comments, response.getError(),choose);
                            } catch (Exception e) {
                                Log.e(TAG,"sharePlaces error ",e);
                            }
                            }

                    };
                    Request request = new Request(session, Constants.fb.fbId + "/comments", postParams, 
                                          HttpMethod.POST, callbackRequest);

                    RequestAsyncTask task = new RequestAsyncTask(request);
                    task.execute();


                }

问题是post()在点击其进入else部分的dailogbutton时没有执行,我想在post()方法中执行if条件,感谢任何帮助

2 个答案:

答案 0 :(得分:0)

当您从微调器获得值时。该值可能不会更新。

您应该使用项目选择的侦听器。

String result =“”;

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
                @Override
               public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {
      Toast.makeText(parent.getContext()), "The planet is " +
          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
// Your variable should update here 
result = parent.getItemAtPosition(pos).toString();
    }



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

            });

现在将onPost方法中的值用作:

private void post(String comments, String choose) throws Exception{

            StringBuffer finalMessage = new StringBuffer("\nRecomends " + Data.getfName());
            if(Data.getAddress() != null && !Data.getAddress().isEmpty()){
                finalMessage.append("\n" + Data.getAddress());
            }
Log.v("TEST",spinner.getSelectedItem().toString());
----------------->      if(result.equals("Post to personal directory &#38; FB")){
        Bundle params = new Bundle();
        params.putString("message",finalMessage.toString() );
        publishStory(params,comments);

    }else {
         try {
                new FBUtils(activity).sharePlaces(attractionData, comments, null);
            } catch (Exception e) {
                Log.e(TAG,"sharePlaces error ",e);
            }
    }
        }

答案 1 :(得分:0)

试试这样:

private Spinner status;

status = (Spinner) findViewById(R.id.status);
final ArrayList<Simplestatus> statusList = new ArrayList<Simplestatus>();

        Simplestatus tempstatus = new Simplestatus();
        tempstatus.setSimpleStatusName("ZERO");
        tempstatus.setSimpleStatusValue("0");

        Simplestatus tempstatus1 = new Simplestatus();
        tempstatus1.setSimpleStatusName("ONE");
        tempstatus1.setSimpleStatusValue("1");

        statusList.add(tempstatus);
        statusList.add(tempstatus1);


SpinnerAdapter stateAdaptor = new SpinnerAdapter(SettingActivity.this,statusList);
        // stateList.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        status.setAdapter(stateAdaptor);
        status.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
                changeStatus(statusList.get(position));
                selected = statusList.get(position).getsimpleStatusName();
                System.out.println("selected"+selected);
            }

            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });

public class Simplestatus 
{
    private String simpleStatusName;
    private String simpleStatusValue;


    public String getSimpleStatusName() 
    {
        return simpleStatusName;
    }

    public void setSimpleStatusName(String simpleStatusName) 
    {
        this.simpleStatusName = simpleStatusName;
    }

    public String getsimpleStatusValue() 
    {
        return simpleStatusValue;
    }

    public void setsimpleStatusValue(String simpleStatusValue) 
    {
        this.simpleStatusValue = simpleStatusValue;
    }
}