gpt4 book ai didi

java - 如何将 SharedPreferences 与微调器一起使用

转载 作者:行者123 更新时间:2023-12-02 03:26:00 25 4
gpt4 key购买 nike

我想使用 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) { }
});
}
}
}

我做错了什么?

最佳答案

尝试在 onItemSelected(...){ *here }

中使用它
String selectedText = les1.getSelectedItem().toString();
SharedPreferences.Editor editor = getSharedPreferences("your_prefs_name", MODE_PRIVATE).edit();
editor.putString("your_key", selectedText);
editor.commit();

关于java - 如何将 SharedPreferences 与微调器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38878940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com