更改列表视图时,setEmptyView不会更新

时间:2013-07-19 02:46:09

标签: android listview

我正在使用this horizontal list view,如果我从listView适配器中没有项开始,则会显示emptyView,如果我在listView适配器中启动WITH项,它会隐藏emptyView,但我的问题是空视图将是从来没有显示我是否从项目开始并从listView适配器中删除所有项目,如果我没有项目并尝试将项目添加到listview适配器将保留在那里

Mainactivity

    @Override
protected void onCreate(Bundle savedInstanceState) { 

      listView = (HorizontalListView) findViewById(R.id.listView1);
      imageAdapter = new ImageAdapter(this, products);
      listView.setAdapter(imageAdapter);
      listView.setEmptyView(findViewById(R.id.empty_list_view));

...

private void loadProduct(Intent data) {

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

}

... imageadapter class

     public class ImageAdapter extends BaseAdapter {

...

public ImageAdapter(Context context, List<Product> products) {
    this.context = context;
    this.products = products;
}

// getView that displays the data at the specified position in the data set.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // create a new LayoutInflater
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View gridView;
    gridView = null;
    convertView = null;// avoids recycling of grid view
    if (convertView == null) {

        gridView = new View(context);
        // inflating grid view item
        gridView = inflater.inflate(R.layout.list_view_item, null);

        Product p = products.get(position);

...

        public void add(Product p) {
    products.add(p);
    // notify the list that the underlying model has changed
    System.out.println("product added");
    notifyDataSetChanged();
}


public void remove(int position) {

    products.remove(position);
    System.out.println("product removed");
    notifyDataSetChanged();
}

2 个答案:

答案 0 :(得分:1)

从ListView适配器中删除数据后,请务必调用notifyDataSetChanged()

答案 1 :(得分:1)

鉴于您已延长BaseAdapter,您可能需要确保Adapter.getCount()BaseAdapter.isEmpty()都已实施并返回您需要的值。