android - 如何在android Activity 生命周期方法中保存数据

标签 android autocompletetextview

我目前正在开发一个应用程序。在我的应用程序中,我使用了一个自动完成 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/

相关文章:

android - Android 中 SQLite 的默认线程模式是什么?

android - 如何避免在 onDraw 方法中创建对象

android - 自动完成功能无法正常工作的地方

android - 如何在 Android 中为 AutoCompleteTextView 设置多次点击监听器?

java - Android AutoCompleteTextView 如何忽略多余的空格

android - 当行宽等于 TextView 的宽度时,为什么 TextView 会断行

java - 作为我的应用程序打开应用程序时获取文件路径

android - 如何在 AutoCompleteTextView 中填充数据?

android - 按下返回键后如何保持 AutoCompleteTextView 的 DropDownList 打开?

java - 如何制作一个 parse.com 注销按钮将我带到另一个让我登录或注册的 Activity ?