Android 开发Tips 之 Bmob操作

/*
    在项目中Bmob云数据库进行的数据操作
*/

/* top bar 清空按钮的事件处理*/
    public void deleteAllResult(View v) {
        /* | */
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确定清空所有结果?")
                .setCancelable(false)
                .setPositiveButton("清空", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        batchDelete(); // 实现清空
                        getResultData(); // 刷新结果
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                }).show();
    }

    /**
     * 批量删除
     */
    protected void batchDelete() {
        List queryBeans = new ArrayList<>();
        for (int i = 0; i < resultList.size(); i++) {
            queryBeans.add(resultList.get(i));
        }
        if (queryBeans.size() == 0) {
            showToast("清空失败");
            return;
        }
        new BmobObject().deleteBatch(this, queryBeans, new DeleteListener() {

            @Override
            public void onSuccess() {
                showToast("已清空所有的结果");
            }

            @Override
            public void onFailure(int arg0, String arg1) {
                showToast("清空失败");
            }
        });

    }

    // 获得测试结果集
    public void getResultData() {
        /* | */
        BmobQuery query = new BmobQuery<>();
        // 按测试时间由近到远排序
        query.order("-updateAt");
        query.findObjects(this, new FindListener() {
            @Override
            public void onSuccess(List list) {
                resultList = list;
                // 通知Adapter数据更新了
                mMyListAdapter.refresh((ArrayList) resultList);
                mMyListAdapter.notifyDataSetChanged();
                Log.i(TAG, "数据更新了");
            }

            @Override
            public void onError(int i, String s) {
                Log.e(TAG, "获取数据失败");
                showToast("获取数据失败");
            }
        });
    }

    private void showToast(String s) {
        Toast toast = Toast.makeText(this, s, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    @Override
    public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确定删除这条记录?")
                .setCancelable(false)
                .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 实现删除Item
                        itemDelete(position);
                        getResultData(); // 刷新结果
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                }).show();
        return true;
    }

    // 单条记录的删除
    private void itemDelete(int position) {
        TestResults testResult = new TestResults();
        testResult.delete(this, resultList.get(position).getObjectId(), new DeleteListener() {
            @Override
            public void onSuccess() {
                showToast("已清空所有的结果");
            }

            @Override
            public void onFailure(int i, String s) {
                showToast("删除失败 " + s);
            }
        });
    }

你可能感兴趣的:(bmob,云数据库,Android)