首页 > 代码库 > (android)bmob中实现保存

(android)bmob中实现保存

1先查找出来

2保存当前表的数据

3更新关联表的数据

插入有关联的数据字段的时候,为了检测是否重复插入问题,

  /**
     * 查询是否存在已存在的名字
     */
    private void findNoteGroupInfoByName() {
        if (TextUtils.isEmpty(m_et_note_group_add_name.getText().toString().trim())) {
            ShowToast("名字不能为空");
            return;
        }
        BmobQuery<NoteGroup> noteGroups = new BmobQuery<NoteGroup>();
        noteGroups.addWhereRelatedTo("noteGroups", new BmobPointer(myUser));
        noteGroups.addWhereEqualTo("name", m_et_note_group_add_name.getText().toString());
        noteGroups.findObjects(this, new FindListener<NoteGroup>() {
            @Override
            public void onSuccess(List<NoteGroup> noteGroups) {
                if (noteGroups.size() == 0) {
                    saveNoteGroupInfo();
                } else {
                    ShowToast("已存在\"" + m_et_note_group_add_name.getText().toString() + "\"类型");
                }
            }
 
            @Override
            public void onError(int i, String s) {
                showErrorIms(i);
            }
        });
    }
 
    /**
     * 保存note组
     */
    private void saveNoteGroupInfo() {
        noteGroup = new NoteGroup();
        noteGroup.setName(m_et_note_group_add_name.getText().toString());
        noteGroup.setDescription(m_et_note_group_add_description.getText().toString());
        noteGroup.setUser(myUser);
        noteGroup.save(this, new SaveListener() {
            @Override
            public void onSuccess() {
                addNoteGroupToUser();
            }
 
            @Override
            public void onFailure(int i, String s) {
                showErrorIms(i);
            }
        });
    }
 
    /**
     * 把note组关联到User
     */
    private void addNoteGroupToUser() {
        BmobRelation noteGroups = new BmobRelation();
        noteGroups.add(noteGroup);
        myUser.setNoteGroups(noteGroups);
        myUser.update(this, new UpdateListener() {
            @Override
            public void onSuccess() {
                BmobQuery.clearAllCachedResults(getApplicationContext());
                ShowToast("保存\"" + m_et_note_group_add_name.getText().toString() + "\"成功");
            }
 
            @Override
            public void onFailure(int i, String s) {
                showErrorIms(i);
            }
        });
    }


(android)bmob中实现保存