Android:无法使用SharedPreferences保存Spinner数据

时间:2014-05-16 17:14:34

标签: android sharedpreferences spinner

我尝试了一切可能的方法来加载已保存的微调器数据,但它失败了。我尝试将label(TextView)保存到加载首选项中的SharedPreferencesmySpinner.getSelection,但无效。这是我的自定义微调器,任何想法如何完成它?

 public class AndroidCustomSpinner extends Activity {

    String[] DayOfWeek = {"Sunday", "Monday", "Tuesday", 
        "Wednesday", "Thursday", "Friday", "Saturday"};

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
        mySpinner.setAdapter(new MyCustomAdapter(AndroidCustomSpinner.this, R.layout.row, DayOfWeek)); 
    }

    public class MyCustomAdapter extends ArrayAdapter<String>{

        public MyCustomAdapter(Context context, int textViewResourceId,
                String[] objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }

        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
        // TODO Auto-generated method stub
            return getCustomView(position, convertView, parent);
    }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            //return super.getView(position, convertView, parent);

            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.row, parent, false);
            TextView label=(TextView)row.findViewById(R.id.weekofday);
            label.setText(DayOfWeek[position]);

            ImageView icon=(ImageView)row.findViewById(R.id.icon);

            if (DayOfWeek[position]=="Sunday"){
            icon.setImageResource(R.drawable.icon);
            }
            else{
                icon.setImageResource(R.drawable.icongray);
            }

            return row;
        }   
    }
}

2 个答案:

答案 0 :(得分:0)

使用以下代码:

String value;//Globally declared

    mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            String value = mySpinner.getSelectedItem().toString();
        }
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

一旦获得该值,您就可以将该值存储在共享首选项中

用于在共享偏好中存储值

SharedPreferences.Editor editor = getSharedPreferences(MODE_PRIVATE).edit();
 editor.putString("selected", value );
 editor.commit();

用于从共享首选项中检索值

SharedPreferences prefs = getSharedPreferences(MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("selected", "No selection");

}

答案 1 :(得分:0)

如果要在SharedPreferences中保存文本,可以执行以下代码:

        SharedPreferences Text;

       public static String filename = "Text";


    Text = getSharedPreferences(filename, 0);
    Editor editor = Text.edit;
     editor.putString("name",TextView.getText().toString());
       editor.commit();

然后,如果您想在重新启动应用后加载此文字:

       Text = getSharedPreferences(filename, 0);
       String textname = Text.getString("name", "this is default value if something goes wrong");