メインコンテンツまでスキップ

Favorites

This example does not contain a UI. Results will be logged in Android Studio.

Note

This example is a part of the Search SDK for Android sample app. You can find the values for all referenced resources in the res directory. For example, see res/values/strings.xml for R.string.* references used in this example. The dependencies can be found here.The examples use View binding.See setup documention if necessary.

MainActivity.java
package com.mapbox.search.sample.api;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.mapbox.geojson.Point;
import com.mapbox.search.ServiceProvider;
import com.mapbox.search.common.AsyncOperationTask;
import com.mapbox.search.record.FavoriteRecord;
import com.mapbox.search.record.FavoritesDataProvider;
import com.mapbox.search.common.CompletionCallback;
import com.mapbox.search.record.LocalDataProvider.OnDataChangedListener;
import com.mapbox.search.result.SearchAddress;
import com.mapbox.search.result.SearchResultType;

import java.util.List;
import java.util.UUID;

import kotlin.Unit;

public class FavoritesDataProviderJavaExample extends AppCompatActivity {

private final FavoritesDataProvider favoritesDataProvider = ServiceProvider.getInstance().favoritesDataProvider();

private AsyncOperationTask task = null;

private final CompletionCallback<List<FavoriteRecord>> retrieveFavoritesCallback = new CompletionCallback<List<FavoriteRecord>>() {
@Override
public void onComplete(List<FavoriteRecord> result) {
Log.i("SearchApiExample", "Favorite records: " + result);
}

@Override
public void onError(@NonNull Exception e) {
Log.i("SearchApiExample", "Unable to retrieve favorite records", e);
}
};

private final CompletionCallback<Unit> addFavoriteCallback = new CompletionCallback<Unit>() {
@Override
public void onComplete(Unit result) {
Log.i("SearchApiExample", "Favorite record added");
task = favoritesDataProvider.getAll(retrieveFavoritesCallback);
}

@Override
public void onError(@NonNull Exception e) {
Log.i("SearchApiExample", "Unable to add a new favorite record", e);
}
};

private final OnDataChangedListener<FavoriteRecord> onDataChangedListener = newData -> {
Log.i("SearchApiExample", "Favorites data changed. New data: " + newData);
};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

favoritesDataProvider.addOnDataChangedListener(onDataChangedListener);

final FavoriteRecord newFavorite = new FavoriteRecord(
UUID.randomUUID().toString(),
"Paris Eiffel Tower",
"Eiffel Tower, Paris, France",
new SearchAddress(null, null, null, null, null, "Paris", null, null, "France"),
null,
null,
null,
Point.fromLngLat(2.294434, 48.858349),
SearchResultType.PLACE,
null
);

task = favoritesDataProvider.upsert(newFavorite, addFavoriteCallback);
}

@Override
protected void onDestroy() {
favoritesDataProvider.removeOnDataChangedListener(onDataChangedListener);
task.cancel();
super.onDestroy();
}
}
この{Type}は役に立ちましたか?