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.
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();
}
}
package com.mapbox.search.sample.api
import android.os.Bundle
import android.util.Log
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.common.CompletionCallback
import com.mapbox.search.record.FavoriteRecord
import com.mapbox.search.record.LocalDataProvider.OnDataChangedListener
import com.mapbox.search.result.SearchAddress
import com.mapbox.search.result.SearchResultType
import java.util.UUID
class FavoritesDataProviderKotlinExample : AppCompatActivity() {
private val favoritesDataProvider = ServiceProvider.INSTANCE.favoritesDataProvider()
private lateinit var task: AsyncOperationTask
private val retrieveFavoritesCallback: CompletionCallback<List<FavoriteRecord>> =
object : CompletionCallback<List<FavoriteRecord>> {
override fun onComplete(result: List<FavoriteRecord>) {
Log.i("SearchApiExample", "Favorite records: $result")
}
override fun onError(e: Exception) {
Log.i("SearchApiExample", "Unable to retrieve favorite records", e)
}
}
private val addFavoriteCallback: CompletionCallback<Unit> = object : CompletionCallback<Unit> {
override fun onComplete(result: Unit) {
Log.i("SearchApiExample", "Favorite record added")
task = favoritesDataProvider.getAll(retrieveFavoritesCallback)
}
override fun onError(e: Exception) {
Log.i("SearchApiExample", "Unable to add a new favorite record", e)
}
}
private val onDataChangedListener: OnDataChangedListener<FavoriteRecord> =
object : OnDataChangedListener<FavoriteRecord> {
override fun onDataChanged(newData: List<FavoriteRecord>) {
Log.i("SearchApiExample", "Favorites data changed. New data: $newData")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
favoritesDataProvider.addOnDataChangedListener(onDataChangedListener)
val newFavorite = FavoriteRecord(
id = UUID.randomUUID().toString(),
name = "Paris Eiffel Tower",
descriptionText = "Eiffel Tower, Paris, France",
address = SearchAddress(place = "Paris", country = "France"),
routablePoints = null,
categories = null,
makiIcon = null,
coordinate = Point.fromLngLat(2.294434, 48.858349),
type = SearchResultType.PLACE,
metadata = null
)
task = favoritesDataProvider.upsert(newFavorite, addFavoriteCallback)
}
override fun onDestroy() {
favoritesDataProvider.removeOnDataChangedListener(onDataChangedListener)
task.cancel()
super.onDestroy()
}
}
この{Type}は役に立ちましたか?