【问题标题】:Android Using SharedPreferences to save and retrieve values - CounterAndroid 使用 SharedPreferences 保存和检索值 - 计数器
【发布时间】:2012-10-13 10:01:22
【问题描述】:

您好,我是 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);

            }
        });
       }
}

结果.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”));
                }
        }

}

任何指导我的帮助将不胜感激。谢谢你

【问题讨论】:

    标签: android android-intent sharedpreferences


    【解决方案1】:

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

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

    【讨论】:

    • 您如何建议我检索从按钮到活动结果的 onclick 计数并因此显示在文本视图中?谢谢
    • 这样的事情会得到你刚刚在上面的代码中设置的值:getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE).getInt(GAME_PREFERENCES_SCORE);
    【解决方案2】:

    只需创建一个 Preferences 类

    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 类中设置它

    【讨论】:

    • 您好,感谢您的帮助。我已经尝试了您的代码,但仍然出现错误。我真的不知道为什么。它似乎很容易,但我很难做到这一点。多一点帮助将不胜感激。谢谢
    • @GobinJay 你在哪里得到错误,哪个错误???..你在 Manifest 文件中声明了这两个活动吗??
    【解决方案3】:

    使用这样的方法:

        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 之后 counter++ 添加这个:

    SavePereference(context, "GAME_PREFERENCES_SCORE", counter);
    

    【讨论】:

    • 能否请您更明确地说明 SavePreference 方法的代码放置位置以及 Results.java 应该能够检索数据并将其显示到 textView 中,并且所需的答案是数字按钮上的点击次数。单击按钮后,应将用户定向到活动结果,其中计数将显示为 1。请您帮帮我好吗?我正在尽我所能,但仍然无法这样做。提前谢谢你
    • 你可以把你的静态方法放在一个静态类中。哪个班级和在哪里并不重要。
    猜你喜欢
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    相关资源
    最近更新 更多