我可以从AutocompleteTextView获得所有建议。当我点击建议项目时,我想绘制与该特定项目相关联的纬度和经度坐标。目前我正在将该建议显示为String但是当我尝试将其转换为Object以获取纬度和经度坐标时,我得到Class Cast Exception。我真的需要帮助。
这是我的方法
private void searchAutocomplete(){
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
//Create a new ArrayAdapter with your context and the simple layout for the dropdown menu provided by Android
final ArrayAdapter<String> autoComplete = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1);
//Child the root before all the push() keys are found and add a ValueEventListener()
database.child("wr").child("clubs").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Basically, this says "For each DataSnapshot *Data* in dataSnapshot, do what's inside the method.
for (DataSnapshot suggestionSnapshot : dataSnapshot.getChildren()){
//Get the suggestion by childing the key of the string you want to get.
String suggestion = suggestionSnapshot.child("name1").getValue(String.class);
//Add the retrieved string to the list
autoComplete.add(suggestion);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
final AutoCompleteTextView ACTV= (AutoCompleteTextView)findViewById(R.id.autocompleteView);
ACTV.setAdapter(autoComplete);
ACTV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
LocationModel currentLocation =(LocationModel) arg0.getItemAtPosition(arg2);
double latitude = currentLocation.getLat();
double longitude = currentLocation.getLng();
LatLngBounds bounds;
LatLngBounds.Builder builder = new LatLngBounds.Builder();
// Create LatLng for each locations
LatLng mLatlng = new LatLng(latitude, longitude);
// Make sure the map boundary contains the location
builder.include(mLatlng);
bounds = builder.build();
//markerList.add(mMarker);
// Zoom map to the boundary that contains every logged location
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds,
MAP_ZOOM_LEVEL));
}
});
}
错误记录
4 12045-12045/com.lawerh.jonathan.partygoer V/InputMethodManager: START INPUT: android.widget.AutoCompleteTextView{a5ff873 VFED..CL. .F....ID 0,0-688,85 #7f1000d9 app:id/autocompleteView} ic=com.android.internal.widget.EditableInputConnection@f770cf2 tba=android.view.inputmethod.EditorInfo@ce6f43 controlFlags=#0
01-14 07:10:24.015 12045-12045/com.lawerh.jonathan.partygoer D/AndroidRuntime: Shutting down VM
01-14 07:10:24.016 12045-12045/com.lawerh.jonathan.partygoer E/UncaughtException: java.lang.ClassCastException: java.lang.String cannot be cast to com.lawerh.jonathan.partygoer.model.LocationModel
at com.lawerh.jonathan.partygoer.ui.MapActivity$4.onItemClick(MapActivity.java:107)
at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:920)
at android.widget.AutoCompleteTextView.-wrap1(AutoCompleteTextView.java)
at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1217)
at android.widget.AdapterView.performItemClick(AdapterView.java:315)
at android.widget.AbsListView.performItemClick(AbsListView.java:1193)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3231)
at android.widget.AbsListView$3.run(AbsListView.java:4207)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
01-14 07:10:24.023 12045-12075/com.lawerh.jonathan.partygoer D/FA: Logging event (FE): _ae, Bundle[{_o=crash, _sc=MapActivity, _si=1784221226449878799, timestamp=1484377824016, fatal=1}]