0

editText を Sharedprefrences に保存して TextView に表示しようとしていますamnt。表示されているのに保存されていませんか?私は何を間違っていますか?私はこれが初めてで、週末のちょっとした趣味なので、これがばかげた質問であれば申し訳ありません。お時間をいただきありがとうございます。

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();}
}}
4

2 に答える 2

0

共有プリファレンスを使用すると、キーと値のペア データを保存および取得できます。

初期化:

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); 
// 0 - for private mode
Editor editor = pref.edit();

データの保存:

editor.putString("key_name", "string value"); // Storing string
editor.commit(); // commit changes

データの取得:

pref.getString("key_name", null); // getting String

SharedPreferences について詳しく知りたい場合、または簡単な例を試してみたい場合は、こちらを確認してください

于 2013-11-13T06:16:58.300 に答える
0

一部のプリミティブと文字列 (文字列セットも) のキーと値のペアのみを保存できます。Android ビューはどこにも保存できません。

SharedPreferences manager =PreferenceManager.getDefaultSharedPreferences(context);

次のようにデータの保存と取得に使用します

// for saving data
manager.edit().putString(context.getString(AT), YOUR_TEXT).commit()

// for retrieving data
manager.getString(context.getString(AT), "");
于 2013-11-13T06:18:44.900 に答える