2014-09-24 32 views
0

這裏是我的代碼爲我的MainActivity:我AutoCompleteTextView工作正常,但沒有標記指向

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) 

我認爲這是一個noob問題但請幫忙!謝謝!

+0

你得到'classcastexception' * AutoCompleteAdapter不能轉換爲android.widget.SimpleAdapter * – Gattsu 2014-09-24 08:30:23

+0

你能告訴我應該改變什麼嗎? – 2014-09-25 05:42:28

回答

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 hm = (String) adapter.getItem(position); 

我也設置了適配器轉換考慮數據類型了。

+0

String url = getPlaceDetailsUrl(hm.get(「reference」)); 錯誤「.get」部分,我不知道爲什麼.... – 2014-09-25 05:36:19

+0

這是因爲get不是String對象的方法,它是Map接口的成員。只需將其更改爲'String url = getPlaceDetailsUrl(hm);' – 2014-09-25 09:17:55

相關問題