Bmob中的几种查询方法


package com.kavinapps.androidk.my2webview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import org.json.JSONArray;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindCallback;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.GetListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 15-12-19.
 */
public class DataManager extends Activity{
    Button queryButton,updateButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_datamanager);
        //初始化 Bmob SDK,第一个参数为上下文,第二个参数为Application ID
        Bmob.initialize(this, "e9ae0dc754f7478146a81d9842991d8b");
        queryButton = (Button)findViewById(R.id.queryButton);
        updateButton = (Button)findViewById(R.id.updateButton);

        queryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                queryData();
            }
        });
        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateData();
            }
        });
    }
    /*
        * 查询数据
        * */
    public void queryData(){
        BmobQuery query = new BmobQuery("Lost");
        query.findObjects(this, new FindCallback() {
            @Override
            public void onSuccess(JSONArray jsonArray) {
                //注意:查询结果是JSONArray
                showToast("查询成功:" + jsonArray.length());
            }

            @Override
            public void onFailure(int i, String s) {
                showToast("查询失败:" + s);
            }
        });
    }
    //更新数据
    //修改数据只能通过objectId来修改,目前不能通过查询找objectId,然后修改的方法。
    public void updateData(){
        Lost lost = new Lost();
        lost.setDescribe("15206771922");
        lost.update(this, "96063af175", new UpdateListener() {
            @Override
            public void onSuccess() {
                showToast("更新成功");
            }

            @Override
            public void onFailure(int i, String s) {
                showToast("更新失败" + s);
            }
        });
    }
    //查询单条数据
    public void querySingleData(){
        BmobQuery query = new BmobQuery();
        query.getObject(this, "96063af175", new GetListener() {

            @Override
            public void onSuccess(Lost object) {
                // TODO Auto-generated method stub
                showToast("查询成功:");
                //获得数据的objectId信息
                object.getObjectId();
            }

            @Override
            public void onFailure(int code, String arg0) {
                // TODO Auto-generated method stub
                showToast("查询失败:" + arg0);
            }

        });
    }
    //查询多条数据
    //根据列值查询
    public void queryMultiData(){
        BmobQuery query = new BmobQuery();
        //查询playerName叫“比目”的数据
        query.addWhereEqualTo("describe", "15206771922");
        //返回50条数据,如果不加上这条语句,默认返回10条数据
        query.setLimit(50);
        //执行查询方法
        query.findObjects(this, new FindListener() {
            @Override
            public void onSuccess(List object) {
                // TODO Auto-generated method stub
                showToast("查询成功:共" + object.size() + "条数据。");
                for (Lost Lost : object) {
                    //获得数据的objectId信息
                    Lost.getObjectId();
                    //获得createdAt数据创建时间(注意是:createdAt,不是createAt)
                    Lost.getCreatedAt();
                }
            }

            @Override
            public void onError(int code, String msg) {
                // TODO Auto-generated method stub
                showToast("查询失败:" + msg);
            }
        });
    }
    //查询指定列
    public void queryRow(){
        //只返回Lost表的objectId这列的值
        BmobQuery bmobQuery = new BmobQuery();
        bmobQuery.addQueryKeys("objectId");
        bmobQuery.findObjects(this, new FindListener() {
            @Override
            public void onSuccess(List object) {
                // TODO Auto-generated method stub
                showToast("查询成功:共" + object.size() + "条数据。");
                //注意:这里的Lost对象中只有指定列的数据。
            }
            @Override
            public void onError(int code, String msg) {
                // TODO Auto-generated method stub
                showToast("查询失败:" + msg);
            }
        });
    }
    //对数据进行排序
    public void orderData(){
        // 根据score字段升序显示数据
        //query.order("score");
        // 根据score字段降序显示数据
        //query.order("-score");
    }

    //Toast
    public void showToast(CharSequence text ){
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
}



你可能感兴趣的:(android,中级)