2013-02-12 56 views
0

在我的應用程序,我有兩個項目的微調..我有選擇的項目做動作相應Android的:如何獲得微調值和檢查車況

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()不執行時點擊dailogbutton進入其他部分,我想執行if條件中的post()方法,任何幫助表示讚賞

+0

確定..是什麼,如果條件得到的值..請打印控制檯Log.v(「TEST」,spinner.getSelectedItem()。toString()) – 2013-02-12 13:16:31

+0

@ArpitGarg iam獲取默認項目..項目中的第一個值 – teekib 2013-02-12 13:20:41

+0

@ArpitGarg獲取此02-12 18:49:57.235:D/CommentFragment(18696):spinner ---- ------> Post to personal directory&FB – teekib 2013-02-12 13:22:50

回答

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); 
      } 
    } 
     } 
+0

這個答覆不是張貼自己什麼dialogBu​​tton.setOnClickListener(新OnClickListener(){ 公共無效的onClick(視圖v){ 字符串評論= edittext.getText()的toString(); 串選擇= spinner.getSelectedItem () .toString(); if(Comment!= null && Comment.equalsIgnoreCase(「」)){ post(Comment,choose); getActivity()。finish(); – teekib 2013-02-12 13:36:13

+0

teekib我剛纔建議使用item選定的監聽器並將該值存儲在公共變量中......並根據需要在帖子或任何地方使用它... – 2013-02-12 13:41:04

+0

teekib似乎有點困惑... – 2013-02-12 13:41:22

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; 
    } 
}