[英]taking variable from another class
I have to spinners, and when I start my app, the PHP only returns values of spinner's first choice. 我必须使用微调器,并且当我启动应用程序时,PHP仅返回微调器的首选值。 First code is part of one class (IzboraGrada.java) 第一个代码是一个类的一部分(IzboraGrada.java)
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
str_grad=spinner1.getSelectedItem().toString();
str_predmet=spinner2.getSelectedItem().toString();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
And the second code is part of MainActivity.class that was in intent. 第二个代码是意图的MainActivity.class的一部分。
private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
String url = "http://192.168.1.102/test/spinner.php";
Bundle extras = getIntent().getExtras();
if (extras != null) {
url = extras.getString("URL");
}
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
I presume that's because str_grad and str_predmet are not defined in second class. 我认为这是因为str_grad和str_predmet没有在第二类中定义。 But If I put str_grad and str_predmet in second class, they are can't be resolved as type.Any ideas what to do? 但是,如果我将str_grad和str_predmet放在第二类中,则无法将它们解析为类型。任何想法怎么办?
It looks like you are calling this method at the beginning, before an item is selected, so I think the problem is that you are setting the values for str_grad
and str_predmet
when they are first set so the selected item is the default item. 看来您是在开始选择某个项目之前调用此方法的,所以我认为问题在于您是在初次设置str_grad
和str_predmet
的值时将它们设置为默认值的。 Those are getter functions not listeners. 这些是getter函数,而不是侦听器。
You need to move those lines inside the onClick()
or use onItemSelected()
on your Spinners
to set those variable values 您需要将这些行移动到onClick()
或在Spinners
上使用onItemSelected()
来设置这些变量值
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
str_grad=spinner1.getSelectedItem().toString(); // move these lines here
str_predmet=spinner2.getSelectedItem().toString();
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
If I understand your problem correctly, that should solve your problem. 如果我正确理解您的问题,那应该可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.