2013-07-28 138 views
0

我使用的ArrayList與定義無法映射與ArrayList的

ArrayList<HashMap<String, String>> placesListItems = new ArrayList<HashMap<String,String>>(); 

使用下面的代碼來填充ArrayList中

for (Place p : placelist.getPlaces()) { 
       HashMap<String, String> map = new HashMap<String, String>(); 

       // Place reference won't display in listview - it will be hidden 
       // Place reference is used to get "place full details" 
       map.put(KEY_REFERENCE, p.getReference()); 

       // Place name 
       map.put(KEY_NAME, p.getName()); 


       // adding HashMap to ArrayList 
       placesListItems.add(map); 
      } 

和下面的代碼將其綁定到ListView

 ListAdapter adapter = new SimpleAdapter(this, placesListItems, 
        R.layout.list_item, 
        new String[] { KEY_REFERENCE, KEY_NAME}, new int[] { 
          R.id.reference, R.id.name }); 

      lv.setAdapter(adapter); 
一個ListView

問題是,我的ListView保持空白並在一會兒之後意外停止。 我使用調試器檢查了placelistitems被正確構建。

任何幫助將不勝感激。

謝謝!

編輯: 這是接收值的東西,但我不知道是什麼。我正在使用google Place的JSON響應將數據提供給此數組列表。 這裏是我的logcat:

07-28 21:18:33.709: W/System.err(8176): org.json.JSONException: No value for rating 
07-28 21:18:33.709: W/System.err(8176):  at org.json.JSONObject.get(JSONObject.java:354) 
07-28 21:18:33.719: W/System.err(8176):  at org.json.JSONObject.getDouble(JSONObject.java:409) 
07-28 21:18:33.719: W/System.err(8176):  at org.gmarz.googleplaces.models.Place.<init>(Place.java:36) 
07-28 21:18:33.719: W/System.err(8176):  at org.gmarz.googleplaces.models.PlacesResult.<init>(PlacesResult.java:21) 
07-28 21:18:33.719: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:94) 
07-28 21:18:33.719: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:53) 
07-28 21:18:33.729: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:66) 
07-28 21:18:33.729: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:71) 
07-28 21:18:33.729: W/System.err(8176):  at com.example.testlibrary.Async.doInBackground(Async.java:26) 
07-28 21:18:33.729: W/System.err(8176):  at com.example.testlibrary.Async.doInBackground(Async.java:1) 
07-28 21:18:33.729: W/System.err(8176):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
07-28 21:18:33.729: W/System.err(8176):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
07-28 21:18:33.739: W/System.err(8176):  at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
07-28 21:18:33.739: W/System.err(8176):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
07-28 21:18:33.739: W/System.err(8176):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
07-28 21:18:33.739: W/System.err(8176):  at java.lang.Thread.run(Thread.java:1019) 
07-28 21:18:33.749: W/System.err(8176): org.json.JSONException: No value for rating 
07-28 21:18:33.759: W/System.err(8176):  at org.json.JSONObject.get(JSONObject.java:354) 
07-28 21:18:33.759: W/System.err(8176):  at org.json.JSONObject.getDouble(JSONObject.java:409) 
07-28 21:18:33.769: W/System.err(8176):  at org.gmarz.googleplaces.models.Place.<init>(Place.java:36) 
07-28 21:18:33.769: W/System.err(8176):  at org.gmarz.googleplaces.models.PlacesResult.<init>(PlacesResult.java:21) 
07-28 21:18:33.769: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:94) 
07-28 21:18:33.769: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:53) 
07-28 21:18:33.769: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:66) 
07-28 21:18:33.769: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:71) 
07-28 21:18:33.769: W/System.err(8176):  at com.example.testlibrary.Async.doInBackground(Async.java:26) 
07-28 21:18:33.779: W/System.err(8176):  at com.example.testlibrary.Async.doInBackground(Async.java:1) 
07-28 21:18:33.779: W/System.err(8176):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
07-28 21:18:33.779: W/System.err(8176):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
07-28 21:18:33.779: W/System.err(8176):  at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
07-28 21:18:33.779: W/System.err(8176):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
07-28 21:18:33.789: W/System.err(8176):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
07-28 21:18:33.789: W/System.err(8176):  at java.lang.Thread.run(Thread.java:1019) 
07-28 21:18:33.819: W/System.err(8176): org.json.JSONException: No value for rating 
07-28 21:18:33.859: W/System.err(8176):  at org.json.JSONObject.get(JSONObject.java:354) 
07-28 21:18:33.989: W/System.err(8176):  at org.json.JSONObject.getDouble(JSONObject.java:409) 
07-28 21:18:33.989: W/System.err(8176):  at org.gmarz.googleplaces.models.Place.<init>(Place.java:36) 
07-28 21:18:33.989: W/System.err(8176):  at org.gmarz.googleplaces.models.PlacesResult.<init>(PlacesResult.java:21) 
07-28 21:18:34.009: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:94) 
07-28 21:18:34.019: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:53) 
07-28 21:18:34.039: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:66) 
07-28 21:18:34.039: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:71) 
07-28 21:18:34.039: W/System.err(8176):  at com.example.testlibrary.Async.doInBackground(Async.java:26) 
07-28 21:18:34.039: W/System.err(8176):  at com.example.testlibrary.Async.doInBackground(Async.java:1) 
07-28 21:18:34.049: W/System.err(8176):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
07-28 21:18:34.049: W/System.err(8176):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
07-28 21:18:34.049: W/System.err(8176):  at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
07-28 21:18:34.049: W/System.err(8176):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
07-28 21:18:34.049: W/System.err(8176):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
07-28 21:18:34.049: W/System.err(8176):  at java.lang.Thread.run(Thread.java:1019) 
07-28 21:18:34.049: W/System.err(8176): org.json.JSONException: No value for rating 
07-28 21:18:34.069: W/System.err(8176):  at org.json.JSONObject.get(JSONObject.java:354) 
07-28 21:18:34.069: W/System.err(8176):  at org.json.JSONObject.getDouble(JSONObject.java:409) 
07-28 21:18:34.069: W/System.err(8176):  at org.gmarz.googleplaces.models.Place.<init>(Place.java:36) 
07-28 21:18:34.069: W/System.err(8176):  at org.gmarz.googleplaces.models.PlacesResult.<init>(PlacesResult.java:21) 
07-28 21:18:34.069: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:94) 
07-28 21:18:34.069: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:53) 
07-28 21:18:34.069: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:66) 
07-28 21:18:34.079: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:71) 
07-28 21:18:34.079: W/System.err(8176):  at com.example.testlibrary.Async.doInBackground(Async.java:26) 
07-28 21:18:34.079: W/System.err(8176):  at com.example.testlibrary.Async.doInBackground(Async.java:1) 
07-28 21:18:34.079: W/System.err(8176):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
07-28 21:18:34.079: W/System.err(8176):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
07-28 21:18:34.079: W/System.err(8176):  at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
07-28 21:18:34.079: W/System.err(8176):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
07-28 21:18:34.089: W/System.err(8176):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
07-28 21:18:34.089: W/System.err(8176):  at java.lang.Thread.run(Thread.java:1019) 
07-28 21:18:34.089: W/System.err(8176): org.json.JSONException: No value for rating 
07-28 21:18:34.099: W/System.err(8176):  at org.json.JSONObject.get(JSONObject.java:354) 
07-28 21:18:34.099: W/System.err(8176):  at org.json.JSONObject.getDouble(JSONObject.java:409) 
07-28 21:18:34.099: W/System.err(8176):  at org.gmarz.googleplaces.models.Place.<init>(Place.java:36) 
07-28 21:18:34.099: W/System.err(8176):  at org.gmarz.googleplaces.models.PlacesResult.<init>(PlacesResult.java:21) 
07-28 21:18:34.099: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:94) 
07-28 21:18:34.109: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:53) 
07-28 21:18:34.109: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:66) 
07-28 21:18:34.109: W/System.err(8176):  at org.gmarz.googleplaces.GooglePlaces.getPlaces(GooglePlaces.java:71) 
07-28 21:18:34.119: W/System.err(8176):  at com.example.testlibrary.Async.doInBackground(Async.java:26) 
07-28 21:18:34.119: W/System.err(8176):  at com.example.testlibrary.Async.doInBackground(Async.java:1) 
07-28 21:18:34.119: W/System.err(8176):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
07-28 21:18:34.119: W/System.err(8176):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
07-28 21:18:34.119: W/System.err(8176):  at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
07-28 21:18:34.119: W/System.err(8176):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
07-28 21:18:34.119: W/System.err(8176):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
07-28 21:18:34.119: W/System.err(8176):  at java.lang.Thread.run(Thread.java:1019) 
07-28 21:18:34.159: W/dalvikvm(8176): threadid=9: thread exiting with uncaught exception (group=0x40018578) 
07-28 21:18:34.329: E/AndroidRuntime(8176): FATAL EXCEPTION: AsyncTask #1 
07-28 21:18:34.329: E/AndroidRuntime(8176): java.lang.RuntimeException: An error occured while executing doInBackground() 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at android.os.AsyncTask$3.done(AsyncTask.java:200) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at java.lang.Thread.run(Thread.java:1019) 
07-28 21:18:34.329: E/AndroidRuntime(8176): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at android.view.ViewRoot.checkThread(ViewRoot.java:3020) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at android.view.ViewRoot.focusableViewAvailable(ViewRoot.java:1718) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:452) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:452) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:452) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:452) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at android.view.View.setFlags(View.java:4614) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at android.view.View.setFocusableInTouchMode(View.java:3190) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at android.widget.AdapterView.checkFocus(AdapterView.java:694) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at android.widget.ListView.setAdapter(ListView.java:503) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at com.example.testlibrary.MainActivity.processFinish(MainActivity.java:66) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at com.example.testlibrary.Async.doInBackground(Async.java:28) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at com.example.testlibrary.Async.doInBackground(Async.java:1) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
07-28 21:18:34.329: E/AndroidRuntime(8176):  ... 4 more 

另一個編輯:

這裏是我的名單XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView android:id="@+id/reference" 
     android:visibility="gone" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 

    <TextView android:id="@+id/name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="10sp" 
     android:textStyle="bold" 
     android:textSize="16sp"/> 
    <ListView 
     android:id="@+id/lvPlaces" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginRight="26dp" > 
    </ListView> 
</LinearLayout> 
+0

慈祥發佈logcat的 – KOTIOS

+0

發表您的logcate –

+0

待辦事項R.id.reference和R.id.name引用TextViews? http://developer.android.com/reference/android/widget/SimpleAdapter.html「這些應該都是TextViews」 – Bram

回答

0
ListAdapter adapter = new SimpleAdapter(filename.this, placesListItems, 
        R.layout.list_item, 
        new String[] { KEY_REFERENCE, KEY_NAME}, new int[] { 
          R.id.reference, R.id.name }); 

      // lv.setAdapter(adapter); 
setListAdapter(adapter); 
+0

謝謝Venkata,你能幫忙定義setListAdapter(adapter)來填充ListView嗎? – Hitesh

+0

請參閱http://www.vogella.com/articles/AndroidListView/瞭解更多信息.. –