2014-01-07 74 views
0

我從sqlite的檢索數據,並使用simpleadapter.I沒有關於Layouts.Still僅供參考任何問題出在ListView它:實現simpleadapter使用HashMap的

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="10dp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" > 

    <!-- Name Label --> 

    <TextView 
     android:id="@+id/subject" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="2dip" 
     android:paddingTop="6dip" 
     android:textColor="#421fc4" 
     android:textSize="16sp" 
     android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/day" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="2dip" 
      android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/slot" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="left" 
     android:textColorHighlight="#782086" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/instructor" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="left" 
     android:textColor="#782086" 
     android:textStyle="bold" /> 

</LinearLayout> 

我有問題在下面的代碼片段的某處。我試圖從普通列表視圖中檢索sqlite的數據,它工作完美。但是當我使用此代碼片段的自定義列表視圖使用SimpleAdapter我只得到結果的最後一行在列表視圖中最後一行的數據重複8次,因爲結果中有8行查詢。

HashMap<String, String> hashMap = new HashMap<String, String>(); 
ArrayList<HashMap<String, String>> Timetablelist; 

//c being the cursor  
    if (c.moveToFirst()) { 
      do { 
       String subject = c.getString(c.getColumnIndex(dba.KEY_SUBJECT)); 
       String day = c.getString(c.getColumnIndex(dba.KEY_DAY)); 
       String slot = c.getString(c.getColumnIndex(dba.KEY_SLOT)); 
       String instructor = c.getString(c.getColumnIndex(dba.KEY_INSTRUCTOR_NAME));   
       // adding each child node to HashMap key => value 
       hashMap.put("Subject", subject); 
       hashMap.put("Day", day); 
       hashMap.put("Slot", slot); 
       hashMap.put("Instructor", instructor); 
       Timetablelist.add(hashMap); 
       ListAdapter adapter = new SimpleAdapter(
           this, Timetablelist,R.layout.list_item, new String[] { "Subject", "Day", 
           "Slot","Instructor" }, new int[] { R.id.subject, 
           R.id.day, R.id.slot,R.id.instructor }); 
       setListAdapter(adapter);     

       }while (c.moveToNext()); 

         } 

P.S:我能夠使用普通的listview檢索數據。所以數據庫和其他佈局工作正常。

+0

移動setListAdapter跳出DO的同時 – Raghunandan

+0

還要移動適配器實例:'ListAdapter適配器=新SimpleAdapter(' – ramaral

+0

移動的時候,做下段,以正確的,而循環,但仍然是相同的。最後一行在列表視圖中重複8次。 –

回答

1

移動下面出來做雖然

ListAdapter adapter = new SimpleAdapter(
          this, Timetablelist,R.layout.list_item, new String[] { "Subject", "Day", 
          "Slot","Instructor" }, new int[] { R.id.subject, 
          R.id.day, R.id.slot,R.id.instructor }); 
setListAdapter(adapter); 

你從數據庫中的數據,您可以在同時做填充Timetablelist。但是不需要每次都初始化適配器和setListAdapter。

編輯:

HashMap<String, String> hashMap ; 
ArrayList<HashMap<String, String>> Timetablelist = new ArrayList<HashMap<String,String>>(); 
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { 
    String subject = c.getString(c.getColumnIndex(dba.KEY_SUBJECT)); 
    String day = c.getString(c.getColumnIndex(dba.KEY_DAY)); 
    String slot = c.getString(c.getColumnIndex(dba.KEY_SLOT)); 
    String instructor = c.getString(c.getColumnIndex(dba.KEY_INSTRUCTOR_NAME));   
    // adding each child node to HashMap key => value 
    hashMap = new HashMap<String, String>(); 
    hashMap.put("Subject", subject); 
    hashMap.put("Day", day); 
    hashMap.put("Slot", slot); 
    hashMap.put("Instructor", instructor); 
    Timetablelist.add(hashMap); 
} 
ListAdapter adapter = new SimpleAdapter(
          this, Timetablelist,R.layout.list_item, new String[] { "Subject", "Day", 
          "Slot","Instructor" }, new int[] { R.id.subject, 
          R.id.day, R.id.slot,R.id.instructor }); 
setListAdapter(adapter); 
+0

嘗試了這一點。將此代碼片段移動到右邊while(c.moveToNext());。仍然是一樣的 –

+1

@Sash_kp如果移動它下面的代碼您的列表尚未填充 – Raghunandan

+1

@Sash_kp也檢查我的編輯現在你有hashmap的陣列列表,你需要hashmap每次你從數據庫中獲取新數據 – Raghunandan