Reputation: 507
I have a global parameter defined in my MyApp
class which is used to reset some values in all activities and I want to set some default values for some checkBoxes
and SeekBars
but I am not able to call function setDefaultvarValue
in setOnclickListner()
.
Here is code for myApp.class
:
class MyApp extends Application {
private int setDefault=0;
public int getDeafultVarValue() {
return setDefault;
}
public void setDefaultVarValue(int val) {
setDefault = val;
}
}
and setonClickListner
looks like this :
Button reset_btn = (Button) rootView.findViewById(R.id.reset_button);
reset_btn.setOnClickListener(new OnClickListener() {
((MyApp) this.getApplication()).setDefaultVarValue(1);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Upvotes: 4
Views: 3024
Reputation: 21
I have found the solution. getter setter method helped me to declare my r variable and I'm able to control my variable within every function. Solution, even tho code looks ugly.
private TextView broj;
private TextView tekst1;
private TextView posljednjeVrijeme;
String stringTStamp;
private int r;
public int getDefaultR(){
return r;
}
public void setDefaultR(int r){
this.r=r;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* Ponavlja radnju svakih 4 sekunde, učitava vrijeme systema
*/
final Handler ha=new Handler();
ha.postDelayed(new Runnable() {
@Override
public void run() {
Long tStamp = System.currentTimeMillis()/1000;
stringTStamp = tStamp.toString();
Toast.makeText(MainActivity.this, stringTStamp, Toast.LENGTH_SHORT).show();
SharedPreferences preferences3 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences3.edit();
editor.putString("posljednjeVrijeme", stringTStamp);
editor.apply();
ha.postDelayed(this, 4000);
}
}, 4000);
/**
* **************************************************************
*/
broj = (TextView)findViewById(R.id.broj);
posljednjeVrijeme = (TextView)findViewById(R.id.posljednjeVrijeme);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final String name = preferences.getString("vrijednost je ", "");
final String name2 = preferences.getString("posljednjeVrijeme", "");
if(!name.equalsIgnoreCase(""))
{
broj.setText("vrijednost je "+name);
posljednjeVrijeme.setText("Zadnje vrijeme "+name2);
}
Long tStamp = System.currentTimeMillis()/1000;
stringTStamp = tStamp.toString();
if(Integer.parseInt(stringTStamp)-Integer.parseInt(name2)<10){
r = 0+Integer.parseInt(name);
broj.setText("vrijednost je "+r);
}
else if(Integer.parseInt(stringTStamp)-Integer.parseInt(name2)>10){
Toast.makeText(this, "POZDRAV GAZDA DUGO SE NE VIDJEH, UGASIO SI ME PRIJE " + (Integer.parseInt(stringTStamp)-Integer.parseInt(name2))+ " SEKUNDI" , Toast.LENGTH_SHORT).show();
r = 0+Integer.parseInt(name)-10;
broj.setText("vrijednost je "+r);
}else{}
tekst1 = (TextView)findViewById(R.id.tekst1);
final SharedPreferences preferences2 = PreferenceManager.getDefaultSharedPreferences(this);
tekst1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
r = r+1;
broj.setText("vrijednost je "+r);
String s= String.valueOf(r);
SharedPreferences.Editor editor = preferences2.edit();
editor.putString("vrijednost je ", s);
editor.apply();
}
});
}
}
Upvotes: 0
Reputation: 699
Either try this: ((Activity) mContext).getApplication(...)
or make setDefaultVarValue method static It will solve your problem.
Upvotes: 0
Reputation: 661
Move your setDefault method into the onClick method. Then everything should work fine.
Upvotes: 0
Reputation: 1371
please change your code to this.
Button reset_btn = (Button) rootView.findViewById(R.id.reset_button);
reset_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((MyApp)YourActivityname.this.getApplicationContext()).setDefaultVarValue(1);
}
});
If you are calling from the activity. Replace YourActivityname with your Acitivity Name. If you have any query please let me know.
Upvotes: 1
Reputation: 344
When you setDefaultVarValue
in setOnClickListener
method,
setDefaultVarValue
is only called once in setOnClickListener
If you want setDefaultVarValue
called when button clicked, you should move it inside onClick
method
Upvotes: 1
Reputation: 132982
Call setDefaultVarValue
method as from MyApp
class :
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
MyApp objMyApp = (MyApp)v.getContext().getApplicationContext();
objMyApp.setDefaultVarValue(1);
}
Upvotes: 3
Reputation: 16043
Move this line ((MyApp) this.getApplication()).setDefaultVarValue(1);
inside onClick()
So it should be:
reset_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((MyApp) this.getApplication()).setDefaultVarValue(1);
}
});
Upvotes: 0