Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何设置ArrayAdapter<;字符串>;来自SharedReferences_Java_Android_Sharedpreferences - Fatal编程技术网

Java 如何设置ArrayAdapter<;字符串>;来自SharedReferences

Java 如何设置ArrayAdapter<;字符串>;来自SharedReferences,java,android,sharedpreferences,Java,Android,Sharedpreferences,如何设置id.setAdapter(适配器)来自SharedReferences?我希望当应用程序关闭时,所有id.getText().toString()都应该存在 ArrayList<String> list = new ArrayList<String>(); ArrayAdapter<String> adapter; private SharedPreferences atPrefs; atPrefs = PreferenceManager.getDe

如何设置
id.setAdapter(适配器)来自
SharedReferences
?我希望当应用程序关闭时,所有
id.getText().toString()
都应该存在

ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
private SharedPreferences atPrefs;
atPrefs = PreferenceManager.getDefaultSharedPreferences(Login.this);
id = (AutoCompleteTextView) findViewById(R.id.id);

adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_expandable_list_item_1, list);
id.setAdapter(adapter);
adapter.setNotifyOnChange(true);
id.setThreshold(1);

submit.setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View arg0) {
        if (id.getText().toString().length() >= 3) {
            
            atPrefs.edit().putString(pos, id.getText().toString()).commit();
            adapter.add(atPrefs.getString(pos, ""));
            
            Intent nextscreen = new Intent(Login.this, Otp.class);
            startActivity(nextscreen);
            // finish();
        }
    }
});
ArrayList list=new ArrayList();
阵列适配器;
私人共享的参考资料;
atPrefs=PreferenceManager.getDefaultSharedReferences(Login.this);
id=(AutoCompleteTextView)findviewbyd(R.id.id);
适配器=新阵列适配器(此,
android.R.layout.simple_可扩展_列表_项目_1,列表);
id.setAdapter(适配器);
adapter.setNotifyOnChange(true);
id.setThreshold(1);
submit.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图arg0){
如果(id.getText().toString().length()>=3){
atPrefs.edit().putString(pos,id.getText().toString()).commit();
adapter.add(atPrefs.getString(pos,“”);
Intent nextscreen=newintent(Login.this,Otp.class);
startActivity(nextscreen);
//完成();
}
}
});

解决了的 用下面的代码解决

for (i = 0; i < k; i++) {

    list.add(0, (String) prfs.getString(String.valueOf(i), "null"));
    adapter.notifyDataSetChanged();
}

public void onClick(View arg0) {
    if (id.getText().toString().length() >= 3) {
    
        list.add(0, (String) id.getText().toString());
        adapter.notifyDataSetChanged();
        
        edit.putString(String.valueOf(i), id.getText().toString());
        edit.putInt("counter", i + 1);
        edit.apply();
        
        id.setText("");
        
        i = i + 1;
    }
(i=0;i{ 添加(0,(String)prfs.getString(String.valueOf(i),“null”); adapter.notifyDataSetChanged(); } 公共void onClick(视图arg0){ 如果(id.getText().toString().length()>=3){ 添加(0,(字符串)id.getText().toString()); adapter.notifyDataSetChanged(); edit.putString(String.valueOf(i),id.getText().toString()); 编辑输入(“计数器”,i+1); edit.apply(); id.setText(“”); i=i+1; }
您应该检查一下,在SharedReferences中保留值不是一个好主意

此外,如果没有正确使用共享首选项,则只能将值保存在中,而无法使用_context.getString将其取回

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  savedInstanceState.putStringArray("myarray", list);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  list = savedInstanceState.getStringArray("myarry");

}

那么问题是什么呢?如果我关闭应用程序数据,为什么要使用SharedReference?使用数据库。只需2-3个条目打印字符串和键-您可能使用了错误的键进行检索。不要为此使用db,当然本质上这与使用全局变量的参数相同,应该避免使用ed.您存储的数据是特定于活动的,而不是特定于应用程序的,因此每次您获得共享首选项的引用时,您都会将保存在其中的所有值加载到内存中。当您有更多活动时,您将加载后台其他活动的不必要值,更不用说每个人都会加载其他活动允许对您保存的值进行更改。den我使用了putStringArrayList,但当我关闭应用程序并再次打开它时,仍然无法获取数据。Ini不同意-SP非常适合这样做-保持密钥的私密性可以帮助您最大限度地减少活动交互