Zugriff Auf Die Gemeinsamen Einstellungen Über Aktivitäten

Ich habe eine SharedPreference in diesem .java-Datei, in Richtung der Unterseite können Sie sehen, dass ich speichern Sie die Werte, um die SharedPreferences GB_PREFERENCES_BENCH, und GB_PREFERENCES_FLIES. Wie verwende ich diese Werte in einer anderen Tätigkeit? Siehe zweites code-Beispiel, wie ich es verwenden möchten.

package com.creativecoders.gymbuddy;

import com.creativecoders.gymbuddy.R;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class Benchmark extends Activity {

public static final String GB_PREFERENCES = "Prefs";

public static final String GB_PREFERENCES_BENCH = "Bench";
public static final String GB_PREFERENCES_FLIES = "Flies";

SharedPreferences gBValues;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_benchmark);

    gBValues = getSharedPreferences(GB_PREFERENCES, Context.MODE_PRIVATE);

}

public void onStart() {
    super.onStart();

    findViewById(R.id.button5).setOnClickListener(new handleButton5());

}

class handleButton5 implements OnClickListener {
    public void onClick(View v) {

        EditText editText1 = (EditText)findViewById(R.id.editText1);
        String sWeight = editText1.getText().toString();
        final double dWeight = Double.parseDouble(sWeight);

        EditText editText2 = (EditText)findViewById(R.id.editText2);
        String sPush = editText2.getText().toString();
        final double dPush = Double.parseDouble(sPush);

        EditText editText3 = (EditText)findViewById(R.id.editText3);
        String sSit = editText3.getText().toString();
        final double dSit = Double.parseDouble(sSit);

        EditText editText4 = (EditText)findViewById(R.id.editText4);
        String sPull = editText4.getText().toString();
        final double dPull = Double.parseDouble(sPull);

        double dBench = (((Math.floor(dWeight*.0664))*10)-10)+dPush;
        double dFlies = (Math.floor(((Math.floor(dBench*.6)/10)*10)));

        int iBench = (int)dBench;
        int iFlies = (int)dFlies;

        Editor editor1 = gBValues.edit();
        editor1.putInt(GB_PREFERENCES_BENCH, iBench);
        editor1.commit();

        Editor editor2 = gBValues.edit();
        editor2.putInt(GB_PREFERENCES_FLIES, iFlies);
        editor2.commit();

        }
    }

 }

Hier ist, wie ich will, es zu benutzen; (speziell in der create-Methode, um eine TextView text auf den Wert in der SharePreference)

package com.creativecoders.gymbuddy;

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 Upper100Start extends Activity {

public static final String GB_PREFERENCES = "Prefs";
public static final String GB_PREFERENCES_CURLS = "Curls";
SharedPreferences gBValues;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.upper100start);

    if (gBValues.contains(GB_PREFERENCES_CURLS)){
    TextView TextView2 = (TextView)findViewById(R.id.textView2);
    TextView2.setText(gBValues.getString(GB_PREFERENCES_CURLS, ""));
    }

}
public void onStart() {
    super.onStart();

    findViewById(R.id.button2).setOnClickListener(new handleButton2());
    findViewById(R.id.button3).setOnClickListener(new handleButton3());
}
class handleButton2 implements OnClickListener {
    public void onClick(View v) {
        Intent intent = new Intent(Upper100Start.this, Upper101.class);
        startActivity(intent);
        }
    }
class handleButton3 implements OnClickListener {
    public void onClick(View v) {
        Intent intent = new Intent(Upper100Start.this, Main.class);
        startActivity(intent);
        }
    }

}
Schreibe einen Kommentar