未调用NotifyDataSetChanged

时间:2016-09-02 13:43:53

标签: android android-arrayadapter notifydatasetchanged android-adapterview

我正在尝试使用卡片滑动来制作应用程序。我有一个带有一些文字的Texview,当我刷下一张卡时还有另一个文本等等。我还想做的是当我点击一个按钮去除屏幕上的卡片然后转到下一张卡片。我从列表中删除第一个String并调用adapter.notifyDataSetChange。问题是屏幕上的文字保持不变,即使每次点击都删除了字符串。我甚至搞了适配器中的值,它们被更改,卡的数量也在下降,但屏幕上的文字保持不变。

当我在onResponse中下载数据时,我调用,flingContainer是自定义视图,扩展了adapterView

myAppAdapter = new MyAppAdapter(list, this);
flingContainer.setAdapter(myAppAdapter);

当我点击按钮时,我打电话

list.remove(0);
myAppAdapter.notifyDataSetChanged();

这是我的适配器

public class MyAppAdapter extends BaseAdapter {

    public List<String> parkingList;
    public Context context;

    private MyAppAdapter(List<String> apps, Context context) {
        this.parkingList = apps;
        this.context = context;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View rowView = convertView;

        if (rowView == null) {

            LayoutInflater inflater = getLayoutInflater();
            rowView = inflater.inflate(R.layout.item, parent, false);
            viewHolder = new ViewHolder();
            ViewHolder.background = (FrameLayout) rowView.findViewById(R.id.background);
            viewHolder.cardImage = (TextView) rowView.findViewById(R.id.cardImage);

            rowView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.cardImage.setText(parkingList.get(position));

        return rowView;
    }
}

1 个答案:

答案 0 :(得分:1)

您致电list.remove(0),因此只需从列表中删除第一个索引,而不是从适配器中删除。实际上,您将列表副本传递到 Adapter 并且它们是不同的;所以要使它工作除了调用list.remove(0)在你的适配器中添加removeItem()

public void removeItem(int index){
      parkingList.remove(index);
      notifyDataSetChange();
}
相关问题