2013-07-19 60 views
1

我使用this horizontal list view,如果我在listView适配器中没有项目开始emptyView显示,如果我开始WITH项目在ListView适配器它隐藏emptyView,但我的问题是该空视图将永远不会显示,如果我开始与项目,并从ListView控件适配器删除所有项目,并会呆在那里,如果我开始没有项目,并尝试将项目添加到ListView适配器当listview被更改时,setEmptyView不会更新

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类

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

删除项目的代码不显示,是否删除项目后调用notifyDataSetChanged()? –

+0

是的,它和add方法一样,除了products.remove(p) – Thomas

回答

1

一定要调用notifyDataSetChanged()你删除数据后您的ListView适配器

1

鉴于哟你已经延长BaseAdapter,也许你需要确保Adapter.getCount()BaseAdapter.isEmpty()都实现并返回你需要的值。

相关问题