Java 获取/使用单选按钮值
我知道我在做什么。我将变量名与URL字符串的其余部分一起用引号括起来。 如何将单选按钮的值保存到变量中,并在以后使用该变量 我可以在我的LogCat中看到变量Java 获取/使用单选按钮值,java,android,radio-button,Java,Android,Radio Button,我知道我在做什么。我将变量名与URL字符串的其余部分一起用引号括起来。 如何将单选按钮的值保存到变量中,并在以后使用该变量 我可以在我的LogCat中看到变量Day\u Item,该值在其中,但稍后尝试使用Day\u Item时,它不会显示有价值的值 下面是我的代码中显示按钮的部分 String Day_Item = null; public class SearchDB extends Activity { private static final String TAG
Day\u Item
,该值在其中,但稍后尝试使用Day\u 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();
}
};
}
String Day\u Item=null;
公共类SearchDB扩展活动{
私有静态最终字符串TAG=“MyApp”;
字符串start\u log=“start”;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.search\u布局);
最终单选按钮周一=(单选按钮)findViewById(R.id.monday);
radio_monday.setOnClickListener(radio_listener);
cityspinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长arg3)
{
int id=parent.getId();
if(喷丝头2_计数2<喷丝头2_计数1){
喷丝头2_count2++;}
其他的
{
字符串city\u spinner\u log=“city spinner”;
Log.d(标签、城市微调器和日志);
String item=cityspinner.getSelectedItem().toString();
字符串nameContentType=“name”;
String cityURL=“GetRestaurant.php?day=day\u Item&city=“+Item;
日志d(标签,城市URL);
字符串shop_data=DataCall.getJSON(cityURL,nameContentType);
日志d(标签、车间数据);
Bundle=新Bundle();
bundle.putString(“shopData”,shop_数据);
Intent myIntent=新Intent(SearchDB.this,ShowRestaurant.class);
myIntent.putExtras(bundle);
startActivityForResult(myIntent,0);
}
}
}
//ONCLICKLISTENER,将单选值保存到变量中。
public OnClickListener radio\u listener=new OnClickListener(){
公共void onClick(视图v){
//对单击执行操作
RadioButton rb=(RadioButton)v;
Day_Item=(字符串)rb.getText();
日志d(标签、日期项目);
Toast.makeText(SearchDB.this,Day_Item,Toast.LENGTH_SHORT).show();
}
};
}
您需要更多的代码才能得到一个可靠的答案。例如,Day_项目是如何分配的?它的范围是全局的吗?您是从另一个活动中调用它还是从它所分配的活动中调用它?目前这些只是猜测:
1) 你确定你的onClickListener没有多次触发吗?因此将Day_项设置为不需要的文本或什么都没有
2) 而是一个问题/答案
“但稍后尝试使用Day_项目时,它不会显示有价值的内容”
我假设这意味着它是空的?如果它被正确设置,然后它是空的…它或者是在某个地方被你显式地空的(比如(1)),或者分配和范围是我认为的问题区域…什么是“以后”对你意味着什么?(例如开关活动?)相同的活动。因此,用户必须首先选择一个单选选项,该变量在选择微调器选项后将在相同的活动中使用。然后,这两个变量都将用于URL。但是,当我对URL字符串(而不是变量中的值)进行logcat时,它只会显示变量名称。我会更新上面的代码。变量将被读入相同的活动。因此,用户必须先选择一个单选选项,然后在选择微调器选项后,在相同的活动中使用该变量。这两个变量随后都用于URL。但是,当我对URL字符串而不是变量中的值进行日志记录时,它只会显示变量名称。我已经计算出来了。感谢您的反馈让我重新检查我的代码,我发现了我的错误。检查!