Android使用SharedPreferences保存和检索值 - 计数器

时间:2012-10-13 10:01:22

标签: android android-intent sharedpreferences

您好我是Android的新手,实际上我正在开发一个应用程序,用户可以点击按钮,按钮应该记录点击事件 - 每次点击按钮时计数器都会递增。该按钮将显示在一个活动中,一旦用户点击该按钮,将显示另一个活动,从而显示结果。

实际上我在将sharedPreferences分配给按钮然后将其显示到下一个活动时遇到了问题,因此具有点击次数。

我使用的代码如下:

MainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    /** Declare the variables being used */
    public static final String GAME_PREFERENCES = "GamePrefs";

     public static final String GAME_PREFERENCES_SCORE = "Score"; // Integer
    int counter;
    Button add;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        counter = 0;
        add = (Button) findViewById (R.id.bAdd);
        add.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;

                Intent openClickActivity2 = new Intent("com.android.jay.Results");
                startActivity(openClickActivity2);

            }
        });
       }
}

Results.java

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
public class Results extends MainActivity{

    public void onCreate(Bundle savedInstanceState) {

        SharedPreferences mGameSettings;
        super.onCreate(savedInstanceState);
        mGameSettings = getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE);
        setContentView(R.layout.results);

        final TextView DisplayResults =
                (TextView) findViewById(R.id.bAdd);
                if (mGameSettings.contains(GAME_PREFERENCES_SCORE)) {
                DisplayResults.setText(mGameSettings.getString(GAME_PREFERENCES_SCORE, “counter”));
                }
        }

}

非常感谢任何指导我的帮助。谢谢你

3 个答案:

答案 0 :(得分:1)

您必须在MainActivity中设置GAME_PREFERENCES_SCORE。在反++之后做这样的事情:

getSharedPreferences(GAME_PREFERENCES,Context.MODE_PRIVATE).edit()。setInt(GAME_PREFERENCES_SCORE,counter)。提交();

答案 1 :(得分:0)

使用这样的方法:

    public static void SavePreference(Context context, String key, Integer value) {
    SharedPreferences.Editor editor = PreferenceManager
            .getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE)
            .edit();
    editor.putInt(key, value);
    editor.commit();
}

在你的onclick中,在++之后加上这个:

SavePereference(context, "GAME_PREFERENCES_SCORE", counter);

答案 2 :(得分:0)

只需制作一个偏好类

public class Preferences {

String MASTER_NAME = "mysticmatrix_master";
SharedPreferences mysticMatrixPref;

Preferences(Context context) {
    mysticMatrixPref = context.getSharedPreferences(MASTER_NAME, 0);
}

public void setAddCount(int count) {
    SharedPreferences.Editor prefEditor = mysticMatrixPref.edit();
    prefEditor.putInt("count", count);
    prefEditor.commit();

}

public int getAddCount() {
    return mysticMatrixPref.getInt("count", 0);
}
}

并在您的MainActivity.java中输入此代码

public class MainActivity extends Activity implements OnClickListener {

ImageButton add;
Preferences cpObj;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    preferences = new Preferences(getApplicationContext());

    /*
     * getting the count variable and adding 1 in that to check the condition of showing rate activity and adds
     */
     add = (Button) findViewById (R.id.bAdd);
    add.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
         cpObj = new Preferences(getApplicationContext());
         cpObj.setAddCount(cpObj.getAddCount() + 1);

        }
    });
    }
    }

在结果活动中,只需获取计数值

import android.content.Context;
public class Results extends MainActivity{
Preferences cpObj;

public void onCreate(Bundle savedInstanceState) {

     preferences = new Preferences(getApplicationContext());
    setContentView(R.layout.results);

    final TextView DisplayResults =
            (TextView) findViewById(R.id.bAdd);
            DisplayResults.setText(cpObj.getAddCount());
            }
    } }

这样你就会得到结果的默认值为'0',你可以在你的Preferences类中设置它