我想保存最后一次在微调器中单击的项目号,当我打开应用程序时,我希望微调器在此项目号中打开。但是,即使我现在保存项目位置,打开应用程序时它也不会在该位置打开。打开应用程序时,我希望选择打开的最后一个项目。
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
SharedPreferences prefs = getSharedPreferences("..",Context.MODE_PRIVATE);
Integer indexValue =spinner.getSelectedItemPosition();
sharedPreferences = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor editor = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE).edit();
editor.putInt("Spinner", indexValue);
editor.apply();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
refRoomsNew.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
spinner = (Spinner)findViewById(R.id.spinnerMain);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String(MainActivity.this,android.R.layout.simple_spinner_item, RoomsNew);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
String data = snapshot.getValue(String.class);
RoomsNew.add(data);
Integer spinnerNew = prefs.getInt("Spinner", 0);
if(spinnerNew != null ) {
spinner.setSelection(spinnerNew);
}
addListenerOnSpinnerItemSelection();
}
}
refRooms.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
spinner = (Spinner)findViewById(R.id.spinnerMain);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_spinner_item, Rooms);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
String data = snapshot.getValue(String.class);
Rooms.add(data);
Integer spinnerNew = prefs.getInt("Spinner", 0);
if(spinnerNew != null ) {
spinner.setSelection(spinnerNew);
}
addListenerOnSpinnerItemSelection();
}
}
public void addListenerOnSpinnerItemSelection() {
spinner.setOnItemSelectedListener(this);
}
答案 0 :(得分:0)
很抱歉用Kotlin编写解决方案。 下面是工作代码。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/txt_select"
android:layout_margin="8dp"
android:text="Select Month"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Spinner
android:layout_below="@id/txt_select"
android:id="@+id/spinner"
android:layout_margin="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
MainActivity.kt
class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener {
private val monthList = arrayOf("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Noc", "Dec")
private var preferenceManger = PreferenceManager(this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val adapter: ArrayAdapter<String> =
ArrayAdapter(this, android.R.layout.simple_spinner_item, monthList)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = adapter
spinner.onItemSelectedListener = this
}
override fun onResume() {
super.onResume()
spinner.setSelection(preferenceManger.getSelection())
}
override fun onNothingSelected(parent: AdapterView<*>?) {
spinner.setSelection(preferenceManger.getSelection())
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
preferenceManger.setSelection(position)
}
}
PreferenceManager.kt
const val PREFERENCE_NAME = "test"
const val KEY_SELECTION = "key_selection"
class PreferenceManager(val context: Context) {
fun setSelection(pos: Int) {
val sharedPref = context.getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE)
sharedPref.edit().putInt(KEY_SELECTION, pos).commit()
}
fun getSelection(): Int {
val sharedPref = context.getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE)
return sharedPref.getInt(KEY_SELECTION, 0)
}
}
让我知道更多帮助:)
答案 1 :(得分:0)
在Java中具有相同的答案
注意:以下是示例代码,您可以在其中存储微调框选择。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/txt_select"
android:layout_margin="8dp"
android:text="Select Month"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Spinner
android:layout_below="@id/txt_select"
android:id="@+id/spinner"
android:layout_margin="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private String[] monthList = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Noc", "Dec"};
private PreferenceManager preferenceManger;
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferenceManger = new PreferenceManager(this);
spinner = findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, monthList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
protected void onResume() {
super.onResume();
spinner.setSelection(preferenceManger.getSelection());
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
preferenceManger.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
spinner.setSelection(preferenceManger.getSelection());
}
}
PreferenceManager.java
public class PreferenceManager {
private static final String PREFERENCE_NAME = "test";
private static final String KEY_SELECTION = "key_selection";
Context mContext;
public PreferenceManager(Context context) {
mContext = context;
}
public void setSelection(int pos) {
SharedPreferences sharedPref = mContext.getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE);
sharedPref.edit().putInt(KEY_SELECTION, pos).commit();
}
public int getSelection() {
SharedPreferences sharedPref = mContext.getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE);
return sharedPref.getInt(KEY_SELECTION, 0);
}
}