您好我想将方法veikia()中的值“valuey”传递给方法 checkButtonClick()。我是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();
}
});
}
}
答案 0 :(得分:1)
调用它时,只需将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());
}
答案 1 :(得分:0)
更改checkButtonClick
方法的签名:
public void checkButtonClick(){...}
为:
public void checkButtonClick(String myString) {
// Use the value of myString here
}
然后使用onCreate
方法,而不是调用:
checkButtonClick();
veikia();
以相反的顺序调用函数,例如:
String valuey = veikia();
checkButtonClick(valuey)
修改强>
要从您覆盖的方法内部访问valuey
,例如OnClickListener.onClick(View v)
,您必须将valuey
定义为成员变量。最终结果看起来像这样:
public class ListViewCheckboxesActivity extends Activity implements View.OnClickListener {
private String valuey;
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
valuey = veikia();
}
@Override
public void onClick(View view) {
// do something with valuey, for example print it to the logger
Log.d("TAG", valuey);
}
}