SharedPreferences数据仍然无法更新TextView

时间:2013-04-22 06:03:19

标签: java android textview sharedpreferences

我有一个活动类,它将扫描一个NFC标签并将其分配给一个字符串(这部分运行正常)然后通过sharedpreferences共享该字符串,然后更新textview(以及其他内容)。出于某种原因,textview似乎永远不会使用NFC标签中的文本进行更新。它应该是一个足够简单的问题 - 我根本无法获得分配给字符串的NFC数据来通过共享偏好更新textview,我不知道为什么。

CONNECT.JAVA CODE SNIPPET:

   // after scanning - splitting the message by comma
          String[]tagdata=msgtext.split(",");

        String networkSSID = tagdata[0].toString();
        String networkPass = tagdata[1].toString();
        String time = tagdata[2].toString();
        String restricted = tagdata[3].toString();
        String corename = tagdata[4].toString();





        String NDEF_PREF = "prefs";
        SharedPreferences prefs = getSharedPreferences(NDEF_PREF, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=prefs.edit();
     //   editor.putBoolean(time, true);
        editor.putString("time", time);
        editor.putBoolean(restricted, true);
        editor.putBoolean(corename, true);
        editor.commit();

RULES.JAVA CODE SNIPPET :(显示时间textview - 但从未改变)

    String NDEF_PREF = "prefs";
    SharedPreferences prefs = getSharedPreferences(NDEF_PREF, Context.MODE_PRIVATE); 
    boolean name = prefs.getBoolean("name", true); 
    boolean code = prefs.getBoolean("corename", true);
    //boolean time = prefs.getBoolean("time", true);
    String time = prefs.getString("time", "");
    boolean ssid = prefs.getBoolean("restricted", true);
    Time.setText(String.valueOf(time));
//String time = String.valueOf(time);



       Intent intent2 = new Intent(Rules.this, KillTimer.class);
        PendingIntent pintent2 = PendingIntent.getActivity(Rules.this, 0, intent2,
                0);
        AlarmManager alarm2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                time ? 1000 : 0, pintent2);   

1 个答案:

答案 0 :(得分:0)

editor.putString("name", name)依此类推和prefs.getString(“name”,“”);

而不是

SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE);

在CONNECT.JAVA中

你应该使用

SharedPreferences prefs = getSharedPreferences(NDEF_PREF, Context.MODE_PRIVATE);

当您致电getPreferences时,偏好设为私人活动。