3

Would anyone be kind enough to help me? I am making an android app that uses toggle buttons to collect user answers in the form of yes and no (on and off respectively). I have set up the following branch in the first buttons on click listener method:-

If the toggle is clicked, Use an already declared and initialised local variable to store a number (eg, 1) Else Use the already declared and initialised variable to store a different number (eg, 2)

Well. I realise I cannot use local variables in another method, however I want to collect the variables from all the toggle buttons so that I can calculate a user score somewhere else in the program. How would I do this?

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

public class NewActivity1 extends Activity{

public static int exportNumber1 = 0;
public static int exportNumber2 = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_activity1);

    final TextView textView = (TextView) findViewById(R.id.textView11);
    textView.setText("" + exportNumber1);

    final ToggleButton atb1 = (ToggleButton) findViewById(R.id.toggleButton1);
    atb1.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {

        int x = 0;

            if (atb1.isChecked())
            {
            x = 1;
            }
            else
            {
            x = 2;
            }
            exportNumber1 = x;
        }
    });}}

1 Answers1

2

You should declare those variables globally (after your Activity/class declaration.)

This way you will be able to access the from any method you desire within that activity.

ex:

     public class MyActivity extends Activity {
          public int toggle1, toggle2, toggle3....   
//or String or whaterever, you can use these variables to store the values the user selects from within your OnClick listener
JanBo
  • 2,925
  • 3
  • 23
  • 32
  • Firstly thank you for replying everyone, where do I paste code on here? :) – Johnny Biscuits Jan 19 '14 at 18:23
  • Well you cant copy/paste it :) type it yourself since you know how do you want your variables to be called ;) ...since i see you are having some difficulties maybe taka a look at this first: http://stackoverflow.com/questions/4646577/global-variables-in-java and this http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html and this http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – JanBo Jan 19 '14 at 18:30
  • Anyway, while I figure that out, I have declared two global variables above the onCreate method already. What i'm trying to do is pass (unsucessfully!) the result of the local variable from inside the onClick method to the global variable by using exportNumber2 = y; (after processing y). I hoped that this would then export y into the global variable, exportNumber2. This appears not to work however as when I check the value of exportNumber2, is it as it was when initialised (0). – Johnny Biscuits Jan 19 '14 at 18:31
  • Don't know how to post code in that neat format, but above is a snippet, any help? – Johnny Biscuits Jan 19 '14 at 18:45
  • Yes that nice blue shaded background you used above JanBo. How do you do that? – Johnny Biscuits Jan 19 '14 at 18:48
  • You can edit your question and update it with code...it will suggest you how to format it so it looks neat...from what you posted there is an extra "}" but that would cause you compile problems and wouldt let you run the app...mu suggestion is to put a break point at that code and debug the app, then you will see exactly what is your issue, and that your view on click even triggers at all. – JanBo Jan 19 '14 at 18:49
  • The app runs, I have it connected directly to my android device. It just wont do what I want! lol yeah not seeing this shaded, neato option. – Johnny Biscuits Jan 19 '14 at 18:52
  • Like i said, debugging is your only option, you are doing something wrong and i cant help you any more then this unless i am sitting next to you ;). You made some obvious mistake just find it, because what you are trying to do is trivial and so is the mistake probably ;) – JanBo Jan 19 '14 at 18:54