Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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
如何在android活动生命周期方法中保存数据_Android_Autocompletetextview - Fatal编程技术网

如何在android活动生命周期方法中保存数据

如何在android活动生命周期方法中保存数据,android,autocompletetextview,Android,Autocompletetextview,我目前正在开发一个应用程序。在我的应用程序中,我使用的是一个自动完成文本视图,我最初没有设置自动完成文本视图适配器。其想法是用户将正常输入他们的第一个数据,并且不会显示任何建议,但在按下保存按钮后,将设置适配器,然后如果他们尝试输入相同的数据它将显示建议。我已完成此部分。但我的问题是,当我返回主屏幕或按“上一步”或“主屏幕”按钮时,我的所有数据都将丢失。请帮助我并推进tnx。通过使用数据库,我希望问题能得到解决,但我想保持简单,不想使用数据库。还有其他方法吗 package com.exampl

我目前正在开发一个应用程序。在我的应用程序中,我使用的是一个自动完成文本视图,我最初没有设置自动完成文本视图适配器。其想法是用户将正常输入他们的第一个数据,并且不会显示任何建议,但在按下保存按钮后,将设置适配器,然后如果他们尝试输入相同的数据它将显示建议。我已完成此部分。但我的问题是,当我返回主屏幕或按“上一步”或“主屏幕”按钮时,我的所有数据都将丢失。请帮助我并推进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();

    }

}
package com.example.oggy.autocompletetextviewsuggestion;
导入android.content.Intent;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.AutoCompleteTextView;
导入android.widget.Button;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
导入java.util.ArrayList;
导入java.util.List;
公共类MainActivity扩展了AppCompatActivity{
自动完成文本视图文本视图;
字符串[]数组;
ArrayList myList=新的ArrayList();
ArrayList myList1=新的ArrayList();
ArrayList myList2=新的ArrayList();
列表视图列表视图;
阵列适配器;
int i=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(),“oncreate
正在调用“Toast.LENGTH_SHORT).show();
Button Button=(Button)findViewById(R.id.Button);
textView=(autocompletextview)findViewById(R.id.autocomplete);
listView=(listView)findViewById(R.id.list);
数组=
getResources().getStringArray(R.array.country_name);
//当屏幕旋转时,我可以检索数据
成功地
如果(savedInstanceState!=null){
Toast.makeText(getApplicationContext(),“execuiting saveinstance部分”,Toast.LENGTH\u SHORT.show();
myList2=savedInstanceState.getStringArrayList(“值”);
Toast.makeText(getApplicationContext(),“size:+myList2.size(),Toast.LENGTH\u SHORT).show();
adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple\u list\u item\u 1,myList2);
setAdapter(适配器);
}
adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple\u list\u item\u 1,myList1);
setAdapter(适配器);
//按下按钮保存数据并将其保存在我的列表中,同时设置autocomplite textview适配器
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//字符串[]系列={“阿邦”,“邦”};
//array1[i]=textView.getText().toString();
添加(textView.getText().toString());
Toast.makeText(getApplicationContext(),“Item:+textView.getText().toString(),Toast.LENGTH_SHORT).show();
//Toast.makeText(getApplicationContext(),“数据:+array1[0].toString(),Toast.LENGTH_SHORT).show();
adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple\u list\u item\u 1,myList);
setAdapter(适配器);
}
});
}
//在屏幕旋转时保存列表视图
@凌驾
SaveInstanceState上受保护的无效(束超出状态){
super.onSaveInstanceState(超出状态);
myList1=myList;
outState.putStringArrayList(“值”,myList1);
}
@凌驾
受保护的void onStop(){
super.onStop();
Toast.makeText(getApplicationContext(),“onstop正在调用”,Toast.LENGTH\u SHORT.show();
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
Toast.makeText(getApplicationContext(),“ondistroy正在调用”,Toast.LENGTH\u SHORT.show();
}
@凌驾
受保护的void onRestart(){
super.onRestart();
Toast.makeText(getApplicationContext(),“onrestart正在调用”,Toast.LENGTH\u SHORT.show();
}
@凌驾
受保护的void onStart(){
super.onStart();
Toast.makeText(getApplicationContext(),“onstart正在调用”,Toast.LENGTH\u SHORT.show();
}
@凌驾
受保护的void onResume(){
super.onResume();
Toast.makeText(getApplicationContext(),“onresume正在调用”,Toast.LENGTH\u SHORT.show();
}
@凌驾
受保护的void onPause(){
super.onPause();
Toast.makeText(getApplicationContext(),“onpause正在调用”,Toast.LENGTH\u SHORT.show();
}
}

将数据保存到另一个活动

Intent intent = new Intent(CurrentActivity.this, ActivityToRetrieve.class);
intent.putExtra("KEY", "VALUE");//String data
startActivity(intent);
然后检索
onCreate()中的数据


String theData=getIntent.getExtras().getString(“KEY”)

如果您想在本地保存数据,请使用,它允许您永久保存键值对

这里有一个简单的使用方法(引用上面的链接)

获取
SharedReferences
实例 写信给它 从中读出 书写列表 无法添加字符串列表,但您可以使用添加
集合
。假设您有一个
ArrayList
,您可以通过以下方式将其写入SharedReferences:

ArrayList<String> mList = new ArrayList<> {{/*  ....  */}};
Set<String> mSet = new HashSet<String>(mList);

SharedPreferences.Editor editor = sharedPref.edit();
editor.putStringSet("MyStringSetKey", mSet);
editor.commit();
ArrayList mList=新的ArrayList{{/*../};
Set mSet=新哈希集(mList);
SharedPreferences.Editor=sharedPref.edit();
编辑器.putStringSet(“MyStringSetKey”,mSet);
commit();
阅读列表
Set defValues=new HashSet();
/*使用默认值初始化默认值集,
最终为空或空*/
Set-storedValues=sharedPref.getStringSet(“MyStringSetKey”,defValues);
/*现在你有了一个集合
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("SavedHighScore", newHighScore);
editor.commit();
long highScore = sharedPref.getInt("SavedHighScore", myDefaultValue);
ArrayList<String> mList = new ArrayList<> {{/*  ....  */}};
Set<String> mSet = new HashSet<String>(mList);

SharedPreferences.Editor editor = sharedPref.edit();
editor.putStringSet("MyStringSetKey", mSet);
editor.commit();
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);