2014-01-07 47 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