我目前正在开发一个应用程序。在我的应用程序中,我使用了一个自动完成 TextView ,其中我最初没有设置自动完成 TextView 适配器。这个想法是用户将正常输入他们的第一个数据,因此不会显示任何建议,但按下保存按钮后,适配器将被设置,然后如果他们尝试输入相同的数据,它将显示建议。我完成了这部分。但我的问题是当我返回主屏幕或按下返回或主屏幕按钮时我所有的数据都丢失了。帮助我并推进 tnx。通过使用数据库,我希望问题会得到解决,但我想保持简单并且不想使用数据库。还有其他方法吗..
package com.example.oggy.autocompletetextviewsuggestion;
import android.content.Intent;
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.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView textView;
String[] array;
ArrayList <String> myList = new ArrayList<String>();
ArrayList<String> myList1 = new ArrayList<String>();
ArrayList<String> myList2 = new ArrayList<String>();
ListView listView;
ArrayAdapter<String> adapter;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), "oncreate
is calling", Toast.LENGTH_SHORT).show();
Button button = (Button) findViewById(R.id.button);
textView = (AutoCompleteTextView) findViewById(R.id.autocomplite);
listView = (ListView) findViewById(R.id.list);
array =
getResources().getStringArray(R.array.country_name);
// when screen is rotating i can retrive data
successfully
if (savedInstanceState!=null){
Toast.makeText(getApplicationContext(), "execuiting saveinstance part" , Toast.LENGTH_SHORT).show();
myList2=savedInstanceState.getStringArrayList("value");
Toast.makeText(getApplicationContext(), "size :" +myList2.size(), Toast.LENGTH_SHORT).show();
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, myList2);
textView.setAdapter(adapter);
}
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, myList1);
textView.setAdapter(adapter);
//saving data by pressing the button and save it in my list and also set the autocomplite textview adapter
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// String[] series = {"abang","bang"};
// array1[i]=textView.getText().toString();
myList.add(textView.getText().toString());
Toast.makeText(getApplicationContext(), "Item :" + textView.getText().toString(), Toast.LENGTH_SHORT).show();
// Toast.makeText(getApplicationContext(), "data :" + array1[0].toString(), Toast.LENGTH_SHORT).show();
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, myList);
textView.setAdapter(adapter);
}
});
}
//save the list view while screen is roatating
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
myList1 = myList;
outState.putStringArrayList("value",myList1);
}
@Override
protected void onStop() {
super.onStop();
Toast.makeText(getApplicationContext(), "onstop is calling", Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(), "ondistroy is calling", Toast.LENGTH_SHORT).show();
}
@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(getApplicationContext(), "onrestart is calling", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(), "onstart is calling", Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(), "onresume is calling", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(getApplicationContext(), "onpause is calling", Toast.LENGTH_SHORT).show();
}
}
最佳答案
如果您想在本地保存数据,请使用 SharedPreferences ,它允许您永久保存键值对。
这是一个简单的使用方法(引用上面的链接)。
获取SharedPreferences
实例
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
写给它
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("SavedHighScore", newHighScore);
editor.commit();
从中读取
long highScore = sharedPref.getInt("SavedHighScore", myDefaultValue);
写 list
无法添加字符串列表,但可以添加 Set<String>
使用 putStringSet .假设你有一个 ArrayList<String>
您可以这样将其写入 SharedPreferences:
ArrayList<String> mList = new ArrayList<> {{/* .... */}};
Set<String> mSet = new HashSet<String>(mList);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putStringSet("MyStringSetKey", mSet);
editor.commit();
阅读 list
Set<String> defValues = new HashSet<String>();
/* Initialize your default value set with your default values,
eventually empty or null */
Set<String> storedValues = sharedPref.getStringSet("MyStringSetKey", defValues);
/* Now you have a set containing your strings
if you want you can convert it to ArrayList or whatever this way */
ArrayList<String> mStrings = new ArrayList<String>(storedValues);
附言
在示例中,我对 key 字符串进行了硬编码。这个例子更容易,但你必须在实际代码中避免它。始终使用字符串资源。
关于android - 如何在android Activity 生命周期方法中保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44454335/