Search in sources :

Example 1 with QueryOptions

use of com.backendless.persistence.QueryOptions in project Android-SDK by Backendless.

the class EditPreferencesActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editpreference);
    progressDialog = UIFactory.getDefaultProgressDialog(this);
    TextView preferenceName = (TextView) findViewById(R.id.preferenceName);
    preferencesContainer = (LinearLayout) findViewById(R.id.preferencesContainer);
    preferenceTheme = PreferenceTheme.valueOf(getIntent().getStringExtra(Lifecycle.PREFERENCE_TYPE_TAG));
    preferenceStore = Backendless.Persistence.of(UserPreferences.class);
    // Naming the page
    preferenceName.setText(preferenceTheme.getPreference());
    ((TextView) findViewById(R.id.preferenceLabel)).setText(getString(R.string.textfield_preferences_label_base) + " " + preferenceTheme.getPreference().toLowerCase() + ":");
    // Getting preference structure
    BackendlessDataQuery backendlessDataQuery = new BackendlessDataQuery("theme = '" + preferenceTheme.getPreference() + "'");
    QueryOptions queryOptions = new QueryOptions();
    queryOptions.setPageSize(50);
    queryOptions.addSortByOption("name");
    backendlessDataQuery.setQueryOptions(queryOptions);
    Backendless.Persistence.of(PreferencesDefaults.class).find(backendlessDataQuery, new AsyncCallback<Collection<PreferencesDefaults>>() {

        @Override
        public void handleResponse(Collection<PreferencesDefaults> preferencesDefaultsBackendlessCollection) {
            // Creating a map with predefined checkboxes
            List<PreferencesDefaults> preferencesDefaults = preferencesDefaultsBackendlessCollection.getCurrentPage();
            for (PreferencesDefaults preference : preferencesDefaults) {
                CheckBox checkBox = UIFactory.getPreferenceCheckbox(EditPreferencesActivity.this, preference);
                checkBox.setOnClickListener(getCheckboxListener(checkBox));
                preferencesContainer.addView(checkBox);
                preferenceNameToCheckboxMap.put(preference.getName(), checkBox);
            }
            // Getting preferences, which should be enabled for current user
            String whereClause = "email = '" + Backendless.UserService.CurrentUser().getEmail() + "'";
            whereClause += " and theme = '" + preferenceTheme.getPreference() + "'";
            BackendlessDataQuery backendlessDataQuery = new BackendlessDataQuery();
            backendlessDataQuery.setWhereClause(whereClause);
            backendlessDataQuery.setQueryOptions(new QueryOptions(50, 0));
            Backendless.Persistence.of(UserPreferences.class).find(backendlessDataQuery, new AsyncCallback<Collection<UserPreferences>>() {

                @Override
                public void handleResponse(Collection<UserPreferences> userPreferencesBackendlessCollection) {
                    List<UserPreferences> userPreferences = userPreferencesBackendlessCollection.getCurrentPage();
                    for (UserPreferences userPreference : userPreferences) {
                        CheckBox checkBox = preferenceNameToCheckboxMap.get(userPreference.getPreference());
                        preferenceNameToPreferenceMap.put(userPreference.getPreference(), userPreference);
                        checkBox.setChecked(true);
                    }
                    progressDialog.cancel();
                }

                @Override
                public void handleFault(BackendlessFault backendlessFault) {
                    progressDialog.cancel();
                    Toast.makeText(EditPreferencesActivity.this, backendlessFault.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
        }

        @Override
        public void handleFault(BackendlessFault backendlessFault) {
            progressDialog.cancel();
            Toast.makeText(EditPreferencesActivity.this, backendlessFault.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
}
Also used : UserPreferences(com.backendless.examples.endless.matchmaker.models.persistent.UserPreferences) ResponseAsyncCallback(com.backendless.examples.endless.matchmaker.controllers.shared.ResponseAsyncCallback) AsyncCallback(com.backendless.async.callback.AsyncCallback) QueryOptions(com.backendless.persistence.QueryOptions) BackendlessFault(com.backendless.exceptions.BackendlessFault) BackendlessDataQuery(com.backendless.persistence.BackendlessDataQuery) CheckBox(android.widget.CheckBox) PreferencesDefaults(com.backendless.examples.endless.matchmaker.models.persistent.PreferencesDefaults) BackendlessCollection(com.backendless.BackendlessCollection) TextView(android.widget.TextView) List(java.util.List)

Example 2 with QueryOptions

use of com.backendless.persistence.QueryOptions in project Android-SDK by Backendless.

the class BrowseActivity method onCreate.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.browse);
    padding = (int) getResources().getDimension(R.dimen.micro_padding);
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int screenWidth = displayMetrics.widthPixels;
    itemWidth = (screenWidth - (2 * (int) getResources().getDimension(R.dimen.default_margin))) / 3;
    GridView gridView = (GridView) findViewById(R.id.gridView);
    imageAdapter = new ImageAdapter(this);
    gridView.setAdapter(imageAdapter);
    Toast.makeText(BrowseActivity.this, "Downloading images...", Toast.LENGTH_SHORT).show();
    BackendlessDataQuery backendlessDataQuery = new BackendlessDataQuery();
    backendlessDataQuery.setQueryOptions(new QueryOptions(100, 0, "uploaded"));
    Backendless.Persistence.of(ImageEntity.class).find(backendlessDataQuery, new AsyncCallback<Collection<ImageEntity>>() {

        @Override
        public void handleResponse(final Collection<ImageEntity> response) {
            Toast.makeText(BrowseActivity.this, "Will add " + response.getCurrentPage().size() + " images", Toast.LENGTH_SHORT).show();
            new Thread() {

                @Override
                public void run() {
                    List<ImageEntity> imageEntities = response.getCurrentPage();
                    for (ImageEntity imageEntity : imageEntities) {
                        Message message = new Message();
                        try {
                            URL url = new URL(imageEntity.getUrl());
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                            connection.setDoInput(true);
                            connection.connect();
                            InputStream input = connection.getInputStream();
                            message.obj = BitmapFactory.decodeStream(input);
                        } catch (Exception e) {
                            message.obj = e;
                        }
                        imagesHandler.sendMessage(message);
                    }
                }
            }.start();
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Toast.makeText(BrowseActivity.this, "Make some upload first", Toast.LENGTH_SHORT).show();
        }
    });
}
Also used : Message(android.os.Message) InputStream(java.io.InputStream) DisplayMetrics(android.util.DisplayMetrics) QueryOptions(com.backendless.persistence.QueryOptions) URL(java.net.URL) BackendlessFault(com.backendless.exceptions.BackendlessFault) HttpURLConnection(java.net.HttpURLConnection) BackendlessDataQuery(com.backendless.persistence.BackendlessDataQuery) BackendlessCollection(com.backendless.BackendlessCollection) GridView(android.widget.GridView)

Aggregations

BackendlessCollection (com.backendless.BackendlessCollection)2 BackendlessFault (com.backendless.exceptions.BackendlessFault)2 BackendlessDataQuery (com.backendless.persistence.BackendlessDataQuery)2 QueryOptions (com.backendless.persistence.QueryOptions)2 Message (android.os.Message)1 DisplayMetrics (android.util.DisplayMetrics)1 CheckBox (android.widget.CheckBox)1 GridView (android.widget.GridView)1 TextView (android.widget.TextView)1 AsyncCallback (com.backendless.async.callback.AsyncCallback)1 ResponseAsyncCallback (com.backendless.examples.endless.matchmaker.controllers.shared.ResponseAsyncCallback)1 PreferencesDefaults (com.backendless.examples.endless.matchmaker.models.persistent.PreferencesDefaults)1 UserPreferences (com.backendless.examples.endless.matchmaker.models.persistent.UserPreferences)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 List (java.util.List)1