[NCMB] I searched the data store with mbaas

Purpose

-Previous article has registered the data store with NCMB, so let's search next.

Let's create a query

//Creating a query(Specify datastore class name)
final NCMBQuery<NCMBObject> query = new NCMBQuery<>("Testclass");
//In the message column of the data store"Dragon"Search for
query.whereEqualTo("message", "Dragon");

Let's search using a query

//data search
query.findInBackground(new FindCallback<NCMBObject>() {
    @Override
    public void done(List<NCMBObject> list, NCMBException e) {
        if(e != null){
            Toast.makeText(context, "Search error.", Toast.LENGTH_LONG).show();
        } else {
            if(list.isEmpty()){
                Toast.makeText(context, "Does not exist in the data store.", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(context, "Exists in the data store.", Toast.LENGTH_LONG).show();
            }
        }
    }
});

Operation check

スクリーンショット 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

Source

MainActivity.java


public class MainActivity extends AppCompatActivity {
    //Describe the API KEY acquired by NCMB
    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 initialization
        NCMB.initialize(this.getApplicationContext(),app_key,client_key);
        //Creating a query(Specify datastore class name)
        final NCMBQuery<NCMBObject> query = new NCMBQuery<>("Testclass");
        //In the message column of the data store"Dragon"Search for
        query.whereEqualTo("message", "Dragon");
        final Context context = getApplicationContext();
        Button button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //data search
                query.findInBackground(new FindCallback<NCMBObject>() {
                    @Override
                    public void done(List<NCMBObject> list, NCMBException e) {
                        if(e != null){
                            Toast.makeText(context, "Search error.", Toast.LENGTH_LONG).show();
                        } else {
                            if(list.isEmpty()){
                                Toast.makeText(context, "Does not exist in the data store.", Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(context, "Exists in the data store.", Toast.LENGTH_LONG).show();
                            }
                        }
                    }
                });
            }
        });
    }
}

Summary

Now that you can register and search, let's implement ranking next.

Recommended Posts

[NCMB] I searched the data store with mbaas
I want to distinct the duplicated data with has_many through
FactoryBot Register multiple data with the same model
I checked the number of taxis with Ruby
[Rails] I tried playing with the comment send button
I tried to get started with Spring Data JPA
I want to dark mode with the SWT app
I investigated the super that I met with the devise controller
twitter4j java Store the searched tweets in DB (MySQL).