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 8132061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:15:17+00:00 2026-06-06T09:15:17+00:00

I am very new to Android development and Java. Have read around but I’m

  • 0

I am very new to Android development and Java. Have read around but I’m not getting anywhere on this issue.

I have a button which when clicked should set a variable A’s Value to “Item Purchased”.
However, I am only get the value used when the variable is first defined in the class.

For those learning like me on this – this topic will hopefully make an excellent reference to those just starting.

Code is:

public class shopView extends Activity
{

    String temp = "temp";

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shopview);


        Button btnRef1 = (Button) findViewById(R.id.btnbtnRef11);
        final TextView ConfirmPurchasetest = (TextView) findViewById(R.id.tvMigName);

        btnRef1.setOnClickListener(new View.OnClickListener()
        {   
            @Override
            public void onClick(View v) {
                temp = "passed value";
                ConfirmPurchasetest.setText("item Purchased");
                buyFromShop();
                Log.v("after button push", "temp");
                };
        });

    }
    public String buyFromShop(){
        Log.v("button push", "after buy from shop");
        Log.v("temp variable",temp);
        return temp;
    }

}

and is called using the following:

shopcheckout = shop.buyFromShop();

Log.v("Value in myView",shopcheckout);

Expected: shopcheckout = “item purchased”
Actual: shopcheckout = “temp”

Thanks again for any answers. Will actively monitor this post.

  • 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-06-06T09:15:18+00:00Added an answer on June 6, 2026 at 9:15 am

    Unless you click on your Button btnRef1, buyFromShop() will always return “temp”.

    If you add this code in your onCreate():

    temp = "Changing the string.";
    

    Now buyFromShop() will return “Changing the string.”.

    If you want buyFromShop() to return the value of ConfirmPurchasedtest, change your code to this:

    public class shopView extends Activity
    {
        TextView confirmPurchaseTest;
        String temp = "temp";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.shopview);
    
            Button btnRef1 = (Button) findViewById(R.id.btnbtnRef11);
            confirmPurchaseTest = (TextView) findViewById(R.id.tvMigName);
    
            btnRef1.setOnClickListener(new View.OnClickListener()
            {   
                @Override
                public void onClick(View v) {
                    temp = "passed value";
                    confirmPurchaseTest.setText("item Purchased");
                    buyFromShop();
                    Log.v("after button push", "temp");
                }
            });
        }
    
        public String buyFromShop(){
            Log.v("button push", "after buy from shop");
            Log.v("temp variable", temp);
    
            // Change this!
            return confirmPurchaseTest.getText().toString();
        }
    
    }
    

    Also according to naming convention, class names like shopView should have the first letter of each word capitalized; so ShopView. Variables like ConfirmPurchasetest should start with lower case and then capitalize each word (after the first), so confirmPurchaseTest.

    Hope that helps!

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

Sidebar

Related Questions

I am very new to Android development. I have this code: Button btnLaunch; btnLaunch=(Button)findViewById(R.id.btnLaunch);
Doing Android development on a Mac and this very new phone I have doesn't
I'm very new on Android development. I have this: AlertDialog.Builder builder = new AlertDialog.Builder(this);
i m new to this android application development i would be very much grateful
Im very new to this android development.I just started to create a hello world
I am very new to android development and have been trying to draw a
ok so im very new to android development and fairly new to java and
I am new to android development but in the past few weeks I have
I am very new in android Development. I am getting a warning when i
I am very new to android development.... i have created an app and want

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.