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

Bmob移动后端云服务平台--Android从零开始--(三)Bmob数据操作((批量)增删改)

时间:2014-12-25 10:04:46      阅读:342      评论:0      收藏:0      [点我收藏+]

标签:bmob   批量   增加   修改   删除   

Bmob移动后端云服务平台--Android从零开始--(三)Bmob数据操作((批量)增删改)

上篇文章简单的利用Bmob实现了用户注册与登录效果(http://blog.csdn.net/a123demi/article/details/42059621)。

在项目中,对数据的操作脱离不了增删改查。Bmob其实也一样,他对数据操作的增删改查做了很好的封装,以及其批量的处理。本文将重点讲解利用Bmob实现Android端与Bmob服务端的数据操作,实现增加、修改、删除以及他们的批量处理。

源码路径:http://download.csdn.net/detail/a123demi/8296591

代码执行效果:

技术分享


具体内容如下:

一、准备工作:定义继承于BmobObject的实体类

在进行数据操作之前,首先我们得定义一个继承于BmobObject的实体。因为Bmob基本上是通过这个实体来对数据进行增删改查的操作。本文将建立一个关于用户信息的实体类,如下:UserBean.java

<span style="font-family:SimSun;font-size:18px;">package com.example.bmobdatademo.bean;

import cn.bmob.v3.BmobObject;

/**
 * 用户信息实体类
 * @author 
 *
 */
public class UserBean extends BmobObject {
	private static final long serialVersionUID = 762796567708784658L;
	
	private String userId;
	private String password;
	private String userName;
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
}</span>


二、数据增加

1.单个对象增加

Bmob实现单个对象增加的方法其实很简单,就是利用BmobObject自带的save(Context context , SaveListener saveListener)方法

<span style="font-family:SimSun;font-size:18px;"><span style="white-space:pre">		</span>userBean.save(this, new SaveListener() {
			
			@Override
			public void onSuccess() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onFailure(int arg0, String arg1) {
				// TODO Auto-generated method stub
				
			}
		});</span>

2.批量增加

Bmob对于批量增加的方法为insertBatch(Context context,List<BmobObject> list,SaveListener saveListener),

<span style="font-family:SimSun;font-size:18px;">		new BmobObject().insertBatch(this, userBeans, new SaveListener() {
			
			@Override
			public void onSuccess() {
				
			}
			
			@Override
			public void onFailure(int arg0, String arg1) {
				
			}
		});</span>

注意:其中userBeans是对象BmobObject的集合

具体代码如下:

package com.example.bmobdatademo.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

import com.example.bmobdatademo.R;
import com.example.bmobdatademo.bean.UserBean;
import com.example.bmobdatademo.utils.ToastUtil;

public class AddActivity extends Activity {

	private EditText userIdEt, userNameEt, passwordEt;
	private Button addBtn, batchAddBtn,queryBtn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_data_add);
		
		initView();

	}

	private void initView() {
		userIdEt = (EditText) this.findViewById(R.id.id_user_id_et);
		userNameEt = (EditText) this.findViewById(R.id.id_user_name_et);
		passwordEt = (EditText) this.findViewById(R.id.id_password_et);
		addBtn = (Button) this.findViewById(R.id.id_add_btn);
		batchAddBtn = (Button) this.findViewById(R.id.id_batch_btn);
		queryBtn = (Button) this.findViewById(R.id.id_query_add_btn);

		addBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				add();
			}
		});

		batchAddBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				batchAdd();
			}
		});
		
		queryBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				query();
			}
		});

	}

	/**
	 * 批量增加
	 */
	protected void batchAdd() {
		List<BmobObject> userBeans = initUserBeans(5);

		new BmobObject().insertBatch(this, userBeans, new SaveListener() {

			@Override
			public void onSuccess() {
				ToastUtil.toast(AddActivity.this, "批量增加成功");
			}

			@Override
			public void onFailure(int arg0, String arg1) {
				ToastUtil.toast(AddActivity.this, arg1 + "批量增加失败");
			}
		});

	}

	/**
	 * 增加
	 */
	protected void add() {
		UserBean userBean = (UserBean) initUserBeans(1).get(0);

		userBean.save(this, new SaveListener() {

			@Override
			public void onSuccess() {
				ToastUtil.toast(AddActivity.this, "增加成功");

			}

			@Override
			public void onFailure(int arg0, String arg1) {
				ToastUtil.toast(AddActivity.this, arg1 + "增加失败");

			}
		});
	}

	/**
	 * 初始化实体对象
	 * 
	 * @param count
	 * @return
	 */
	private List<BmobObject> initUserBeans(int count) {
		List<BmobObject> userBeans = new ArrayList<BmobObject>();
		for (int i = 0; i < count; i++) {
			UserBean userBean = new UserBean();
			userBean.setUserId(userIdEt.getText().toString() + i);
			userBean.setUserName(userNameEt.getText().toString() + i);
			userBean.setPassword(passwordEt.getText().toString());

			userBeans.add(userBean);
		}
		return userBeans;
	}
	

	/**
	 * 修改
	 */
	protected void query() {
		BmobQuery<UserBean> query = new BmobQuery<>();

		query.findObjects(this, new FindListener<UserBean>() {

			@Override
			public void onError(int arg0, String arg1) {
				ToastUtil.toast(AddActivity.this, arg1 + "查询失败");
				
			}

			@Override
			public void onSuccess(List<UserBean> userBeanList) {
				// TODO Auto-generated method stub
				ToastUtil.toast(AddActivity.this, showMsg(userBeanList));
				
			}
		});
	}
	
	/**
	 * 弹出提示
	 * @param userBmobObjects
	 * @return
	 */
	private String showMsg(List<UserBean> userBmobObjects){
		String msg = "";
		if(userBmobObjects.size() == 0){
			msg = "数据不存在!";
		}
		for(UserBean userBean:userBmobObjects){
			msg += userBean.getObjectId() + "," + userBean.getUserId() + "," + userBean.getUserName() + "," 
					+ userBean.getPassword() + "\n";
		}
		return msg;
	}
}


三、 数据修改

1.单个对象修改

Bmob实现单个对象修改方法

update(Context context,UpdateListener updateListener)默认修改Bmob服务器第一行元素

update(Context context,String objectId,UpdateListener updateListener) 根据objectId修改指定元素 

<span style="font-family:SimSun;font-size:18px;">userBean.update(this,userBean.getObjectId(), new UpdateListener() {
			
			@Override
			public void onSuccess() {
				ToastUtil.toast(UpdateActivity.this, "修改成功");

			}

			@Override
			public void onFailure(int arg0, String arg1) {
				ToastUtil.toast(UpdateActivity.this, arg1 + "修改失败");

			}
		});</span>

2.批量修改

bmob实现批量修改方法udpateBatch(Context context,List<BmobObject> list,UpdateListener updateListener)

<span style="font-family:SimSun;font-size:18px;">new BmobObject().updateBatch(this, queryBeans, new UpdateListener() {

			@Override
			public void onSuccess() {
				ToastUtil.toast(UpdateActivity.this, "批量修改成功");
			}

			@Override
			public void onFailure(int arg0, String arg1) {
				ToastUtil.toast(UpdateActivity.this, arg1 + "批量修改失败");
			}
		});</span>

修改具体代码:

<span style="font-family:SimSun;font-size:18px;">package com.example.bmobdatademo.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

import com.example.bmobdatademo.R;
import com.example.bmobdatademo.bean.UserBean;
import com.example.bmobdatademo.utils.ToastUtil;

public class UpdateActivity extends Activity {

	private EditText userIdEt, userNameEt, passwordEt;
	private Button updateBtn, batchUpdateBtn,queryUpdateBtn;
	
	private List<BmobObject> queryBeans = new ArrayList<BmobObject>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_data_update);
		
		initView();

	}

	private void initView() {
		userIdEt = (EditText) this.findViewById(R.id.id_user_id_update_et);
		userNameEt = (EditText) this.findViewById(R.id.id_user_name_update_et);
		passwordEt = (EditText) this.findViewById(R.id.id_password_update_et);
		updateBtn = (Button) this.findViewById(R.id.id_update_btn);
		queryUpdateBtn = (Button) this.findViewById(R.id.id_query_update_btn);
		batchUpdateBtn = (Button) this.findViewById(R.id.id_batch_update_btn);

		updateBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				update();
			}
		});

		batchUpdateBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				batchUpdate();
			}
		});
		
		queryUpdateBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				query();
			}
		});

	}

	/**
	 * 批量修改
	 */
	protected void batchUpdate() {
		
		if(queryBeans.size() == 0){
			ToastUtil.toast(this, "数据不存在,请先插入");
		<span style="white-space:pre">	</span>return;}
		
		//修改数据
		for(int i = 0; i < queryBeans.size(); i++){
			UserBean userBean =(UserBean) queryBeans.get(i);
			userBean.setUserId(userIdEt.getText().toString() + i);
			userBean.setUserName(userNameEt.getText().toString() + i);
			userBean.setPassword(passwordEt.getText().toString());
		}
		
		new BmobObject().updateBatch(this, queryBeans, new UpdateListener() {

			@Override
			public void onSuccess() {
				ToastUtil.toast(UpdateActivity.this, "批量修改成功");
			}

			@Override
			public void onFailure(int arg0, String arg1) {
				ToastUtil.toast(UpdateActivity.this, arg1 + "批量修改失败");
			}
		});

	}

	/**
	 * 修改
	 */
	protected void update() {
		if(queryBeans.size() == 0){
			ToastUtil.toast(this, "无数据,请先插入!");
			return;
		}
		UserBean userBean = (UserBean)queryBeans.get(0);
		
		userBean.setUserId(userIdEt.getText().toString() + "33333");
		userBean.setUserName(userNameEt.getText().toString());
		userBean.setPassword(passwordEt.getText().toString());
		
		/**
		 * 根据指定的objectId修改元素
		 */
		userBean.update(this,userBean.getObjectId(), new UpdateListener() {
			
			@Override
			public void onSuccess() {
				ToastUtil.toast(UpdateActivity.this, "修改成功");

			}

			@Override
			public void onFailure(int arg0, String arg1) {
				ToastUtil.toast(UpdateActivity.this, arg1 + "修改失败");

			}
		});
		
		/**
		 * 默认修改Bmob服务器第一行元素
		 */
//		userBean.update(this , new UpdateListener() {
//			
//			@Override
//			public void onSuccess() {
//				// TODO Auto-generated method stub
//				ToastUtil.toast(UpdateActivity.this, "修改成功");
//			}
//			
//			@Override
//			public void onFailure(int arg0, String arg1) {
//				// TODO Auto-generated method stub
//				ToastUtil.toast(UpdateActivity.this, arg1 + "修改失败");
//			}
//		});
	}
	
	/**
	 * 修改
	 */
	protected void query() {
		BmobQuery<UserBean> query = new BmobQuery<>();

		query.findObjects(this, new FindListener<UserBean>() {

			@Override
			public void onError(int arg0, String arg1) {
				ToastUtil.toast(UpdateActivity.this, arg1 + "查询失败");
				
			}

			@Override
			public void onSuccess(List<UserBean> userBeanList) {
				// TODO Auto-generated method stub
				ToastUtil.toast(UpdateActivity.this, showMsg(userBeanList));
				convertUserToObject(userBeanList);
				
			}
		});
	}
	
	/**
	 * 弹出提示
	 * @param userBmobObjects
	 * @return
	 */
	private String showMsg(List<UserBean> userBmobObjects){
		String msg = "";
		for(UserBean userBean:userBmobObjects){
			msg += userBean.getObjectId() + "," + userBean.getUserId() + "," + userBean.getUserName() + "," 
					+ userBean.getPassword() + "\n";
		}
		return msg;
	}
	
	/**
	 * 将子类集合转换为基类BmobObject集合
	 * @param userBeanList
	 * @return
	 */
	private List<BmobObject> convertUserToObject(List<UserBean> userBeanList){
		queryBeans.clear();
		for(UserBean userBean: userBeanList){
			queryBeans.add(userBean);
		}
		
		return queryBeans;
	}

}
</span>

四、数据删除

1.单个对象删除

Bmob实现单个对象删除方法

delete(Context context,DeleteListener deleteListener)默认删除Bmob服务器第一行元素

delete(Context context,String objectId,DeleteListenerdeleteListener) 根据objectId删除指定元素 

<span style="font-family:SimSun;font-size:18px;">userBean.delete(this,userBean.getObjectId(), new DeleteListener() {
			
			@Override
			public void onSuccess() {
				ToastUtil.toast(DeleteActivity.this, "删除成功");

			}

			@Override
			public void onFailure(int arg0, String arg1) {
				ToastUtil.toast(DeleteActivity.this, arg1 + "删除失败");

			}
		});</span>


2.批量删除

bmob实现批量删除方法deleteBatch(Context context,List<BmobObject> list,DeleteListenerdeleteListener)

<span style="font-family:SimSun;font-size:18px;">		new BmobObject().deleteBatch(this, queryBeans, new DeleteListener() {

			@Override
			public void onSuccess() {
				ToastUtil.toast(DeleteActivity.this, "批量删除成功");
			}

			@Override
			public void onFailure(int arg0, String arg1) {
				ToastUtil.toast(DeleteActivity.this, arg1 + "批量删除失败");
			}
		});</span>

删除的具体代码:

<span style="font-family:SimSun;font-size:18px;">package com.example.bmobdatademo.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.DeleteListener;
import cn.bmob.v3.listener.FindListener;

import com.example.bmobdatademo.R;
import com.example.bmobdatademo.bean.UserBean;
import com.example.bmobdatademo.utils.ToastUtil;

public class DeleteActivity extends Activity {

	private Button deleteBtn, batchdeleteBtn,querydeleteBtn;
	
	private List<BmobObject> queryBeans = new ArrayList<BmobObject>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_data_delete);
		
		initView();

	}

	private void initView() {
		deleteBtn = (Button) this.findViewById(R.id.id_delete_btn);
		querydeleteBtn = (Button) this.findViewById(R.id.id_query_delete_btn);
		batchdeleteBtn = (Button) this.findViewById(R.id.id_batch_delete_btn);

		deleteBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				delete();
			}
		});

		batchdeleteBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				batchDelete();
			}
		});
		
		querydeleteBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				query();
			}
		});

	}

	/**
	 * 批量删除
	 */
	protected void batchDelete() {
		
		if(queryBeans.size() == 0){
			ToastUtil.toast(this, "数据不存在,请先插入");
		<span style="white-space:pre">	return;</span>}
		
		
		new BmobObject().deleteBatch(this, queryBeans, new DeleteListener() {

			@Override
			public void onSuccess() {
				ToastUtil.toast(DeleteActivity.this, "批量删除成功");
			}

			@Override
			public void onFailure(int arg0, String arg1) {
				ToastUtil.toast(DeleteActivity.this, arg1 + "批量删除失败");
			}
		});

	}

	/**
	 * 删除
	 */
	protected void delete() {
		if(queryBeans.size() == 0){
			ToastUtil.toast(this, "无数据,请先插入!");
			return;
		}
		UserBean userBean = (UserBean)queryBeans.get(0);
		
		/**
		 * 根据指定的objectId修改元素
		 */
		userBean.delete(this,userBean.getObjectId(), new DeleteListener() {
			
			@Override
			public void onSuccess() {
				ToastUtil.toast(DeleteActivity.this, "删除成功");

			}

			@Override
			public void onFailure(int arg0, String arg1) {
				ToastUtil.toast(DeleteActivity.this, arg1 + "删除失败");

			}
		});
		
		/**
		 * 默认删除Bmob服务器第一行元素
		 */
//		userBean.delete(this , new deleteListener() {
//			
//			@Override
//			public void onSuccess() {
//				// TODO Auto-generated method stub
//				ToastUtil.toast(deleteActivity.this, "删除成功");
//			}
//			
//			@Override
//			public void onFailure(int arg0, String arg1) {
//				// TODO Auto-generated method stub
//				ToastUtil.toast(deleteActivity.this, arg1 + "删除失败");
//			}
//		});
	}
	
	/**
	 * 修改
	 */
	protected void query() {
		BmobQuery<UserBean> query = new BmobQuery<>();

		query.findObjects(this, new FindListener<UserBean>() {

			@Override
			public void onError(int arg0, String arg1) {
				ToastUtil.toast(DeleteActivity.this, arg1 + "查询失败");
				
			}

			@Override
			public void onSuccess(List<UserBean> userBeanList) {
				// TODO Auto-generated method stub
				ToastUtil.toast(DeleteActivity.this, showMsg(userBeanList));
				convertUserToObject(userBeanList);
				
			}
		});
	}
	
	/**
	 * 弹出提示
	 * @param userBmobObjects
	 * @return
	 */
	private String showMsg(List<UserBean> userBmobObjects){
		String msg = "";
		if(userBmobObjects.size() == 0){
			msg = "数据不存在!";
		}
		for(UserBean userBean:userBmobObjects){
			msg += userBean.getObjectId() + "," + userBean.getUserId() + "," + userBean.getUserName() + "," 
					+ userBean.getPassword() + "\n";
		}
		return msg;
	}
	
	/**
	 * 将子类集合转换为基类BmobObject集合
	 * @param userBeanList
	 * @return
	 */
	private List<BmobObject> convertUserToObject(List<UserBean> userBeanList){
		queryBeans.clear();
		for(UserBean userBean: userBeanList){
			queryBeans.add(userBean);
		}
		
		return queryBeans;
	}

}
</span>

以上就是本文Bmob对数据进行(批量)增删改的所有内容,下一篇我们将讲解Bmob如何进行查询的。


Bmob移动后端云服务平台--Android从零开始--(三)Bmob数据操作((批量)增删改)

标签:bmob   批量   增加   修改   删除   

原文地址:http://blog.csdn.net/a123demi/article/details/42059623

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