Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java notifyDataSetChanged()赢得';是否使用DBHelper更新视图?_Java_Android - Fatal编程技术网

Java notifyDataSetChanged()赢得';是否使用DBHelper更新视图?

Java notifyDataSetChanged()赢得';是否使用DBHelper更新视图?,java,android,Java,Android,当我删除按钮时,以下代码会自动更新gridView public View getView(int position, View convertView, ViewGroup parent) { final Item item = getItem(position); if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.la

当我删除
按钮时,以下代码会自动更新
gridView

public View getView(int position, View convertView, ViewGroup parent) {
        final Item item = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_button, parent, false);
        }
        Button itemBtn = (Button)convertView.findViewById(R.id.itemBtn);
        itemBtn.setText(item.getName() + " (" + item.getPrice()+")");
        itemBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                     MainActivity.cart.addItem(item);
                }
        });

    itemBtn.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
                remove(item);
                notifyDataSetChanged();
            return true;
        }
    });
       itemBtn.setLongClickable(true);
        return convertView;
    }
}
但由于我还需要更新数据库,下面是我当前的代码。
视图
不会自动更新,我仍然需要更改选项卡以更新
网格视图
,并删除
按钮

itemBtn.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View view) {
        DBHelper dbHelper = new DBHelper(mActivity);
        if (dbHelper.removeData(item.getName(), item.getType()) > 1) {
            remove(item);
            notifyDataSetChanged();
        } else {
            Toast.makeText(mActivity, "No such item to delete", Toast.LENGTH_SHORT);
        }
        dbHelper.close();
        return true;
    }
});

为什么会这样

如果未更新数据,则在类或片段中创建一个方法,当长按时再次加载适配器,然后调用此方法,而不是notifydatasetchanged

已解决。如果语句
未运行,则代码在中。我以为我一直在检查Toast.makeText()
,并确信它确实在运行,因为Toast不会显示,直到我意识到我忘记调用
.show()
。对不起,我犯了个愚蠢的错误,我已经找到问题了

是否确实,dbHelper.removeData()返回的值大于1,因此实际上执行了if语句的主体?