gpt4 book ai didi

java - 如何将值从一个函数传递到另一个函数

转载 作者:行者123 更新时间:2023-12-02 02:58:32 25 4
gpt4 key购买 nike

您好,我想将值“valuey”从方法 veikia() 传递到方法检查按钮点击()。我是 Java 新手,如果这个问题已经得到解答,我很抱歉,因为我不明白:)

祝你有美好的一天:)

这是我的代码:

public class ListViewCheckboxesActivity extends Activity {

MyCustomAdapter dataAdapter = null;
PrekesDBController controller = new PrekesDBController(this);

//private Context mContext;
ScrollView sv;
TextView infotext;
CheckBox check;
LinearLayout ll;
Spinner spinner2;


@Override
public void onCreate(Bundle savedInstanceState) {
// mContext = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.main123);

// mContext = this;


//Generate list View from ArrayList
displayListView();

checkButtonClick();

veikia();

}


public String veikia() {
ArrayList<HashMap<String, String>> data2 = controller.getpavadinimas();
String valuey = "";
for (int kl = 0; kl < data2.size(); kl++) {
spinner2 = (Spinner) findViewById(R.id.spinner11);

ScrollView sv = (ScrollView) findViewById(R.id.myscroll123);
LinearLayout ll = (LinearLayout) findViewById(R.id.mylinearLayout123);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.naujasuzsakymailist123, null);


ll.addView(view);

//sikna = int kl;
Spinner jopapa = new Spinner(this);
jopapa = (Spinner) findViewById(R.id.spinner11);
jopapa.setId(kl);

List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
list.add("6");
list.add("7");
list.add("8");
list.add("9");
list.add("10");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
jopapa.setAdapter(dataAdapter);
valuey = String.valueOf(jopapa.getSelectedItem());
System.out.println(valuey);
}


return valuey;
}

public void checkButtonClick() {


Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");

ArrayList<Country> countryList = dataAdapter.countryList;
for(int i=0;i<countryList.size();i++){
Country country = countryList.get(i);
if(country.isSelected()){
responseText.append("\n" + country.getName());
}
}

Toast.makeText(getApplicationContext(),
responseText, Toast.LENGTH_LONG).show();

}
});

}

}

最佳答案

调用时只需将 veikia() 的返回值传递给 checkButtonClick() 即可:

public String veikia() {
...
return valuey;
}

// Added parameter valuey
public void checkButtonClick(String valuey) {
...
}

@Override
public void onCreate(Bundle savedInstanceState) {
...
// Call checkButtonClick() with the parameter valuey returned from veikia()
checkButtonClick(veikia());
}

关于java - 如何将值从一个函数传递到另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42721910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com