以下是我的MainActivity的代码:
public class MainActivity extends FragmentActivity implements OnItemClickListener{
AutoCompleteTextView atvPlaces;
GoogleMap mMap;
final int PLACES=0;
final int PLACES_DETAILS=1;
private static final String TAG="google";
private static final String LOG_TAG="SearchViewActivity1";
private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String OUT_JSON = "/json";
private static final String API_KEY = "MY API KEY";
private static final String API = (PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
private ArrayList<String> autocomplete(String input) {
ArrayList<String> resultList = null;
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("?key=" + API_KEY);
sb.append("&components=country:ph");
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Error processing Places API URL", e);
return resultList;
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to Places API", e);
return resultList;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
// Extract the Place descriptions from the results
resultList = new ArrayList<String>(predsJsonArray.length());
for (int i = 0; i < predsJsonArray.length(); i++) {
resultList.add(predsJsonArray.getJSONObject(i).getString("description"));
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Cannot process JSON results", e);
}
return resultList;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mapFragment.getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
AutoCompleteTextView autoCompView = (AutoCompleteTextView) findViewById(R.id.atv_places);
autoCompView.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.list_view));
autoCompView.setOnItemClickListener(this);
}
private class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private ArrayList<String> resultList;
public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public String getItem(int index) {
return resultList.get(index);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
resultList = autocomplete(constraint.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}};
return filter;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
ListView lv = (ListView) adapterView;
SimpleAdapter adapter = (SimpleAdapter) adapterView.getAdapter();
HashMap<String, String> hm = (HashMap<String, String>) adapter.getItem(position);
// Creating a DownloadTask to download Places details of the selected place
DownloadTask DownloadTask = new DownloadTask();
// Getting url to the Google Places details api
String url = getPlaceDetailsUrl(hm.get("reference"));
// Start downloading Google Place Details
// This causes to execute doInBackground() of DownloadTask class
DownloadTask.execute(url);
// TODO Auto-generated method stub
String str = (String) adapterView.getItemAtPosition(position);
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
private String getPlaceDetailsUrl(String ref){
// Obtain browser key from https://code.google.com/apis/console
String key = "key=YOUR_API_KEY";
// reference of place
String reference = "reference="+ref;
// Sensor enabled
String sensor = "sensor=false";
// Building the parameters to the web service
String parameters = reference+"&"+sensor+"&"+key;
// Output format
String output = "json";
// Building the url to the web service
String url = "https://maps.googleapis.com/maps/api/place/details/"+output+"?"+parameters;
return url;
}
/** A class, to download Places from Geocoding webservice */
private class DownloadTask extends AsyncTask<String, Integer, String>{
String data = null;
// Invoked by execute() method of this object
@Override
protected String doInBackground(String... url) {
try{
data = downloadUrl(url[0]);
}catch(Exception e){
Log.d("Background Task",e.toString());
}
return data;
}
// Executed after the complete execution of doInBackground() method
@Override
protected void onPostExecute(String result){
ParserTask parserTask = new ParserTask();
parserTask.execute(API);
}
}
private String downloadUrl(String strUrl) throws IOException{
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try{
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while( ( line = br.readLine()) != null){
sb.append(line);
}
data = sb.toString();
br.close();
}catch(Exception e){
Log.d("Exception while downloading url", e.toString());
}finally{
iStream.close();
urlConnection.disconnect();
}
return data;
}
class ParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>>{
JSONObject jObject;
@Override
protected List<HashMap<String,String>> doInBackground(String... jsonData) {
List<HashMap<String, String>> places = null;
GeocodeJSONParser parser = new GeocodeJSONParser();
try{
jObject = new JSONObject(jsonData[0]);
/** Getting the parsed data as a an ArrayList */
places = parser.parse(jObject);
}catch(Exception e){
Log.d("Exception",e.toString());
}
return places;
}
// Executed after the complete execution of doInBackground() method
@Override
public void onPostExecute(List<HashMap<String,String>> list){
// Clears all the existing markers
mMap.clear();
for(int i=0;i<list.size();i++){
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
HashMap<String, String> hmPlace = list.get(i);
double lat = Double.parseDouble(hmPlace.get("lat"));
double lng = Double.parseDouble(hmPlace.get("lng"));
String name = hmPlace.get("formatted_address");
LatLng latLng = new LatLng(lat, lng);
markerOptions.position(latLng);
markerOptions.title(name);
mMap.addMarker(markerOptions);
// Locate the first location
if(i==0)
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
markerOptions.position(latLng);
Toast.makeText(getApplicationContext(), latLng.toString(),
Toast.LENGTH_LONG).show();
}
}
}}
我收到此错误(logcat
):
09-24 16:08:30.805: E/AndroidRuntime(16994): FATAL EXCEPTION: main
09-24 16:08:30.805: E/AndroidRuntime(16994): java.lang.ClassCastException: com.example.google.MainActivity$PlacesAutoCompleteAdapter cannot be cast to android.widget.SimpleAdapter
09-24 16:08:30.805: E/AndroidRuntime(16994): at com.example.google.MainActivity.onItemClick(MainActivity.java:195)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:902)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:91)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1192)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.widget.AbsListView.onKeyUp(AbsListView.java:2890)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.widget.ListView.commonKey(ListView.java:2258)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.widget.ListView.onKeyUp(ListView.java:2113)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.widget.ListPopupWindow.onKeyUp(ListPopupWindow.java:912)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.widget.AutoCompleteTextView.onKeyUp(AutoCompleteTextView.java:672)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.view.KeyEvent.dispatch(KeyEvent.java:2633)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.view.View.dispatchKeyEvent(View.java:7205)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
09-24 16:08:30.805: E/AndroidRuntime(16994): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1920)
09-24 16:08:30.805: E/AndroidRuntime(16994): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1395)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.app.Activity.dispatchKeyEvent(Activity.java:2370)
09-24 16:08:30.805: E/AndroidRuntime(16994): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.os.Looper.loop(Looper.java:137)
09-24 16:08:30.805: E/AndroidRuntime(16994): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-24 16:08:30.805: E/AndroidRuntime(16994): at java.lang.reflect.Method.invokeNative(Native Method)
09-24 16:08:30.805: E/AndroidRuntime(16994): at java.lang.reflect.Method.invoke(Method.java:511)
09-24 16:08:30.805: E/AndroidRuntime(16994): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-24 16:08:30.805: E/AndroidRuntime(16994): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-24 16:08:30.805: E/AndroidRuntime(16994): at dalvik.system.NativeStart.main(Native Method)
我认为这是一个菜鸟问题,但请帮忙!谢谢!
答案 0 :(得分:0)
ArrayAdapter不会从SimpleAdapter继承,这会导致此错误。
SimpleAdapter adapter = (SimpleAdapter) adapterView.getAdapter();
http://developer.android.com/reference/android/widget/SimpleAdapter.html http://developer.android.com/reference/android/widget/ArrayAdapter.html
要转换对象,它必须具有相同的数据类型,或者从您尝试转换为的对象继承。
尝试将其切换为:
ArrayAdapter<String> adapter = (ArrayAdapter<String>) adapterView.getAdapter();
您继承的方法调用是继承的,因此应该没有问题:
http://developer.android.com/reference/android/widget/ArrayAdapter.html#getItem(int)
希望这有帮助。
编辑:对不起,我也注意到了:
HashMap<String, String> hm = (HashMap<String, String>) adapter.getItem(position);
您的适配器的数据类型为String,这将永远不会起作用。它必须是:
String hm = ( String ) adapter.getItem(position);
我还设置了适配器强制转换以考虑数据类型。