0

我正在使用这个水平列表视图,但是当我调用通知 notifyDataSetChanged() Mainactivity 类时它不会刷新视图

   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();

..

图像适配器类

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

如果我有一个按钮监听器来删除 imageadapter 类中的项目,它可以很好地从列表中删除项目并正确刷新

4

1 回答 1

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

您正在创建两个不同ImageAdapter的 s。通话不影响notifyDataSetChanged()对方。imageAdapterImageAdapter

我认为您打算这样做:

imageAdapter = new ImageAdapter(this, results);
listView.setAdapter(imageAdapter);
于 2013-07-19T01:57:14.207 回答