Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6200585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:23:45+00:00 2026-05-24T04:23:45+00:00

So im sure this is probably a fairly easy question but I am stumped

  • 0

So im sure this is probably a fairly easy question but I am stumped because I am a beginner.

I am looking to pass a value from one class to another, and I have my helper function down and working just fine. If i create an integer outside of my onClick I can pass it no problem. If I create it inside the onClick though it doesn’t seem to make it out.

package com.movi.easypar;

//import java.util.logging.Handler;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class EntryScreen extends Activity implements OnClickListener {

Button buttonSetHoles, buttonSetPlayers, buttonLetsGo;
TextView textGameSetup;
public int setHoles; <--- declared here###############################
private String name1 = "Crista";
private String name2 = "Rob";
private String name3 = "Gato";
private String name4 = "Movi";



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


    //******************//
    //***DEFINE FONTS***//
    //******************//
    Typeface merge = Typeface.createFromAsset(getAssets(), "merge.otf");

    //*****************************************************//
    //***CREATES THE UI FOR EACH OF THESE IN JAVA MEMORY***//
    //*****************************************************//
    buttonSetHoles = (Button)findViewById(R.id.buttonSetHoles);
    buttonSetPlayers = (Button)findViewById(R.id.buttonSetPlayers);
    buttonLetsGo = (Button)findViewById(R.id.buttonLetsGo);
    textGameSetup = (TextView)findViewById(R.id.textGameSetup);
    buttonSetHoles.setTypeface(merge);
    buttonSetPlayers.setTypeface(merge);
    buttonLetsGo.setTypeface(merge);
    textGameSetup.setTypeface(merge);
    buttonSetHoles.setText("Set Holes");
    buttonLetsGo.setText("Lets Go");
    buttonSetPlayers.setText("Set Players");

    //******************************//
    //***DEFINES BUTTON LISTENERS***//
    //******************************//
    buttonSetHoles.setOnClickListener(this);
    buttonSetPlayers.setOnClickListener(this);
    buttonLetsGo.setOnClickListener(this);
}






//*************************************************//
//***SETS ON CLICK FUNCTIONALITY FOR ALL BUTTONS***//
//*************************************************//
@Override
public void onClick(View src) {

    switch(src.getId()){

    case R.id.buttonSetPlayers:
        break;

    case R.id.buttonSetHoles:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final CharSequence[] items = {"18", "9"};
        builder.setTitle("Set Holes");
        builder.setItems(items, new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                if (items[item].equals("9")){
                    EntryScreen.this.setHoles = 9; <---#### VALUE SET HERE ####

                }
                else if (items[item].equals("18")){
                    EntryScreen.this.setHoles = 18;

                }

                return;
            }
        });
        builder.create().show();

        return;

    case R.id.buttonLetsGo:
        //*********************************//
        //***LAUNCHES ACTUAL APPLICATION***//
        //*********************************//
        TranslateAnimation slide = new TranslateAnimation(0, -500, 0,0 );
        slide.setDuration(1000);   
        slide.setFillAfter(true);
        buttonLetsGo.startAnimation(slide);
        buttonSetPlayers.startAnimation(slide);
        buttonSetHoles.startAnimation(slide);
        Intent myIntent = new Intent(src.getContext(), EasyPar.class);
        startActivityForResult(myIntent, 0);
        break;
    }
    EntryScreen.this.finish();
}   


public String getNames() {
    return name1;
}

public void setNames(String playerName1) {
    name1 = playerName1;
}

public int getHoles() {
    return setHoles;  <---- #### THIS DOES NOT SEE VALUE SET IN ONCLICK ####
}
}

This helper does not seem to be able to see the setHoles value that is created onClick.

Any suggestions? Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:23:46+00:00Added an answer on May 24, 2026 at 4:23 am

    It’s a scope thing. A variable defined in a function has local scope, and will be destroyed when the function returns. You need a field to hold your value if you wish to retain it.

    [EDIT]
    Then allow me to elaborate. You can create a field by typing the following line outside a function, inside the class:

    [Access][Type][Name];

    ex:

    class foo{
        public int dice;
        public void onClick(){
             //now the dice's value is saved throught the lifecycle of the Activity
        }
    }
    

    [EDIT]
    I copied your code and ran it. (Modified just a little.)

    public class Main extends Activity implements OnClickListener {
    
    Button buttonSetHoles, buttonSetPlayers, buttonLetsGo;
    TextView textGameSetup;
    public int setHoles; //<--- declared here###############################
    private String name1 = "Crista";
    private String name2 = "Rob";
    private String name3 = "Gato";
    private String name4 = "Movi";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        //******************//
        //***DEFINE FONTS***//
        //******************//
        Typeface merge = Typeface.createFromAsset(getAssets(), "merge.otf");
    
        //*****************************************************//
        //***CREATES THE UI FOR EACH OF THESE IN JAVA MEMORY***//
        //*****************************************************//
        /*
    
        buttonSetPlayers = (Button)findViewById(R.id.buttonSetPlayers);
        buttonLetsGo = (Button)findViewById(R.id.buttonLetsGo);
        textGameSetup = (TextView)findViewById(R.id.textGameSetup);
        */
        buttonSetHoles = (Button) findViewById(R.id.buttonSetHoles);
        /*
        buttonSetHoles.setTypeface(merge);
        buttonSetPlayers.setTypeface(merge);
        buttonLetsGo.setTypeface(merge);
        textGameSetup.setTypeface(merge);
        buttonSetHoles.setText("Set Holes");
        buttonLetsGo.setText("Lets Go");
        buttonSetPlayers.setText("Set Players");
        */
    
        //******************************//
        //***DEFINES BUTTON LISTENERS***//
        //******************************//.
        buttonSetHoles.setOnClickListener(this);
        /*
        buttonSetPlayers.setOnClickListener(this);
        buttonLetsGo.setOnClickListener(this);
        */
    }
    
    //*************************************************//
    //***SETS ON CLICK FUNCTIONALITY FOR ALL BUTTONS***//
    //*************************************************//
    @Override
    public void onClick(View src) {
    
        switch (src.getId()) {
    
            case R.id.buttonSetHoles:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                final CharSequence[] items = { "18", "9" };
                builder.setTitle("Set Holes");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int item) {
                        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                        if (items[item].equals("9")) {
                            setHoles = 9;// <---#### VALUE SET HERE ####
                            Toast.makeText(Main.this, "getHoles()=" + getHoles(), Toast.LENGTH_SHORT);
                        }
                        else if (items[item].equals("18")) {
                            setHoles = 18;
                            Toast.makeText(Main.this, "getHoles()=" + getHoles(), Toast.LENGTH_SHORT);
                        }
    
                        return;
                    }
                });
                builder.create().show();
    
                return;
    
        }
        //finish();
    }
    
    public String getNames() {
        return name1;
    }
    
    public void setNames(String playerName1) {
        name1 = playerName1;
    }
    
    public int getHoles() {
        return setHoles;
    }
    }
    

    And it seems to work just fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a really newbie question (well, I'm pretty sure it is), but
This is probably really easy, but I'm lost on how to make sure it
I think the title for this question is probably wrong, but I'm not sure
I'm sure this is possible -- and probably very easy -- but I can't
I know this probably really simple but Im not sure what im doing wrong...
I know this is probably possible using Streams, but I wasn't sure the correct
I'm sure the static analyser is probably right in this case, but I don't
I'm sure there's a clean way to do this, but I'm probably not using
Pretty sure this question counts as blasphemy to most web 2.0 proponents, but I
Im sure this will be a simple one but have a project that started

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.