1

I set up a database in Backendless and started running it on Android Studio. The app is able to access the database and the connection is stable. But when I run the app, it only returns one result from the database. Is it possible to loop through all the items in the database? I built the app using this tutorial: https://www.youtube.com/watch?v=KeYC6gJyt-k&t=1524s&ab_channel=JohanJurrius Here is my code.

package com.mbass.examples;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.backendless.Backendless;
import com.backendless.IDataStore;
import com.backendless.async.callback.AsyncCallback;
import com.backendless.exceptions.BackendlessFault;
import com.backendless.persistence.DataQueryBuilder;

import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {
    private TextView status;
    private TextView listtext;
    private Button updateButton;
    List<VIdeos> videos;

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

        Backendless.setUrl(Defaults.SERVER_URL);
        Backendless.initApp(getApplicationContext(), Defaults.APPLICATION_ID, Defaults.API_KEY);

        status = (TextView) findViewById(R.id.status);
        updateButton = (Button) findViewById(R.id.update_button);
        updateButton.setEnabled(false);
        final int[] textViews = {R.id.textView1, R.id.textView2, R.id.textView3};
        String whereClause = "name != 'Bill'";
        DataQueryBuilder queryBuilder = DataQueryBuilder.create();
        queryBuilder.setWhereClause(whereClause);
        queryBuilder.setPageSize(10).setOffset(0);
        queryBuilder.setSortBy("name");


        Backendless.Persistence.of(VIdeos.class).find(queryBuilder, new AsyncCallback<List<VIdeos>>() {
            @Override
            public void handleResponse(List<VIdeos> response) {
                for (int i = 0; i < response.size(); i++) {
                    Toast.makeText(MainActivity.this, "Name: " + response.get(i).getName() + "\n" +
                            "ID: " + response.get(i).getCreated(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void handleFault(BackendlessFault fault) {
                Toast.makeText(MainActivity.this, fault.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }
}

                                    
David A
  • 11
  • 1
  • 2

2 Answers2

0

What happens when you comment out this line?

queryBuilder.setWhereClause(whereClause);
JonR85
  • 700
  • 4
  • 12
  • The app only sends one result from the database. – David A Oct 11 '21 at 21:41
  • I'm assuming when you say 'returns from the database'. You are refereeing to the foreach toast message? Add System.out.println(response.get(i).getName()) in your foreach loop. – JonR85 Oct 12 '21 at 00:22
  • Also, how many entries are in your database? (A sane person could also use the debugger to see what's in 'response') – JonR85 Oct 12 '21 at 00:23
  • 1. There are two entries in my database. 2. I applied the toast method because the tutorial used it. – David A Oct 12 '21 at 01:11
  • Is one of them named bill? Add a few more. – JonR85 Oct 12 '21 at 01:36
0

Your code sets the page size to 10 objects with the following line of code: queryBuilder.setPageSize(10).setOffset(0)

The response is an array containing 10 objects. If you need more objects, change the page size. The maximum page size is 100

Mark Piller
  • 1,046
  • 1
  • 7
  • 6