2013-07-02 28 views
0

我正在從一個項目中加載遠程數據庫中的一些數據並在ListView中顯示它們。數據正確加載,但問題顯示在ListviewSimpleAdapter)。該課程延伸至ListActivityListView異常

代碼:

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> 

如果有人能給我一個解決方案來解決這個異常,我將不勝感激。 在此先感謝...

+0

請列出您的佈局list_note。 – wodong

回答

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"}

+0

哈哈,怎麼可能,我沒有看到?:)謝謝了很多人!你可能救了我幾個小時! :) – Husky

+0

雅,我也花了一些時間....無論如何高興,你的問題解決.. –

1

只需將ListAdapter更改爲Simple Adapter,然後將hashmap對象添加到hashmap arraylist out for for循環中即可。

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); 
     } 

    }); 
    } 
+0

Id說,改變ListAdapter爲SimpleAdapter不會有任何區別。它只是接口和該接口的具體實現。順便說一句,應該將hashMap添加到for循環中的ArrayList中。無論如何,我會嘗試你的解決方案:) – Husky

+0

而在xml文件中你使用了哪種佈局? –

+0

我添加了兩個xml文件,請檢查它:) – Husky