How can I use my local variable as a global?

That's my code. In the onClick method I wanna refresh the textView. So every time I click the button, the method calls int value = SecondFragment.getSmashValue();. From there I get the value I set in another Fragment. I have to use this variable now in my TextView I set in my onCreate method. But it's always showing me the value 0! I think it's showing 0 because I try to make the variable global and the global value is 0.

So how can I make this variable global without losing its value?

import com.example.viewpagertest2.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class Tab1Activity extends Fragment {

Button b1;
static int theValue;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.firsttabview, container, false);
    b1 = (Button) rootView.findViewById(R.id.refreshButton1);
    b1.setOnClickListener(handler);

    TextView tv = (TextView) rootView.findViewById(R.id.SP);
    tv.setText("" + theValue);

   b99.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            theValue = SecondFragment.getSmashValue();
            tv.setText("" + theValue);

        }
    });


    return rootView;


}

Answers


Why r u declaring an local variable no need of it to get the value.Use global variable only.you can do this :

        if(b1==v){
          theValue = SecondFragment.getSmashValue(); //use theValue wherever u  wanted to use


    }

You're assigning the uninitialized theValue to your local value variable. Flip that statement over.

int value = SecondFragment.getSmashValue();
theValue = value;  // was value = theValue;

Also, make tv a field, so you can update your TextView from your event handler.

tv.setText("" + theValue);

Need Your Help

IOC on IValidationDictionary with Castle Windsor

asp.net-mvc inversion-of-control castle-windsor moq modelstate

I'm new to Castle Windsor and am just using the latest version. I've created entries for my repositories which are working fine but I have one final dependency that I'm passing into my controller.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.