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 に変更し、 for ループから 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 に答える