我想使用SharedPreferences
保存在spinner
这是我的编码
package com.mulder.jip.schoolroosterbeta2;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class CustumMaandag extends Activity {
Context Context = this;
private Spinner les1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custum_maandag);
{
les1 = (Spinner) findViewById(R.id.les1);
List<String> list = new ArrayList<String>();
list.add("Test1");
list.add("Test2");
list.add("Test3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_dropdown_item,list);
dataAdapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
les1.setAdapter(dataAdapter);
les1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object obj = parent.getItemAtPosition(pos);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Context);
SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.putString("object", obj.toString());
prefsEditor.commit();
}
public void onNothingSelected(AdapterView<?> parent) { }
});
}
}
}
我做错了什么?
答案 0 :(得分:1)
试试这个。
String selecteditem = les1 .getSelectedItem().toString();
并保存在像这样的共享pref中
prefsEditor.putString("object", selecteditem );
答案 1 :(得分:0)
尝试在onItemSelected(...){ *here }
String selectedText = les1.getSelectedItem().toString();
SharedPreferences.Editor editor = getSharedPreferences("your_prefs_name", MODE_PRIVATE).edit();
editor.putString("your_key", selectedText);
editor.commit();