-Previous article has registered the data store with NCMB, so let's search next.
//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");
//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();
}
}
}
});
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();
}
}
}
});
}
});
}
}
Now that you can register and search, let's implement ranking next.
Recommended Posts