2

我有这个代码Button点击

我正在从中删除项目,ListView但问题是它仍然显示在适配器上。

restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getUrl());
                     restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getName());
    DisplayDataAdapter adapter = new DisplayDataAdapter(getApplicationContext());           
    adapter.notifyDataSetChanged();
     listView.setAdapter(adapter);

任何想法可能是项目没有从对象类中删除的问题ArrayList

谢谢

4

3 回答 3

2

切换顺序:

adapter.notifyDataSetChanged(); 
listView.setAdapter(adapter);

至:

listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
于 2013-09-28T18:59:03.120 回答
1

It seems like you are creating a new Adapter every time you change your dataset and set this new Adapter to your ListView. You don't have to do that. You can just make changes to your dataset and then call notifyDataSetChanged() on your Adapter.

So your code should look like this:

restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getUrl()); 
restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getName());
adapter.notifyDataSetChanged();     
于 2013-09-28T19:07:29.593 回答
1

这是问题

 DisplayDataAdapter adapter = new DisplayDataAdapter(getApplicationContext()); 

您正在创建一个新的适配器。创建一个全局值适配器并尝试如下:

restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getUrl());
                 restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getName());
 adapter.notifyDataSetChanged();
于 2013-09-28T21:04:26.800 回答