获取/使用单选按钮值android

时间:2011-09-25 07:44:06

标签: java android radio-button

我想出了我做了什么。我使用URL STRING的其余部分在引号中有变量名称。

如何将单选按钮的值保存到变量中,稍后再使用该变量。

我可以在我的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();
        }
    };
}

1 个答案:

答案 0 :(得分:1)

您需要更多代码才能获得良好的答案。比如Day_Item是如何分配的?它的范围是全球性的吗?您是从其他活动或其分配的活动中调用它吗?这些只是猜测:

1)你确定你的onClickListener没有多次触发吗?因此将Day_Item设置为不需要的文本或根本没有设置?

2)而不是问题/答案,

  

“但是当稍后尝试使用Day_Item时,它不显示有价值的”

我假设这意味着它是空的?好吧,如果它被正确设置,然后它被空了......它要么被某个地方显式为空(例如(1)),要么分配和范围是我认为的问题区域。 。

相关问题