-4

So I have this class where I push a button, and at the and I get the int maxpluseen which I want to pass over to ClassB.

   private void populateBtn() {
        foto1 = (Button) this.findViewById(R.id.button2);
        foto1.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SimpleDateFormat")
            @Override
            public void onClick(View v) {
 int maxpluseen = maxIndex + 1;
                Log.d("LOG!", "Uiteindelijk resultaat " + maxpluseen);

                Intent myIntent = new Intent(classA.this, classB.class);
                classA.this.startActivity(myIntent);     

            }


        });
    }

I tried doing the following:

public static int Access(int maxpluseen){
  return maxpluseen; 
}

And In classB

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.info);

     int maxpluseen = 0;
    int test = classA.Access(maxpluseen);
    Log.d("LOG!", "Test int tussen klassen= " + test);

}

But it gives back 0 (so my int isn't passed from classA to classB. What am I doing wrong here?

user1393500
  • 199
  • 1
  • 3
  • 16
  • http://stackoverflow.com/questions/15859445/how-do-you-pass-a-string-from-one-activity-to-another/15859488#15859488 – Raghunandan May 21 '13 at 14:19
  • dup here: http://stackoverflow.com/questions/5467246/transferring-int-variable-between-activities-android/5467268#5467268 – TronicZomB May 21 '13 at 14:19
  • I do not understand your logic. You pass a integer to your Access method, and it simply returns the same int to you. In which way you are expectining a "modified" value? – Blackbelt May 21 '13 at 14:22
  • A little tip for future programming. Try to code in English, also the Strings and messages. So people know what you mean if they need it. I made mistake once giving variable a dutch name, had to convert them to english before posting on SO (more work for nothing). – Bigflow May 21 '13 at 14:22
  • since Access does nothing, dont try it that way for sure. Every intent can carry some informations, so you can put an int in intent that starts class B, like myIntent.putExtra(key,value); – Marko Niciforovic May 21 '13 at 14:23

4 Answers4

0

here you are passing ClassA.Access() a value of 0, thus returning a value of 0.

 int maxpluseen = 0;
 int test = classA.Access(maxpluseen);
Caveman42
  • 679
  • 2
  • 13
  • 35
0

in class A:

Intent myIntent = new Intent(classA.this, classB.class);
myIntent.putExtra("number", maxpluseen)
            classA.this.startActivity(myIntent); 

in Class B:

int getal;
getal = getIntent().getExtras().getInt("number");

......

Phil3992
  • 1,059
  • 6
  • 21
  • 45
0

First of all, you should mention that your Classes aren't normal classes, but Activities. Secondly, you should really use all-English variable names (maxplusone).

You can add data to the Intent, you use to start the new Activity with, which you can read more about

Here in a different question or here in the Intent developer description

Community
  • 1
  • 1
Stefan de Bruijn
  • 6,289
  • 2
  • 23
  • 31
0

Is classA your main?

If so, create an object to classB. Write a function to set the int value, so that you can do something like classBObject.setInt(value). Then, in your foto1's onClick() method, call that function.

Steven_BDawg
  • 796
  • 6
  • 21