活动之间不可用的SharedPreference值

时间:2014-10-14 17:08:39

标签: android sharedpreferences

我有2项活动。一个是主要活动。一个是偏好活动。我试图根据Preference活动中RadioGroup中选择的颜色更改活动的背景颜色。

这是主要活动的类文件。

package com.example.mycsimodules;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import android.widget.ArrayAdapter;

public class ModList extends ActionBarActivity {
   SharedPreferences savedData;
   private String[] moduleArray = { "COMP 41600", "COMP 41620", "COMP 47330","COMP 30160", "COMP 30500", "COMP 40725", "COMP 41100", "COMP 41110" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mod_list);

    savedData=getSharedPreferences("MyPrefs",0);
    String colorMine=savedData.getString("color", "deflt");
    TextView head=(TextView) findViewById(R.id.mymodlist);
    head.setText(colorMine);

    ArrayAdapter<String> moduleAdapter = new ArrayAdapter<String>(this,     R.layout.activity_list_view, R.id.list1, moduleArray);
    final ListView list = (ListView) findViewById(R.id.mod_list);
    list.setAdapter(moduleAdapter);


        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.mod_list, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        Intent i=new Intent(this, Preferences.class);
        startActivity(i);

        return true;
    }
    return super.onOptionsItemSelected(item);
}

这是Preference类的类文件。

package com.example.mycsimodules;

import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class Preferences extends ActionBarActivity implements OnClickListener{
   RadioGroup colorList;
   Button saveButton;
   SharedPreferences savedData;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_preferences);

    colorList=(RadioGroup) findViewById(R.id.colorRadioGroup);
    saveButton=(Button) findViewById(R.id.bSave);

    saveButton.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.preferences, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@SuppressLint("NewApi") @Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v==saveButton){
        RadioButton myColor=(RadioButton) findViewById(colorList.getCheckedRadioButtonId());
        String colorSelected=myColor.getText().toString();
        TextView label=(TextView) findViewById(R.id.the_color_is);
        label.setText(colorSelected);

        savedData=getSharedPreferences("MyPref", 0);
        SharedPreferences.Editor editor=savedData.edit();
        editor.putString("color", colorSelected);
        editor.apply();
        editor.commit();


    }

}
}

我还没有真正完成背景色部分。我只想尝试在TextView中显示该值。但它始终只显示默认值。我想做的是。 单击“设置”。 单击RadioButton获取所需的颜色。 单击“保存”按钮。 单击返回键返回主要活动。

我对此很新,可能会做错事。但我无法弄清楚它是什么。

1 个答案:

答案 0 :(得分:4)

savedData=getSharedPreferences("MyPrefs",0);

不同
savedData=getSharedPreferences("MyPref", 0);

打开SharedPreferences

时使用相同的文件