共享首选项数组数据保存在android中的emtpty数组中

时间:2017-09-06 11:22:31

标签: android arrays json sharedpreferences

我是android新手。我正在以共享偏好工作Json。从json我得到数据并保存到共享首选项,它工作正常。我想将共享首选项数组数据分配给SharedPreferences中的字符串。

这是代码:

      private ArrayList<String>urls2 = new ArrayList<>();
         private ArrayList<String>urls1 = new ArrayList<>();

        this is the json parse 
          if (errorCode == 0) {
                        email = p.getString("email");
                        firstName = p.getString("firstName");
                        lastName = p.getString("lastName");
                        phone = p.getString("phone");
                        status = p.getString("status");
                        updatedDate = p.getString("updatedDate");
                        serverURL = p.getString("url");
                        urls1.add(p.getString("url"));

        //                    urls1.append(",");
                        Log.d("URL", "" + urls1);
                        urls2.add(p.getString("staffId"))                   urls2.append(",");
                        Log.d("STAFFID", "" + urls2);

        }
SharedPreferences:

        SharedPreferences.Editor editor = getSharedPreferences("LOGINACTIVITY", 0).edit();


                            for(int i=0;i<urls2.size();i++) {
                                editor.putString("staffId1" + i, urls2.get(i));
                                Log.d("staffId1",urls2.get(i).toString());
                            }

                            for(int i=0;i<urls1.size();i++) {
                                editor.putString("serverURL" + i, urls1.get(i));
                                Log.d("SavedURl",urls1.get(i).toString());



                       }

 in next view controller i want assing the shared preference array data string 

         like ex : firsturl = arrayshared[0]

                  secondurl = arrayshared[1]

            this is next view controller 

             ArrayList<String> staffdata = new ArrayList<String>();

                    int listsize = mPrefsManager.getInt("staffId1", 0);

                    Log.d("firstcall of<<<<<<<",""+listsize);

                     for(int i=0; i<=listsize; i++) {

                        if(listsize == 0)
                        {

                        Hear : first shared preference array first element data 

                            like ex : firsturl = arrayshared[0]

                            Log.d("firstcall of<<<<<<<",""+listsize);
                            firsturl = .get(i).toString() 

                        }
                         else
                        {

                       like : secondurl = arrayshared[1]
                        ===> hear  second url data 
                            Log.d("second irst<<<",""+listsize);
                        }

如何将共享首选项数组中的数据转换为字符串

1 个答案:

答案 0 :(得分:0)

使用共享首选项(https://developer.android.com/training/basics/data-storage/shared-preferences.html#ReadSharedPreference)的正确方法是:

  1. 写入共享首选项:
  2. SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putString("Your Key name", yourString); editor.commit();

    1. 从共享首选项中读取:
    2. SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); String defaultValue = "default String"; String mString = sharedPref.getString("Your Key name", defaultValue);

      所以:

      第1步:我们已将yourString的值存储在具有键调用"Your Key name"的优先级中;

      第2步:我们已阅读首选项并将其值存储在mString变量中。

      因此,您可以轻松地使用循环将字符串存储在arraylist中(列表的每个元素都必须具有不同的键),并使用另一个循环来读取首选项并将每个首选项添加到另一个列表中。

      为了更好地使用,请不要忘记将ArrayList的长度存储在首选项中。

      例如,将String的arrayList写入SharedPreferences:

      SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
      SharedPreferences.Editor editor = sharedPref.edit();
      editor.putInt("lengtArrayList", lengtOfTheArrayList);
      for (int i = 0; i < lengtOfTheArrayList; i++) {
          editor.putString("Your Key name" + i, yourArrayList.get(i));
      }
      editor.commit();
      

      阅读并存储在另一个Arraylist中:

      SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
      int size = sharedPref.getInt("lengtArrayList", 0);
      String defaultValue = "default String";
      for (int i = 0; i < size; i++) {
          String mString = sharedPref.getString("Your Key name" + i, defaultValue);
          mArrayList.add(mString);
      };
      

      我希望它可以帮到你:D

      祝你好运!!

      例如,尝试将这段代码放入一个新项目并查看logcat:

      公共类MainActivity扩展了AppCompatActivity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      
          ArrayList<String> arrayList1 = new ArrayList<String>();
          arrayList1.add("ciao");
          arrayList1.add("come");
          arrayList1.add("stai");
      
          SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
          SharedPreferences.Editor editor = sharedPref.edit();
          editor.putInt("lengtArrayList", arrayList1.size());
          for (int i = 0; i < arrayList1.size(); i++) {
              editor.putString("p" + i, arrayList1.get(i));
          }
          editor.commit();
      
          ArrayList<String> arrayList2 = new ArrayList<String>();
          int size = sharedPref.getInt("lengtArrayList", 0);
          String defaultValue = "default String";
          for (int i = 0; i < size; i++) {
              String mString = sharedPref.getString("p" + i, defaultValue);
              Log.v("Preference = ", mString);
              arrayList2.add(mString);
              Log.v("Array = ", arrayList2.get(i));
          };
      }
      

      }