Android阵列列表保存和调用最佳方法

时间:2017-01-25 11:36:14

标签: android arrays list

我有一个列表(3个列表),其中包含

  1. 姓名,电话,代码
  2. 想要保存数组调用,并在应用关闭后选择。
  3. 到目前为止,该代码创建了列表。

    final EditText et=(EditText)findViewById    (R.id.telephone_number_reference_entry);
    final EditText et1=(EditText)findViewById(R.id.telephone_number_entry);
    final EditText et2=(EditText)findViewById(R.id.access_code_entry);
    final ListView new_text_number=(ListView) findViewById(R.id.new_text_number);
    final ListView  telephone_number_reference=(ListView)findViewById(R.id.telephone_number_reference);
    final ListView  access_code_reference=(ListView)findViewById(R.id.access_code_reference);
    
    final ArrayList<String> list = new ArrayList<String>();
    final ArrayAdapter<String> aa;
    aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
    new_text_number.setAdapter(aa);
    
    final ArrayList<String> list2 = new ArrayList<String>();
    final ArrayAdapter<String> aa2;
    aa2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list2);
    telephone_number_reference.setAdapter(aa2);
    
    final ArrayList<String> list3 = new ArrayList<String>();
    final ArrayAdapter<String> aa3;
    aa3 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list3);
    access_code_reference.setAdapter(aa3);
    
    
    add_number_button.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View view) {
    
            if(et.getText().length()>0 && et.getText().length()>0 && et2.getText().length()>3 && et2.getText().length()<=7) {
    
                list.add(0,et.getText().toString());
                aa.notifyDataSetChanged();
                et.setText("");
                list2.add(0,et1.getText().toString());
                aa2.notifyDataSetChanged();
                et1.setText("");
                list3.add(0,et2.getText().toString());
                aa3.notifyDataSetChanged();
                et2.setText("");       }
    
        }
    });
    
  4. 感谢任何有关此问题的帮助,我目前将其他数据保存为共享pref

    SharedPreferences settings = getSharedPreferences(Settings_Pref, 0);
    

    这样可以正常工作,因为它可以更改它们并且可以调用它们,但需要进行列表,所有列表都是要调用的数据并用于不同的短信系统,而不是记住所有不同的系统并手动输入,希望能够回想起来。

    我不确定我是否正确地看待这一点,任何帮助tinyDB选项都可能会受到极大的欢迎。

    由于

1 个答案:

答案 0 :(得分:0)

您可以使用 GSON

保存数据

如果您使用Android Studio,则需要将以下依赖项添加到您的app build.gradle:

dependencies {
   ...
   compile 'com.google.code.gson:gson:2.8.0'
}

保存ArrayList:

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = sharedPrefs.edit();
Gson gson = new Gson();

String json = gson.toJson(arrayList);

editor.putString(YOUR_PREFERENCE_KEY, json);
editor.commit();

要读取ArrayList(请记住,您需要添加import java.lang.reflect.Type):

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Gson gson = new Gson();
String json = sharedPrefs.getString(YOUR_PREFERENCE_KEY, null);
Type type = new TypeToken<ArrayList<ArrayObject>>() {}.getType();
ArrayList<ArrayObject> arrayList = gson.fromJson(json, type);