android不会从外部适配器刷新自定义列表视图

时间:2013-07-19 01:54:43

标签: android listview custom-adapter

我正在使用this horizontal list view但是当我调用notify notifyDataSetChanged()时它不会刷新视图 主动性等级

   oncreate{
    products = new ArrayList<Product>();
    listView = (HorizontalListView) findViewById(R.id.listView1);
    imageAdapter = new ImageAdapter(this, products);
    listView.setAdapter(new ImageAdapter(this, products));

...

private void loadProduct(Intent data) {

    Bundle extras = data.getExtras();
    Product p  = (Product)extras.getParcelable(PASSED_PRODUCT);

    //tried using imageAdapter.add(p) aswell but doesnt work either
   products.add(p);
   imageAdapter.notifyDataSetChanged();

...

ImageAdapter类

            public void add(Product p) {
    products.add(p);
            // notify the list that the underlying model has changed
    notifyDataSetChanged();
}

public void remove(int position) {

    products.remove(position);
    notifyDataSetChanged();
}

如果我有一个buttonlistener来删除imageadapter类中的项目,这会删除列表中的项目并正确刷新

1 个答案:

答案 0 :(得分:0)

imageAdapter = new ImageAdapter(this, results);
listView.setAdapter(new ImageAdapter(this, results));

您正在创建两个不同的ImageAdapter。在notifyDataSetChanged()上拨打imageAdapter不会影响其他ImageAdapter

我认为你打算这样做:

imageAdapter = new ImageAdapter(this, results);
listView.setAdapter(imageAdapter);