(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);
            }
        });
    }


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。