gpt4 book ai didi

android - 更改 ListView 时,setEmptyView 不会更新

转载 作者:行者123 更新时间:2023-11-29 21:38:36 24 4
gpt4 key购买 nike

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

主要 Activity

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

}

...图像适配器类

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

最佳答案

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

关于android - 更改 ListView 时,setEmptyView 不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17737337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com