0

我正在使用带有定义的 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);
            }

并使用以下代码将其绑定到列表视图

       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 保持空白并在片刻后意外停止。我已经使用调试器检查了 placelistitems 是否正确构建。

任何帮助将不胜感激。

谢谢!

编辑:这与收到的值有关,但我不确定是什么。我正在使用 google Place 的 JSON 响应向这个数组列表提供数据。这是我的日志:

 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>
4

1 回答 1

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);
于 2013-07-28T15:46:12.457 回答