自定义ArrayAdapter(数组列表)保存在SharePreferences

时间:2018-02-21 02:13:43

标签: java android arraylist sharedpreferences android-arrayadapter

我正在尝试在程序中保存数组列表。该列表包含用户访问过或加载到WebView中的URL。我已经做了一些研究,这是可能的但是在我读过的关于它的50个帖子中,它们似乎都没有用。我相信,如果我向CustomArrayAdapter添加一种方法,那么我将能够做到这一点,但考虑到我是一个新手,我的“导师”由于某种原因停止指导我,我无法弄明白。我尝试过创建一个SaveList和AddList方法,但即使花了2-3天处理这些方法,它们都以失败告终。

最终目标是调用onPause,它将保存列表,并且当调用onStart时,它将添加列表。如果用户清除列表,它唯一会出现空白的时间。提前感谢您的帮助,如果有更智能更有效的方法,请不要犹豫,站出来提出想法。最后,我希望有人审查该程序,并帮助使其“减少代码”,并在代码方面拥有更专业的布局。

如果这是一个重复的问题我也找不到有效的方法,如果我用错误的术语称“方法”我打算作为一个例子(public void saveLise(){ })。我刚刚开始弄清楚ActivityLifeCycle并在其中保存信息。

主要活动,

@覆盖

protected void onStart(){
    super.onStart();
    Log.d("lifecycle","onStart invoked");

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    Url = settings.getString("curURL", "");


    if(!Url.matches("https://www.google.ca")
            || !Url.matches("https://www.google.com")){
        mWebView.loadUrl(Url);
    }
    mWebView.loadUrl(urlHomepage);

}

@Override
protected void onPause() {
    super.onPause();
    Log.d("lifecycle","onPause invoked");
    Url = mWebView.getUrl();

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("curURL", Url);

    editor.apply();
}

CustomArray适配器:

public class UrlHistoryAdapter extends BaseAdapter {

private final Context context;
private final List<String> list = new ArrayList<>();

public UrlHistoryAdapter(Context context) {
    this.context = context;


}
public void addItem(String item) {
    this.list.add(item);
}
public void deleteItem(String item) {
    this.list.remove(item);
}
public void deleteList(){
   list.clear();
}

@Override
public int getCount() {
    return list.size();

}
@Override
public String getItem(int position) {
    return list.get(position);
}
@Override
public long getItemId(int position) {
    return position;
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        view = LayoutInflater.from(context).inflate(R.layout.preference_list_item, parent, false);
    }
    TextView textView = view.findViewById(R.id.title);
    textView.setText(getItem(position));

    return view;
}


}

1 个答案:

答案 0 :(得分:0)

从SharedPreference保存和检索ArrayList

        public static void addToPreference(String id,Context context) {
            SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.MyPreference, Context.MODE_PRIVATE);
            ArrayList<String> list = getListFromPreference(context);
            if (!list.contains(id)) {
                list.add(id);
                SharedPreferences.Editor edit = sharedPreferences.edit();
                Set<String> set = new HashSet<>();
                set.addAll(list);
                edit.putStringSet(Constant.LIST, set);
                edit.commit();

            }
        }
        public static ArrayList<String> getListFromPreference(Context context) {
            SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.MyPreference, Context.MODE_PRIVATE);
            Set<String> set = sharedPreferences.getStringSet(Constant.LIST, null);
            ArrayList<String> list = new ArrayList<>();
            if (set != null) {
                list = new ArrayList<>(set);
            }
            return list;
        }