【问题标题】:Custom ArrayAdapter (Array list) saved on SharePreferences保存在 SharedPreferences 上的自定义 ArrayAdapter (Arraylist)
【发布时间】:2018-07-31 12:26:35
【问题描述】:

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

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

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

主要活动,

@Override

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;
}


}

【问题讨论】:

  • 您的要求是在 SharedPreferences 中保存 arraylist..?
  • @Tarunkonda 是的

标签: java android arraylist sharedpreferences android-arrayadapter


【解决方案1】:

从 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;
        }

【讨论】:

  • 我将如何在什么文件和位置实现这一点
  • 感谢您的反馈
猜你喜欢
  • 1970-01-01
  • 2016-10-01
  • 2017-12-28
  • 2018-09-16
  • 2013-02-05
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
  • 2011-10-26
相关资源
最近更新 更多