2011-09-25 217 views
0

我發現我做了什麼。我使用URL STRING的剩餘部分引用了變量名稱。獲取/使用單選按鈕值android

如何將單選按鈕的值保存到變量中並稍後使用該變量。

我可以在我的LogCat中看到變量Day_Item,該值在那裏,但是當嘗試使用Day_Item後,它不顯示有價值的。

下面是我的代碼中顯示按鈕的一部分。

String Day_Item = null; 
    public class SearchDB extends Activity { 
     private static final String TAG = "MyApp"; 
     String start_log = "STARTED"; 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.search_layout); 


     final RadioButton radio_monday = (RadioButton) findViewById(R.id.monday); 
     radio_monday.setOnClickListener(radio_listener); 
cityspinner.setOnItemSelectedListener(new OnItemSelectedListener() { 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position,long arg3) 
    { 
     int id = parent.getId(); 

     if (spinner2_count2 < spinner2_count1) { 
      spinner2_count2++; } 
      else 
      { 
       String city_spinner_log = "CITY SPINNER"; 
       Log.d(TAG, city_spinner_log); 

        String item = cityspinner.getSelectedItem().toString(); 
        String nameContentType = "name"; 
        String cityURL = "GetRestaurant.php?day=Day_Item&city=" + item; 
        Log.d(TAG, cityURL); 
        String shop_data = DataCall.getJSON(cityURL,nameContentType); 
        Log.d(TAG, shop_data); 
        Bundle bundle = new Bundle(); 
        bundle.putString("shopData", shop_data); 

         Intent myIntent = new Intent(SearchDB.this, ShowRestaurant.class); 
         myIntent.putExtras(bundle); 
         startActivityForResult(myIntent, 0); 
      } 


    } 
     } 

    //ONCLICKLISTENER that saves RADIO value into a variable. 

    public OnClickListener radio_listener = new OnClickListener() { 
     public void onClick(View v) { 
      // Perform action on clicks 

      RadioButton rb = (RadioButton) v; 
      Day_Item = (String) rb.getText(); 
      Log.d(TAG,Day_Item); 
      Toast.makeText(SearchDB.this, Day_Item, Toast.LENGTH_SHORT).show(); 
     } 
    }; 
} 
+0

「later」對你來說意味着什麼? (Swicthing的活動例如?) – Blackbelt

+0

同樣的活動。因此用戶必須首先選擇一個無線電選項,然後在選擇了微調器選項後,該變量稍後將用於同一活動。這兩個變量都用在URL中。但是當我登錄url字符串而不是變量中的值時,它只是說變量名。 – Denoteone

回答

1

你需要更多的代碼才能得到一個很好的答案。如何分配Day_Item?它的範圍是全球性的嗎?你是從其他活動還是從其他活動中調用它?這些只是猜測在這一點上:

1)你確定你的onClickListener不是多次發射?因此,將Day_Item設置爲不想要的文本還是一無所有?

2),而一個問題/答案,

「但使用Day_Item嘗試時後,它不會顯示有價值的」

我假設這意味着它是空?那麼如果它被正確設置,然後它被空置......它要麼被某個地方(如(1))明確地空置,要麼分配和範圍是我認爲的問題區域。

+0

我上面更新了我的代碼。該變量將在相同的活動中讀取。因此用戶必須首先選擇一個無線電選項,然後在選擇了微調器選項後,該變量稍後將用於同一活動。這兩個變量都用在URL中。但是當我登錄url字符串而不是變量中的值時,它只是說變量名。 – Denoteone

+0

我明白了。感謝您的反饋讓我回到了代碼中,發現我的錯誤。經過! – Denoteone

+0

很高興聽到它! –