Reputation: 19
Hello I want to pass value "valuey" from method veikia() to method checkButtonClick(). I`m newby on Java so sorry if this question is already answered, because I do not understand:)
Have a nice day :)
Here is my code:
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();
}
});
}
}
Upvotes: 0
Views: 3901
Reputation: 3538
Change the signature of your checkButtonClick
method from:
public void checkButtonClick(){...}
to:
public void checkButtonClick(String myString) {
// Use the value of myString here
}
And then on your onCreate
method, instead of calling:
checkButtonClick();
veikia();
Call the functions in reverse order, like:
String valuey = veikia();
checkButtonClick(valuey)
EDIT:
In order to have access to valuey
from inside a method you're overriding, like OnClickListener.onClick(View v)
you will have to define valuey
as a member variable. The final result would look something like this:
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);
}
}
Upvotes: 1
Reputation: 5470
Just pass the return value of veikia()
to checkButtonClick()
when you call it:
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());
}
Upvotes: 1