【问题标题】:Unable to refresh ListView with SimpleAdapter无法使用 SimpleAdapter 刷新 ListView
【发布时间】:2023-03-22 14:11:01
【问题描述】:

我正在尝试向我的列表视图中添加一个新条目,并使用仍显示在列表视图中的旧条目刷新它。以前我使用的是 ArrayAdapter,我可以在使用

添加新条目后刷新它
 adapter.notifyDataSetChanged();

但我无法将上面的代码与 SimpleAdapter 一起使用。有什么建议吗? 我已经尝试了几种解决方案,但到目前为止都没有奏效。

以下是我正在使用的代码,它没有添加条目:

void beta3 (String X, String Y){
    //listview in the main activity
    ListView POST = (ListView)findViewById(R.id.listView);
    //list = new ArrayList<String>();
    String data = bar.getText().toString();
    String two= data.replace("X", "");
    ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();

    HashMap<String,String> event = new HashMap<String, String>();
    event.put(Config.TAG_one, X);
    event.put(Config.TAG_two, two);
    event.put(Config.TAG_three, Y);
    list.add(event);
    ListAdapter adapter = new SimpleAdapter(this, list, R.layout.list,
            new String[]{Config.TAG_one, Config.TAG_two, Config.TAG_three},
            new int[]{R.id.one, R.id.two, R.id.three});        
    POST.setAdapter(adapter);
}

【问题讨论】:

  • 似乎是 this 的副本。您如何尝试将项目添加到列表中?

标签: android listview simpleadapter


【解决方案1】:

如果您的beta3 方法确实是您向ListView 添加 条目的功能:它会在您每次调用它时设置一个带有新列表的新适配器。因此,这将始终导致 ListView 包含一个条目。退出beta3 方法后,对列表的引用就消失了。

您必须重用list 实例变量。将ArrayList&lt;HashMap&lt;String,String&gt;&gt; list 放入类/活动范围并初始化它一次(例如在onCreate() 中)。

另一个问题是您使用ListAdapter 变量作为对SimpleAdapter 实例的引用。 ListAdapter 是一个不提供 notifyDataSetChanged 方法的接口。您应该改用 SimpleAdapter 变量。

这是一个例子:

public class MyActivity {

    ArrayList<HashMap<String,String>> list;
    SimpleAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        ListView POST = (ListView)findViewById(R.id.listView);
        list = new ArrayList<HashMap<String, String>>();
        adapter = new SimpleAdapter(this, list, R.layout.list,
            new String[]{Config.TAG_one, Config.TAG_two, Config.TAG_three},
            new int[]{R.id.one, R.id.two, R.id.three});        
        POST.setAdapter(adapter);
    }


    void beta3 (String X, String Y){
        String two = ""; // adapt this to your needs
        ...
        HashMap<String,String> event = new HashMap<String, String>();
        event.put(Config.TAG_one, X);
        event.put(Config.TAG_two, two);
        event.put(Config.TAG_three, Y);
        list.add(event);
        adapter.notifyDataSetChanged();
    }

}

【讨论】:

  • 我的项目从 android 手机中的数据库中获取数据/字符串并将数据/字符串传递给 beta3() 并在列表视图中显示。你能给我一些示例代码以便我可以使用它吗?
  • 我已经试过你的代码了。我在 notifyDataSetChanged() 部分有错误。没有它,应用程序正在运行,但列表视图上没有显示任何内容。
  • notifyDataSetChanged() 显示为红色。当鼠标悬停时显示无法解析方法'notifyDataSetChanged()'
  • ListAdapter 替换为SimpleAdapter。代码示例已更正。
  • 我已经检查并将 ListAdapter 替换为 SimpleAdapter,但我现在使用 notifyDataSetChanged() 获取 java.lang.NullPointerException
猜你喜欢
  • 2019-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多