LoginSignup
0
1

More than 5 years have passed since last update.

【NCMB】mbaasでデータストアを検索してみた

Last updated at Posted at 2019-01-29

目的

  • 前の記事にてNCMBでデータストアの登録が出来たので次は検索をやろう。

クエリを作成しよう

//クエリの作成(データストアのクラス名指定)
final NCMBQuery<NCMBObject> query = new NCMBQuery<>("Testclass");
//データストアのmessageカラムに"ドラゴン"があるか検索
query.whereEqualTo("message", "ドラゴン");

クエリを使って検索しよう

//データ検索
query.findInBackground(new FindCallback<NCMBObject>() {
    @Override
    public void done(List<NCMBObject> list, NCMBException e) {
        if(e != null){
            Toast.makeText(context, "検索エラー。", Toast.LENGTH_LONG).show();
        } else {
            if(list.isEmpty()){
                Toast.makeText(context, "データストアに存在しません。", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(context, "データストアに存在します。", Toast.LENGTH_LONG).show();
            }
        }
    }
});

動作確認

スクリーンショット 2019-01-30 0.38.42.png
Photo_19-01-30-00-41-11.241.png
Photo_19-01-30-00-41-09.358.png

ソース

MainActivity.java
public class MainActivity extends AppCompatActivity {
    //NCMBにて取得したAPIKEYを記載
    private final String app_key = "hogehoge";
    private final String client_key = "hogehoge";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //SDKの初期化
        NCMB.initialize(this.getApplicationContext(),app_key,client_key);
        //クエリの作成(データストアのクラス名指定)
        final NCMBQuery<NCMBObject> query = new NCMBQuery<>("Testclass");
        //データストアのmessageカラムに"ドラゴン"があるか検索
        query.whereEqualTo("message", "ドラゴン");
        final Context context = getApplicationContext();
        Button button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //データ検索
                query.findInBackground(new FindCallback<NCMBObject>() {
                    @Override
                    public void done(List<NCMBObject> list, NCMBException e) {
                        if(e != null){
                            Toast.makeText(context, "検索エラー。", Toast.LENGTH_LONG).show();
                        } else {
                            if(list.isEmpty()){
                                Toast.makeText(context, "データストアに存在しません。", Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(context, "データストアに存在します。", Toast.LENGTH_LONG).show();
                            }
                        }
                    }
                });
            }
        });
    }
}

まとめ

登録と検索が出来るようになったので、次はランキング実装しよう。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1