I'm trying to Save an editText to Sharedprefrences so that it displays in the TextView amnt. It's displaying it but not saving it? what am I doing wrong? I am new to this, just a little weekend hobby so I'm sorry if this is a stupid question, thanks for your time.
package org.iimed.www;
import java.util.Calendar;
import org.iimed.www.DateTimePicker.DateWatcher;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.inputmethodservice.Keyboard.Key;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class CaAdd extends Activity implements DateWatcher,OnClickListener{
Button button_click,amount;
int amountt = 0;
Key at;
EditText amountedit;
String amountText;
String amountString;
TextView dateResult, date2,amnt;
int hrs,min;
SharedPreferences.Editor editor ;
SharedPreferences prefs;
String amntprefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datetimeactivity);
dateResult = (TextView)findViewById(R.id.textview1);
date2 = (TextView)findViewById(R.id.textView2);
button_click = (Button)findViewById(R.id.setbtn);
button_click.setOnClickListener(this);
amnt = (TextView)findViewById(R.id.amountText);
}
public final String AT = "Amount";
public void loadDefaultSharedPreferences() {
prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (prefs != null) {
String loadUsername= prefs.getString(
AT , null);
if (loadUsername != null && !loadUsername.isEmpty()) {
amnt.setText(loadUsername);
}}}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.setbtn:
final Dialog mDateTimeDialog = new Dialog(this);
// Inflate the root layout
final RelativeLayout mDateTimeDialogView = (RelativeLayout) getLayoutInflater().inflate(R.layout.date_time_dialog, null);
// Grab widget instance
final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView.findViewById(R.id.DateTimePicker);
mDateTimePicker.setDateChangedListener(this);
mDateTimePicker.initData();
amountedit = (EditText)mDateTimeDialogView.findViewById(R.id.amountedit);
ImageButton increase = (ImageButton)mDateTimeDialogView.findViewById(R.id.increase);
increase.setOnClickListener(new OnClickListener(){
public void onClick(View v){
amountt++;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
amnt.setText(amountText);
}
});
ImageButton decrease = (ImageButton)mDateTimeDialogView.findViewById(R.id.decrease);
decrease.setOnClickListener(new OnClickListener(){
public void onClick(View v){
amountt--;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
amnt.setText(amountText);
}
});
Button setDateBtn = (Button)mDateTimeDialogView.findViewById(R.id.SetDateTime);
// Update demo TextViews when the "OK" button is clicked
setDateBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(CaAdd.this,AlarmReceiver.class);
PendingIntent pi=PendingIntent.getActivity(CaAdd.this, 2, intent,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alm=(AlarmManager) getSystemService(Context.ALARM_SERVICE);
hrs=mDateTimePicker.getHour();
min=mDateTimePicker.getMinute();
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,hrs);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
alm.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), pi);
mDateTimePicker.clearFocus();
String result_string = mDateTimePicker.getMonth() + "/" + String.valueOf(mDateTimePicker.getDay()) + "/" + String.valueOf(mDateTimePicker.getYear())
+ " " + String.valueOf(mDateTimePicker.getHour()) + ":" + String.valueOf(mDateTimePicker.getMinute());
if(mDateTimePicker.getHour() > 12) result_string = result_string + "PM";
else result_string = result_string + "AM";
date2.setText(result_string);
mDateTimeDialog.dismiss();
}
});
Button cancelBtn = (Button)mDateTimeDialogView.findViewById(R.id.CancelDialog);
// Cancel the dialog when the "Cancel" button is clicked
cancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.reset();
mDateTimeDialog.cancel();
}
});
// Reset Date and Time pickers when the "Reset" button is clicked
Button resetBtn = (Button)mDateTimeDialogView.findViewById(R.id.ResetDateTime);
resetBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.reset();
}
});
mDateTimeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Set the dialog content view
mDateTimeDialog.setContentView(mDateTimeDialogView);
//Display the dialog
mDateTimeDialog.show();
}}
public void onDateChanged(Calendar c) {
Log.e("","" + c.get(Calendar.MONTH) + " " + c.get(Calendar.DAY_OF_MONTH)+ " " + c.get(Calendar.YEAR));
String result_string =String.valueOf(c.get(Calendar.MONTH)+1) + "/" + String.valueOf(c.get(Calendar.DAY_OF_MONTH)) + "/" + String.valueOf(c.get(Calendar.YEAR));
// _tvUserDOB.setTypeface(CGlobalVariables.tfFoEnglish);
dateResult.setText(result_string+" (mm/dd/yyyy)");
}
public void saveDefaultPreferences() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);{
SharedPreferences.Editor editor = prefs.edit();
final String AT = "Amount";
editor.putString(AT,amountText);
editor.commit();}
}}
Shared preferences allows you to save and retrieve key, value pair data.
Initialization:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
// 0 - for private mode
Editor editor = pref.edit();
Storing Data:
editor.putString("key_name", "string value"); // Storing string
editor.commit(); // commit changes
Retrieving Data:
pref.getString("key_name", null); // getting String
To know more about SharedPreferences or to try out a simple example, check this
You can only save key-value pairs for some of the primitives and String (String set also) only. Android views can never be saved anywhere.
SharedPreferences manager =PreferenceManager.getDefaultSharedPreferences(context);
Use it for saving and retrieving data as follows
// for saving data
manager.edit().putString(context.getString(AT), YOUR_TEXT).commit()
// for retrieving data
manager.getString(context.getString(AT), "");
Related
I created a fragment which is basically a counter. When pressed, it updates the number of water glasses you've drunk. This data is stored in SharedPreferences. I also update this number once a day as well.
So i inserted fragment in xml of two activities: Main and the Timer.
It's perfectly work on the Main, when i start the TimerActivity it's also work, but when i go back to Main from Timer i see the last number i've reached in MainActivity, it's not updating and ignore my clicks from TimerActivity.
I think the trouble in "this.getActivity", but i don't know how to fix it. Thanks
Fragment code:
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import com.example.fitapp.R;
import java.util.Calendar;
public class WaterBottleFragment extends Fragment {
LinearLayout waterBottle;
TextView iconName;
SharedPreferences sPref;
int counter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_water_bottle, container, false);
// Update once a day
Calendar calendar = Calendar.getInstance();
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
sPref = this.getActivity().getSharedPreferences("startApp", Context.MODE_PRIVATE);
int lastDay = sPref.getInt("day", 0);
if(lastDay != currentDay){
SharedPreferences.Editor ed = sPref.edit();
ed.putInt("day", currentDay);
ed.commit();
counter = 0;
} else {
counter = (int) loadText();
}
waterBottle = (LinearLayout) view.findViewById(R.id.ll_water_bottle);
iconName = (TextView) view.findViewById(R.id.tv_icon_water);
iconName.setText(counter + " glasses");
waterBottle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
iconName.setText(counter + " glasses");
saveText();
}
});
return view;
}
// Save number of glasses
private void saveText() {
sPref = this.getActivity().getSharedPreferences("water_counter", Context.MODE_PRIVATE);
SharedPreferences.Editor ed = sPref.edit();
ed.putInt("num_of_glasses", counter);
ed.commit();
Toast.makeText(this.getActivity(), "updated", Toast.LENGTH_SHORT).show();
}
// Load number of glasses
private int loadText() {
sPref = this.getActivity().getSharedPreferences("water_counter", Context.MODE_PRIVATE);
int savedCounter = sPref.getInt("num_of_glasses", 2);
return savedCounter;
}
}
its not a big deal just create an interface like:
Public interface WaterBottleEventListener{
void onBottleClick();
}
create an object of your interface inside your fragment:
WaterBottleEventListener listener;
Then call it inside your waterBottle.setonclicklistener:
waterBottle.setonclicklistener(){
#override
onclick(View v){
listener.onbottleclick();
}
}
Then make both of your activities implement this interface and inside each implementation put your update glass code like this:
Activity1 implement waterBottleEventListener{
#override
onBottleclick(){
counter++;
IconName.setText(...)
...
}
}
I made a Clicker App (like cookie clicker). But when i Restart the App, the app saves the clicks, but only visible, so i mean when i click on the button, the clicks will be set to 0 again. So my question: Is my SharedPreference correct, or did i have done an mistake?
My complete MainActivity.java:
package com.rage.clicker;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import static com.rage.clicker.R.id.highscore_text;
import static com.rage.clicker.R.id.textView88;
public class MainActivity extends AppCompatActivity {
SoundPool mySound;
int playClick;
private final String TAG = this.getClass().getName();
ImageView hi;
TextView tv_clicks;
TextView ht;
ImageView b_click;
Button save;
int clicks;
SharedPreferences sf;
public static final String preference = "pref";
public static final String saveIt = "saveKey";
private TextView highScoreView;
private TextView currentScoreView;
private int currentScore;
private int highScore;
private String highScoreString;
private String currentScoreString;
private SharedPreferences msharedPreferences;
private static final String FILENAME = "PreferencesFilename";
private static final String VAL_KEY = "ValueKey";
private EditText editText;
private TextView TEV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Speichern laden
TEV = (TextView) findViewById(R.id.textView88);
SharedPreferences sharedPrefs = getSharedPreferences(FILENAME, 0);
TEV.setText(sharedPrefs.getString(VAL_KEY, ""));
//Speichern laden
msharedPreferences = getPreferences(MODE_PRIVATE);
mySound = new SoundPool(100, AudioManager.STREAM_MUSIC, 0);
playClick = mySound.load(this, R.raw.click, 1);
tv_clicks = (TextView) findViewById(textView88);
b_click = (ImageView) findViewById(R.id.imageView);
hi = (ImageView) findViewById(R.id.imageView);
b_click = (ImageView) findViewById(R.id.imageView);
ht = (TextView) findViewById(highscore_text);
final TextView TV = (TextView) findViewById(R.id.highscore_text);
currentScoreString = getString(R.string.current_score);
highScoreString = getString(R.string.high_score);
currentScore = clicks;
highScore = msharedPreferences.getInt(highScoreString, 0);
save = (Button) findViewById(R.id.button);
save.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast dumplings= Toast.makeText(MainActivity.this, "Adolf4", Toast.LENGTH_LONG);
dumplings.show();
}
});
b_click.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final ImageView zoom = (ImageView) findViewById(R.id.imageView);
final Animation zoomAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.size);
zoom.startAnimation(zoomAnimation);
mySound.play(playClick, 1, 1, 1, 0, 1);
clicks++;
tv_clicks.setText("Klicks: " + clicks);
ht.setText(currentScoreString + ": Test" + currentScore);
}
});
}
protected void init() {
currentScoreView.setText(currentScoreString + ": asd" + currentScore);
highScoreView.setText(highScoreString + ":34 " + highScore);
}
#Override
protected void onStop() {
super.onStop();
//Speichern
SharedPreferences sharedPrefs = getSharedPreferences(FILENAME, 0);
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString(VAL_KEY, TEV.getText().toString());
editor.commit();
//Speichern
}
}
When you save data, you take click's count value from TEV TextView, but inside onClick event you update tv_clicks and TEV always remains zero by default. You should save clicks value like this
editor.putInt(VAL_KEY, clicks);
Then you should load clicks value and set it in the TextView:
clicks = sharedPrefs.getInt(VAL_KEY, 0)
TEV.setText(String.valueOf(clicks));
I'm trying to set a second inflated layout in my activity, the one that works inflates to set an alarm from a timepicker, the second one inflates a number picker to set an amount. So far the second button will not respond or, based on another set of code (one shown) I get a runtime error. Can anyone tell me what I am doing wrong?
package org.iimed.www;
import java.util.Calendar;
import org.iimed.www.DateTimePicker.DateWatcher;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class CaAdd extends Activity implements OnClickListener {
Button button_click, amount;
TextView dateResult, date2;
int hrs, min;
int amountt = 0;
EditText amountedit;
String amountText;
Dialog amountdialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datetimeactivity);
dateResult = (TextView) findViewById(R.id.textview1);
date2 = (TextView) findViewById(R.id.textView2);
button_click = (Button) findViewById(R.id.setbtn);
amount = (Button) findViewById(R.id.amountbtn);
amount.setOnClickListener(this);
button_click.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.setbtn:
amountdialog = new Dialog(this);
final Dialog mDateTimeDialog = new Dialog(this);
// Inflate the root layout
final RelativeLayout mDateTimeDialogView = (RelativeLayout) getLayoutInflater()
.inflate(R.layout.date_time_dialog, null);
// Grab widget instance
final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView
.findViewById(R.id.DateTimePicker);
mDateTimePicker.initData();
Button setDateBtn = (Button) mDateTimeDialogView
.findViewById(R.id.SetDateTime);
// Update demo TextViews when the "OK" button is clicked
setDateBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(CaAdd.this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getActivity(CaAdd.this, 2,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
hrs = mDateTimePicker.getHour();
min = mDateTimePicker.getMinute();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hrs);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
alm.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pi);
mDateTimePicker.clearFocus();
String result_string = mDateTimePicker.getMonth() + "/"
+ String.valueOf(mDateTimePicker.getDay()) + "/"
+ String.valueOf(mDateTimePicker.getYear()) + " "
+ String.valueOf(mDateTimePicker.getHour()) + ":"
+ String.valueOf(mDateTimePicker.getMinute());
if (mDateTimePicker.getHour() > 12)
result_string = result_string + "PM";
else
result_string = result_string + "AM";
date2.setText(result_string);
mDateTimeDialog.dismiss();
}
});
Button cancelBtn = (Button) mDateTimeDialogView
.findViewById(R.id.CancelDialog);
// Cancel the dialog when the "Cancel" button is clicked
cancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.reset();
mDateTimeDialog.cancel();
}
});
// Reset Date and Time pickers when the "Reset" button is clicked
Button resetBtn = (Button) mDateTimeDialogView
.findViewById(R.id.ResetDateTime);
resetBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.reset();
}
});
Button amountbtn = (Button) findViewById(R.id.amountbtn);
amountbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Inflate the root layout
final RelativeLayout amountView = (RelativeLayout) getLayoutInflater()
.inflate(R.layout.amount, null);
// Grab widget instance
amountdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
amountdialog.setContentView(amountView);
amountdialog.show();
amountedit = (EditText) amountView
.findViewById(R.id.amount);
ImageButton increase = (ImageButton) amountView
.findViewById(R.id.increase);
increase.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
amountt++;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
}
});
ImageButton decrease = (ImageButton) amountView
.findViewById(R.id.decrease);
decrease.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
amountt--;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
}
});
amountdialog.dismiss();
// No title on the dialog window
mDateTimeDialog
.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set the dialog content view
mDateTimeDialog.setContentView(mDateTimeDialogView);
// Display the dialog
mDateTimeDialog.show();
}
});
}
}
}
I have a DateTimePicker with a little "amount" editext that I save the Sharedpref
but for some reason if I "touch" the EditText and enter the amount in manually, the text is black and doesn't transfer to the TextView, if using the imagebuttons increase and decrease the editText will work as intended. If I enter the dates and times in this manner though it does work, what am I missing?
ImageButton increase = (ImageButton)mDateTimeDialogView.findViewById(R.id.increase);
increase.setOnClickListener(new OnClickListener(){
public void onClick(View v){
amountt++;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
amnt.setText(amountText);
editor.putString("numbers", amountText);
editor.commit();
}
});
ImageButton decrease = (ImageButton)mDateTimeDialogView.findViewById(R.id.decrease);
decrease.setOnClickListener(new OnClickListener(){
public void onClick(View v){
amountt--;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
amnt.setText(amountText);
editor.putString("numbers", amountText);
editor.commit();
}
});
The relevant EditText info
amountedit=(EditText)mDateTimeDialogView.findViewById(R.id.amountedit);
The TextView it displays to
amnt = (TextView)findViewById(R.id.amountText);
The XML of EditText
<EditText
android:id="#+id/amountedit"
android:layout_width="50dp"
android:key = "amntkey"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="62dp"
android:ems="10"
android:inputType="number"
android:persistent="true" />
The complete java activity it takes place:
import java.util.Calendar;
import java.util.HashMap;
import org.iimed.www.DateTimePicker.DateWatcher;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.inputmethodservice.Keyboard.Key;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class CaAdd extends Activity implements DateWatcher,OnClickListener{
Button button_click;
int amountt = 0;
String amountText;
String number = "numbers";
TextView amnt;
TextView dateResult, date2;
int hrs,min;
SharedPreferences pref;
String time = "time";
String get;
String sTime;
String amount ;
EditText amountedit;
String result_string;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datetimeactivity);
dateResult = (TextView)findViewById(R.id.textview1);
date2 = (TextView)findViewById(R.id.textView2);
button_click = (Button)findViewById(R.id.setbtn);
pref = getSharedPreferences("prefs", Context.MODE_PRIVATE);
button_click.setOnClickListener(this);
amnt = (TextView)findViewById(R.id.amountText);
pref = getSharedPreferences("prefs", Context.MODE_PRIVATE);
amountText = Integer.toString(amountt);
amount = amnt.toString();
get = pref.getString("numbers",amountText);
sTime = pref.getString("time", result_string);
amnt.setText(get);
date2.setText(sTime) ;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.setbtn:
final Dialog mDateTimeDialog = new Dialog(this);
// Inflate the root layout
final RelativeLayout mDateTimeDialogView = (RelativeLayout) getLayoutInflater().inflate(R.layout.date_time_dialog, null);
// Grab widget instance
final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView.findViewById(R.id.DateTimePicker);
mDateTimePicker.setDateChangedListener(this);
mDateTimePicker.initData();
amountedit=(EditText)mDateTimeDialogView.findViewById(R.id.amountedit);
ImageButton increase = (ImageButton)mDateTimeDialogView.findViewById(R.id.increase);
increase.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Editor editor = pref.edit();
amountt++;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
amnt.setText(amountText);
String amount =amnt.toString();
editor.putString("numbers", amountText);
editor.commit();
}
});
ImageButton decrease = (ImageButton)mDateTimeDialogView.findViewById(R.id.decrease);
decrease.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Editor editor = pref.edit();
amountt--;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
amnt.setText(amountText);
String edi = amountedit.toString();
editor.putString("numbers", amountText);
editor.commit();
}
});
Button setDateBtn = (Button)mDateTimeDialogView.findViewById(R.id.SetDateTime);
setDateBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(CaAdd.this,AlarmReceiver.class);
PendingIntent pi=PendingIntent.getActivity(CaAdd.this, 2, intent,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alm=(AlarmManager) getSystemService(Context.ALARM_SERVICE);
Editor editor = pref.edit();
hrs=mDateTimePicker.getHour();
min=mDateTimePicker.getMinute();
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,hrs);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
alm.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), pi);
mDateTimePicker.clearFocus();
result_string =
String.valueOf(mDateTimePicker.getHour()) + ":" + String.valueOf(mDateTimePicker.getMinute());
if(mDateTimePicker.getHour() > 12) result_string = result_string + "PM";
else result_string = result_string + "AM";
date2.setText(result_string);
editor.putString("time",result_string);
editor.commit();
mDateTimeDialog.dismiss();
}
});
Button cancelBtn = (Button)mDateTimeDialogView.findViewById(R.id.CancelDialog);
cancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.reset();
mDateTimeDialog.cancel();
}
});
Button resetBtn = (Button)mDateTimeDialogView.findViewById(R.id.ResetDateTime);
resetBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.reset();
}
});
mDateTimeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDateTimeDialog.setContentView(mDateTimeDialogView);
mDateTimeDialog.show();}}
public void onDateChanged(Calendar c) {
Log.e("","" + c.get(Calendar.MONTH) + " " + c.get(Calendar.DAY_OF_MONTH)+ " " + c.get(Calendar.YEAR));
String result_string =String.valueOf(c.get(Calendar.MONTH)+1) + "/" + String.valueOf(c.get(Calendar.DAY_OF_MONTH)) + "/" + String.valueOf(c.get(Calendar.YEAR));
dateResult.setText(null);
}}
Your code isn't clear, you should post code about that EditText you're talking about.
BTW, to get what you would like, you can try to do Sharedpref's edit & commit
when your EditText's text changed, although it's a bad design...
You need a Text change listener
EditText.addTextChangedListener(new TextWatcher()
{
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{}
#Override
public void afterTextChanged(Editable s)
{}
});
I am trying to have it so when you click one of the answeers (Q1A1 or Q1A2) it will add a number of points to the testScore int so I can then later call that in a later class so the score they got would be posted there. Thanks to anyone who helps in advance!
here's my code,
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class Test extends Activity implements OnClickListener
{
TextView Q1A1;
TextView Q1A2;
TextView test;
public static final String PREFS_NAME = "MyPrefsFile";
public static final int testScore = 0;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Q1A1 = (TextView) findViewById(R.id.Q1A1);
Q1A2 = (TextView) findViewById(R.id.Q1A2);
Q1A1.setOnClickListener(this);
Q1A2.setOnClickListener(this);
test = (TextView) findViewById(R.id.test);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
test.setText(settings.getString("YourScore", "No Score"));
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.Q1A1:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("YourScore", (testScore + 10));
editor.commit();
//Intent FinalScore = new Intent(this, FinalScore.class);
//startActivity(FinalScore);
break;
case R.id.Q1A2:
break;
}
}
}
thanks for the help
You are are saving your score as an int but calling it as a string.
Change
test.setText(settings.getString("YourScore" , "No Score"));
To
test.setText(""+settings.getInt("YourScore" , 0));