Android 在首选项中存储值后,它不会在textview中设置

Android 在首选项中存储值后,它不会在textview中设置,android,json,sharedpreferences,textview,android-alertdialog,Android,Json,Sharedpreferences,Textview,Android Alertdialog,我是安卓开发者。我正在创建一个应用程序,在我的应用程序中,我使用json解析获取状态名,因此当用户单击texTextViewtview(“选择状态”)时,将显示包含所有状态名的警报对话框 然后我可以选择任何州的名称,它将设置在我的文本视图中,并优先存储 在这里,它工作得很好,但问题是如果我关闭应用程序,然后再次打开我在TextView中设置的应用程序,它会再次显示“选择状态” 任何帮助都将不胜感激 下面是我的代码片段 MainActivity.java @Override protect

我是安卓开发者。我正在创建一个应用程序,在我的应用程序中,我使用json解析获取状态名,因此当用户单击texTextViewtview(“选择状态”)时,将显示包含所有状态名的警报对话框

然后我可以选择任何州的名称,它将设置在我的文本视图中,并优先存储

在这里,它工作得很好,但问题是如果我关闭应用程序,然后再次打开我在TextView中设置的应用程序,它会再次显示“选择状态”

任何帮助都将不胜感激

下面是我的代码片段

MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //spcountry=(TextView)rootView.findViewById(R.id.spinnercountry);
        spstate=(TextView)findViewById(R.id.feedbackspinnerstates);
        spcity=(TextView)findViewById(R.id.feedbackspinnercitys);




        new LoadAllStates().execute();


    }

    class LoadAllStates extends
            AsyncTask<String, String, ArrayList<HashMap<String, String>>> {



        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please wait..");
            pDialog.setIndeterminate(true);
            // pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
            pDialog.setCancelable(true);
            pDialog.show();

        }

        protected ArrayList<HashMap<String, String>> doInBackground(
                String... args) {
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            statedata = new ArrayList<HashMap<String, String>>();
            String jsonStr = sh.makeServiceCall(STATE_URL, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    state_list = jsonObj.getJSONArray(COUNTRY_LIST);

                    // looping through All Contacts
                    for (int i = 0; i < state_list.length(); i++) {
                        JSONObject c = state_list.getJSONObject(i);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(STATE_SLG, c.getString(STATE_SLG));
                        map.put(STATE_ID, c.getString(STATE_ID));
                        map.put(STATE_NAME, c.getString(STATE_NAME));

                        statedata.add(map);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return statedata;
        }

        protected void onPostExecute(ArrayList<HashMap<String, String>> result) {

            super.onPostExecute(result);
            pDialog.dismiss();

            arrallstates = new String[statedata.size()];
            for (int index = 0; index < statedata.size(); index++) {
                HashMap<String, String> map = statedata.get(index);
                arrallstates[index] = map.get(STATE_NAME);
            }

            // pass arrConuntry array to ArrayAdapter<String> constroctor :
            adapterallstates = new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_spinner_dropdown_item, arrallstates);
            spstate.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View w) {


                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("Select")
                            .setAdapter(adapterallstates,
                                    new DialogInterface.OnClickListener() {



                                        @Override
                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {



                                            test=adapterallstates
                                                    .getItem(which).toString();

                                            SharedP.setStringToSharedPrefsForKey("MyKey", test, MainActivity.this);

                                            System.out.println("whts in pref"+SharedP.setStringToSharedPrefsForKey("MyKey", test, MainActivity.this));

                                            System.out.println("whts in pref get"+SharedP.getStringFromSharedPrefsForKey("MyKey", MainActivity.this));

                                            storeds=SharedP.getStringFromSharedPrefsForKey("MyKey", MainActivity.this);

                                            Log.d("Tag", SharedP.getStringFromSharedPrefsForKey("MyKey", MainActivity.this));
                                                                                        spstate.setText(storeds); 

                                             if( spstate.getText().length() != 0) 
                                                {
                                                 System.out.println("NotEMpty");
                                                }
                                                else
                                                {
                                                    System.out.println("EMpty");

                                                }

                                            try {
                                                statename = state_list
                                                        .getJSONObject(which)
                                                        .getString("state_slug");
                                                stnm = state_list
                                                        .getJSONObject(which)
                                                        .getString(STATE_NAME);

                                                Log.d("Response statenm: ",
                                                        "> " + statename);
                                            //  new LoadAllStatesCity()
                                                        //.execute();

                                                // Toast.makeText(getActivity(),
                                                // statename,
                                                // Toast.LENGTH_LONG).show();
                                            } catch (JSONException e) {
                                                // TODO Auto-generated catch
                                                // block
                                                e.printStackTrace();
                                            }

                                            dialog.dismiss();
                                        }
                                    }).create().show();
                }
            });

        }
    }
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//spcountry=(TextView)rootView.findViewById(R.id.spinnercountry);
spstate=(TextView)findViewById(R.id.feedbackspinnerstates);
spcity=(TextView)findViewById(R.id.feedbackspinnercitys);
新建LoadAllState().execute();
}
类loadAllState扩展
异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(MainActivity.this);
pDialog.setMessage(“请稍候…”);
pDialog.setUndeterminate(真);
//pDialog.setIndenderedRavable(getResources().getDrawable(R.drawable.custom_progress));
pDialog.setCancelable(真);
pDialog.show();
}
受保护的ArrayList doInBackground(
字符串…args){
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
statedata=newarraylist();
字符串jsonStr=sh.makeServiceCall(STATE\uURL,ServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
state_list=jsonObj.getJSONArray(COUNTRY_list);
//通过所有触点循环
对于(int i=0;ivalue
map.put(STATE_SLG,c.getString(STATE_SLG));
map.put(STATE_ID,c.getString(STATE_ID));
map.put(STATE_NAME,c.getString(STATE_NAME));
statedata.add(map);
}
}捕获(JSONException e){
e、 printStackTrace();
}
}否则{
Log.e(“ServiceHandler”,“无法从url获取任何数据”);
}
返回状态数据;
}
受保护的void onPostExecute(ArrayList结果){
super.onPostExecute(结果);
pDialog.disclose();
arrallstates=新字符串[statedata.size()];
对于(int index=0;index SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("yourtextvalueKey", "yourvalue");
 editor.commit();
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String text= prefs.getString("yourtextvalueKey", null);

if(text != null){
textview.setText(text);
}