代码之家  ›  专栏  ›  技术社区  ›  Matty F

如何从通用适配器调用notifyDataSetChanged()。

  •  4
  • Matty F  · 技术社区  · 16 年前

    OnItemClickListener 对于一个 ListView 有以下方法:

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id)
    

    我想把适配器放在 列表视图 刷新,我相信这是通过使用:

    BaseAdapter.notifyDataSetChanged()
    

    我怎么用 parent 中的参数 onItemClick 这样做的方法?到目前为止,我试过:

    parent.getAdapter().notifyDataSetChanged();
    

    这将引发错误,因为返回的对象属于类 HeaderViewListAdapter ,由于未知原因,它不是 BaseAdapter .

    5 回复  |  直到 12 年前
        1
  •  2
  •   Karan    16 年前

    afaik,headerListView中没有用于数据刷新/重新加载的任何方法。我能想到的唯一方法是重新分配适配器。

        2
  •  5
  •   Noah    15 年前

    您还可以通过自定义适配器访问baseadapter,如下代码片段所示:

    public class HeaderViewListAdapter extends BaseAdapter {
    
    ...
    
      @Override
      public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
      }
    
    ...
    }
    
        3
  •  5
  •   ruslanys    12 年前

    这将引发错误,因为返回的对象属于类 HeaderView列表适配器,由于未知原因,它不是的子类 基本适配器。

    ((BaseAdapter) ((HeaderViewListAdapter)listView.getAdapter()).getWrappedAdapter()).notifyDataSetChanged();
    
        4
  •  2
  •   John Raymund    15 年前

    使用光标时,不需要调用任何方法来刷新适配器客户机。 相反,在任何操作之后,您必须使用 getContentResolver().notifyChange(uri, null);

    例如:

    context.getContentResolver().update(SomeURI.CONTENT_URI, values, null,null);
    context.getContentResolver().notifyChange(SomeURI.CONTENT_URI, null);
    

    你的 viewclient (Listview, Gridview, Whatever) 即使您使用的是自定义适配器,也会自动更改。 希望这有帮助。

        5
  •  0
  •   Christian Specht    15 年前

    我找到了更简单的方法…打电话 this.onCreate(null) 这将重新加载完整的创建方法,但活动保持不变。