gpt4 book ai didi

java - 如何使用 Autocompletetextview 修复 Google Places 建议

转载 作者:行者123 更新时间:2023-12-02 09:57:04 24 4
gpt4 key购买 nike

我正在尝试使用自动完成 TextView 实现 Google 地方信息建议,但它没有向我显示建议。我发现我的 API key 接受请求,但没有返回任何内容。我还完成了 API key 的计费帐户。

Main.java

public class Main extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
private Button searchdonar,register;
private EditText bloodgroup;
private TextView responseview;
private AutoCompleteTextView autocompletelocation;
private PlaceAutocompleteAdapter placeAutocompleteAdapter;
private PlacesClient placesClient;
private LatLngBounds latLngBounds = new LatLngBounds(
new LatLng(-40,-168 ),new LatLng(71,136));
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

searchdonar = (Button) findViewById(R.id.searchdonar);
register = (Button) findViewById(R.id.register);

bloodgroup = (EditText) findViewById(R.id.bloodgroup);

// Initialize Places.
if(!Places.isInitialized()){
Places.initialize(getApplicationContext(), "API_KEY");
}

// Create a new Places client instance.
placesClient = Places.createClient(this);

initAutoCompleteTextView();

register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent Intent = new Intent(Main.this,PhoneAuth.class);
Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(Intent);
}
});

searchdonar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String Bloodgroup = bloodgroup.getText().toString().trim().toLowerCase();
String Location = autocompletelocation.getText().toString().trim().toLowerCase();

final String bloodgroup_location = Bloodgroup + "_" + Location;

Intent Intent = new Intent(Main.this,Userprofile.class);
Intent.putExtra("bloodgroup_location",bloodgroup_location);
Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(Intent);
}
});
}

private void initAutoCompleteTextView() {

autocompletelocation = findViewById(R.id.location);
autocompletelocation.setThreshold(1);
autocompletelocation.setOnItemClickListener(autocompleteClickListener);
placeAutocompleteAdapter = new PlaceAutocompleteAdapter(this, placesClient);
autocompletelocation.setAdapter(placeAutocompleteAdapter);
}

private AdapterView.OnItemClickListener autocompleteClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

try {
final AutocompletePrediction item = placeAutocompleteAdapter.getItem(i);
String placeID = null;
if (item != null) {
placeID = item.getPlaceId();
}

List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS
, Place.Field.LAT_LNG);

FetchPlaceRequest request = null;
if (placeID != null) {
request = FetchPlaceRequest.builder(placeID, placeFields)
.build();
}

if (request != null) {
placesClient.fetchPlace(request).addOnSuccessListener(new OnSuccessListener<FetchPlaceResponse>() {
@SuppressLint("SetTextI18n")
@Override
public void onSuccess(FetchPlaceResponse task) {

}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
}
});
}
} catch (Exception e) {
e.printStackTrace();
}

}
};
}

PlaceAutocompleteAdapter.java

public class PlaceAutocompleteAdapter extends ArrayAdapter<AutocompletePrediction> implements Filterable {
private ArrayList<AutocompletePrediction> mResultList;
private PlacesClient placesClient;

PlaceAutocompleteAdapter(Context context, PlacesClient placesClient) {
super(context, android.R.layout.simple_expandable_list_item_2, android.R.id.text1);
this.placesClient = placesClient;
}

@Override
public int getCount() {
return mResultList.size();
}

@Override
public AutocompletePrediction getItem(int position) {
return mResultList.get(position);
}

@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
View row = super.getView(position, convertView, parent);

AutocompletePrediction item = getItem(position);

TextView textView1 = row.findViewById(android.R.id.text1);
TextView textView2 = row.findViewById(android.R.id.text2);
if (item != null) {
textView1.setText(item.getPrimaryText(null));
textView2.setText(item.getSecondaryText(null));
}

return row;
}

@NonNull
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {

FilterResults results = new FilterResults();

// We need a separate list to store the results, since
// this is run asynchronously.
ArrayList<AutocompletePrediction> filterData = new ArrayList<>();

// Skip the autocomplete query if no constraints are given.
if (charSequence != null) {
// Query the autocomplete API for the (constraint) search string.
filterData = getAutocomplete(charSequence);
}

results.values = filterData;
if (filterData != null) {
results.count = filterData.size();
} else {
results.count = 0;
}

return results;
}

@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence charSequence, FilterResults results) {

if (results != null && results.count > 0) {
// The API returned at least one result, update the data.
mResultList = (ArrayList<AutocompletePrediction>) results.values;
notifyDataSetChanged();
} else {
// The API did not return any results, invalidate the data set.
notifyDataSetInvalidated();
}
}

@Override
public CharSequence convertResultToString(Object resultValue) {
// Override this method to display a readable result in the AutocompleteTextView
// when clicked.
if (resultValue instanceof AutocompletePrediction) {
return ((AutocompletePrediction) resultValue).getFullText(null);
} else {
return super.convertResultToString(resultValue);
}
}
};
}

private ArrayList<AutocompletePrediction> getAutocomplete(CharSequence constraint) {

//Create a RectangularBounds object.
RectangularBounds bounds = RectangularBounds.newInstance(
new LatLng(-40, -80),
new LatLng(70, 168));


final FindAutocompletePredictionsRequest.Builder requestBuilder =
FindAutocompletePredictionsRequest.builder()
.setQuery(constraint.toString())
.setCountry("") //Use only in specific country
// Call either setLocationBias() OR setLocationRestriction().
.setLocationBias(bounds)
.setSessionToken(AutocompleteSessionToken.newInstance())
.setTypeFilter(TypeFilter.ADDRESS);

Task<FindAutocompletePredictionsResponse> results =
placesClient.findAutocompletePredictions(requestBuilder.build());

//Wait to get results.
try {
Tasks.await(results, 1, TimeUnit.SECONDS);
} catch (ExecutionException | InterruptedException | TimeoutException e) {
e.printStackTrace();
}

if (results.isSuccessful()) {
if (results.getResult() != null) {

return (ArrayList<AutocompletePrediction>) results.getResult().getAutocompletePredictions();
}
return null;
} else {
return null;
}
}
}

Logcat 向我展示了这个:

An exception occurred during performFiltering()!
java.lang.ClassCastException:
com.google.android.libraries.places.internal.hm cannot be cast to java.util.ArrayList
at com.example.istiaque.donateblood.PlaceAutocompleteAdapter.getAutocomplete(PlaceAutocompleteAdapter.java:160)
at com.example.istiaque.donateblood.PlaceAutocompleteAdapter.access$000(PlaceAutocompleteAdapter.java:34)
at com.example.istiaque.donateblood.PlaceAutocompleteAdapter$1.performFiltering(PlaceAutocompleteAdapter.java:87)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.os.HandlerThread.run(HandlerThread.java:61)

最佳答案

今天您不需要创建类适配器。关于地点自动完成的教程是here

代码:

AutocompleteSupportFragment autocompleteFragment; 
PlacesClient placesClient;
LatLng location;

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

// Initialize Places.
Places.initialize(getApplicationContext(), getString(R.string.googleMapsAPI));
// Create a new Places client instance.
placesClient = Places.createClient(this);

// Initialize the AutocompleteSupportFragment.
autocompleteFragment = (AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);

// Specify the types of place data to return.
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));

// Set up a PlaceSelectionListener to handle the response.
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
final LatLng location = place.getLatLng();
Log.i(LOG_TAG, "Place: " + place.getName() + ", " + place.getId());
getLocationPlace(place.getId());
}

@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i(LOG_TAG, "An error occurred: " + status);
}
});
}

private void getLocationPlace(final String placeId) {
// Specify the fields to return (in this example all fields are returned).
List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG);
// Construct a request object, passing the place ID and fields array.
FetchPlaceRequest request = FetchPlaceRequest.builder(placeId, placeFields).build();

placesClient.fetchPlace(request).addOnSuccessListener(new OnSuccessListener<FetchPlaceResponse>() {
@Override
public void onSuccess(FetchPlaceResponse fetchPlaceResponse) {
Place place = fetchPlaceResponse.getPlace();
Log.d(LOG_TAG, "place = " + place.getLatLng());
location = place.getLatLng();
}
});
}

并在布局中添加了一个 fragment :

 <fragment
android:id="@+id/autocomplete_fragment"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Address, City or Zip Code" />

希望这对您有帮助!

关于java - 如何使用 Autocompletetextview 修复 Google Places 建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55931848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com