数据未动态插入ListView

时间:2017-07-22 02:37:19

标签: android listview sharedpreferences android-arrayadapter

我遇到的问题是我有一个按钮,它实际上是一个“收藏夹”按钮,用于保存来自webview sesson的URL,并将其添加到另一个名为favorites的类的列表视图中。它是通过共享偏好来实现的,因为缺乏找到更好的方法。我遇到的问题是当我尝试保存另一个URL时,它只会覆盖当前存在的URL并且不会添加另一行。我的带有按钮的webview代码是:

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_share) {
        shareURL();
        Toast.makeText(this, getString(R.string.share),
                Toast.LENGTH_SHORT).show();
    }
    if (id == R.id.action_favorite) {
        changeIcon();

        Snackbar.make(findViewById(android.R.id.content), "Saved for later", Snackbar.LENGTH_LONG).show();

        String url = getIntent().getDataString();

        SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
        SharedPreferences.Editor edit = prefs.edit();
        edit.putString("URL", url );
        edit.apply();

    }
    return super.onOptionsItemSelected(item);
}

//我的“收藏夹”类的代码如下:

    //LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems = new ArrayList<String>();

//DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
ArrayAdapter<String> adapter;

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

    ListView listView = (ListView) findViewById(R.id.favoritesList);

    adapter = new ArrayAdapter<String>(this,
            R.layout.custom_lv,
            listItems);
    listView.setAdapter(adapter);

    SharedPreferences bb = getSharedPreferences("my_prefs", 0);
    String m = bb.getString("URL", "");

    listItems.add(m);
    adapter.notifyDataSetChanged();

}

非常感谢任何帮助!

---编辑 -

使用FnR的代码,它仍然只在收藏夹列表视图中添加最新的“已保存”URL,更新的代码如下:

- 收藏夹代码 -

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

    ListView listView = (ListView) findViewById(R.id.favoritesList);

    adapter = new ArrayAdapter<String>(this,
            R.layout.custom_lv,
            listItems);
    listView.setAdapter(adapter);

    getListFromSharedPreferences();

}

private List<String> getListFromSharedPreferences() {
    SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
    Set<String> set = prefs.getStringSet("URL", null);
    List<String> urlList = new ArrayList<>();
    if (set != null) {
        urlList = new ArrayList<>(set);
        listItems.add(String.valueOf(set));
        adapter.notifyDataSetChanged();
    }
    Log.e("size",urlList.size()+"");
    return urlList;
}

- Webview代码 -

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_share) {
        shareURL();
        Toast.makeText(this, getString(R.string.share),
                Toast.LENGTH_SHORT).show();
    }
    if (id == R.id.action_favorite) {
        changeIcon();

        Snackbar.make(findViewById(android.R.id.content), "Saved for later", Snackbar.LENGTH_LONG).show();

        String url = getIntent().getDataString();

        putSharedPreferences(url);

    }
    return super.onOptionsItemSelected(item);
}

private void putSharedPreferences(String url) {
    SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
    SharedPreferences.Editor edit = prefs.edit();
    Set<String> set = prefs.getStringSet("URL", null);
    if (set != null) {
        set.add(url);
        edit.putStringSet("URL", set);
        edit.apply();
    }
}

2 个答案:

答案 0 :(得分:0)

您正在使用相同的SharedPreferences键,这意味着当您添加新网址时,您将覆盖现有的网址。您需要将网址存储在列表中。为此,您可以使用SharedPreferences.Editor.putStringSet。示例在这里 Save ArrayList to SharedPreferences

  private void putSharedPreferences(String url) {
    SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
   SharedPreferences.Editor edit = prefs.edit();
    Set<String> set = prefs.getStringSet("key", null);
    if (set != null) {
        set.add(url);
        edit.putStringSet("key", set);
        edit.apply();
    }
}

private List<String> getListFromSharedPreferences() {
    SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
    Set<String> set = prefs.getStringSet("key", null);
    List<String> urlList = new ArrayList<>();
    if (set != null) {
        urlList = new ArrayList<>(set);
    }
    Log.e("size",urlList.size()+"");
    return urlList;
}

答案 1 :(得分:0)

您需要做的是在添加到SharedPreference之前将以前保存的网址添加到新集。

private void putSharedPreferences(String url) {
  SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
  SharedPreferences.Editor edit = prefs.edit();
  Set<String> previousSet = prefs.getStringSet("key", null);
  Set<String> set = new HashSet<>();
  if (previousSet != null) {
    set.addAll(previousSet);
  }
  set.add(url);
  edit.putStringSet("key", set);
  edit.apply();
 }
}

将所有先前保存的数据添加到新HashSet背后的原因是修改返回的实例不会保证数据的一致性或根本不修改数据。您可以查看documentation here了解更多信息。