码迷,mamicode.com
首页 > 移动开发 > 详细

(android)bmob中实现保存

时间:2015-01-08 21:44:31      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:

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中实现保存

标签:

原文地址:http://my.oschina.net/moziqi/blog/365284

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!