Search history
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.Nullable;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.mapbox.search.ServiceProvider;
import com.mapbox.search.common.AsyncOperationTask;
import com.mapbox.search.record.HistoryDataProvider;
import com.mapbox.search.record.HistoryRecord;
import com.mapbox.search.common.CompletionCallback;
import java.util.List;
public class HistoryDataProviderJavaExample extends AppCompatActivity {
private final HistoryDataProvider historyDataProvider = ServiceProvider.getInstance().historyDataProvider();
private AsyncOperationTask task = null;
private final CompletionCallback<List<HistoryRecord>> callback = new CompletionCallback<List<HistoryRecord>>() {
@Override
public void onComplete(List<HistoryRecord> result) {
Log.i("SearchApiExample", "History records: " + result);
}
@Override
public void onError(@NonNull Exception e) {
Log.i("SearchApiExample", "Unable to retrieve history records", e);
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
task = historyDataProvider.getAll(callback);
}
@Override
protected void onDestroy() {
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.search.ServiceProvider
import com.mapbox.search.common.AsyncOperationTask
import com.mapbox.search.common.CompletionCallback
import com.mapbox.search.record.HistoryRecord
class HistoryDataProviderKotlinExample : AppCompatActivity() {
private val historyDataProvider = ServiceProvider.INSTANCE.historyDataProvider()
private lateinit var task: AsyncOperationTask
private val callback: CompletionCallback<List<HistoryRecord>> = object : CompletionCallback<List<HistoryRecord>> {
override fun onComplete(result: List<HistoryRecord>) {
Log.i("SearchApiExample", "History records: $result")
}
override fun onError(e: Exception) {
Log.i("SearchApiExample", "Unable to retrieve history records", e)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
task = historyDataProvider.getAll(callback)
}
override fun onDestroy() {
task.cancel()
super.onDestroy()
}
}
この{Type}は役に立ちましたか?