0

我正在做一个项目,我从远程数据库加载一些数据并将它们显示在ListView. 数据已正确加载,但问题是在Listview( SimpleAdapter) 中显示它们。类扩展ListActivity

代码:

protected void onPostExecute(String file_url){

        progressDialog.dismiss();

        try{
            if(jsonObject.getInt("success") == 1){
                jsonArray = jsonObject.getJSONArray("notes");

                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject o = jsonArray.getJSONObject(i);

                    Note tempNote = new Note(o.getInt("note_id"), o.getString("description"), o.getString("data"));
                    userData.addNote(tempNote);

                    HashMap<String, String> hashMap = new HashMap<String, String>();
                    hashMap.put("id", o.getString("note_id"));
                    hashMap.put("description", o.getString("description"));
                    userNotes.add(hashMap);
                    Log.d(TAG, userNotes.get(0).toString());

                }

            }
            Toast.makeText(UserNotes.this, jsonObject.getString("message"), Toast.LENGTH_LONG).show();
        }
        catch (JSONException e){
            e.printStackTrace();
        }

        runOnUiThread(new Runnable(){

            @Override
            public void run() {
                ListAdapter listAdapter = new SimpleAdapter(UserNotes.this, 
                        userNotes, 
                        R.layout.list_note, 
                        new String[] {"id, description"},
                        new int[] {R.id.list_note_id, R.id.list_note_description});
                setListAdapter(listAdapter);
            }

        });
        }

错误:

07-02 08:05:09.964: E/AndroidRuntime(357): FATAL EXCEPTION: main
07-02 08:05:09.964: E/AndroidRuntime(357): java.lang.ArrayIndexOutOfBoundsException
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:160)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.widget.AbsListView.obtainView(AbsListView.java:1315)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.widget.ListView.onMeasure(ListView.java:1109)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.view.View.measure(View.java:8171)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.view.View.measure(View.java:8171)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.view.View.measure(View.java:8171)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.view.View.measure(View.java:8171)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.view.View.measure(View.java:8171)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.os.Looper.loop(Looper.java:123)
07-02 08:05:09.964: E/AndroidRuntime(357):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-02 08:05:09.964: E/AndroidRuntime(357):  at java.lang.reflect.Method.invokeNative(Native Method)
07-02 08:05:09.964: E/AndroidRuntime(357):  at java.lang.reflect.Method.invoke(Method.java:521)
07-02 08:05:09.964: E/AndroidRuntime(357):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-02 08:05:09.964: E/AndroidRuntime(357):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-02 08:05:09.964: E/AndroidRuntime(357):  at dalvik.system.NativeStart.main(Native Method)

list_note.xml:

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


<TextView
    android:id="@+id/list_note_description"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:paddingLeft="6dip"
    android:textSize="17dip"
    android:textStyle="bold" />

user_notes.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" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

如果有人能给我一个解决此异常的解决方案,我将不胜感激。提前致谢...

4

2 回答 2

2

你在你的代码中所做的是

ListAdapter listAdapter = new SimpleAdapter(UserNotes.this, 
                        userNotes, 
                        R.layout.list_note, 
                        new String[] {"id, description"},
                        new int[] {R.id.list_note_id, R.id.list_note_description});

将其更改为

ListAdapter listAdapter = new SimpleAdapter(UserNotes.this, 
                        userNotes, 
                        R.layout.list_note, 
                        new String[] {"id", "description"},
                        new int[] {R.id.list_note_id, R.id.list_note_description});

你做new String[] {"id, description"}的应该是
new String[] {"id", "description"}

于 2013-07-02T08:59:57.350 回答
1

只需将 ListAdapter 更改为 Simple Adapter 并将 hashmap 对象添加到 hashmap arraylist 中即可。

    protected void onPostExecute(String file_url){

         progressDialog.dismiss();

    try{
        if(jsonObject.getInt("success") == 1){
            jsonArray = jsonObject.getJSONArray("notes");

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject o = jsonArray.getJSONObject(i);

                Note tempNote = new Note(o.getInt("note_id"), o.getString("description"), o.getString("data"));
                userData.addNote(tempNote);

                HashMap<String, String> hashMap = new HashMap<String, String>();
                hashMap.put("id", o.getString("note_id"));
                hashMap.put("description", o.getString("description"));

                Log.d(TAG, userNotes.get(0).toString());

            }
                userNotes.add(hashMap);

        }
        Toast.makeText(UserNotes.this, jsonObject.getString("message"), Toast.LENGTH_LONG).show();
    }
    catch (JSONException e){
        e.printStackTrace();
    }

    runOnUiThread(new Runnable(){

        @Override
        public void run() {
            SimpleAdapter listAdapter = new SimpleAdapter(UserNotes.this, 
                    userNotes, 
                    R.layout.list_note, 
                    new String[] {"id, description"},
                    new int[] {R.id.list_note_id, R.id.list_note_description});
            setListAdapter(listAdapter);
        }

    });
    }
于 2013-07-02T08:29:44.970 回答