Pregunta

I am retrieving data from sqlite and showing it in a listview using simpleadapter.I don't have any issues regarding Layouts.Still just for your reference :

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>

I am having problem somewhere in the following code snippet.I tried to retrieve the data from sqlite using normal listview and it worked perfect.But when i used this code snippet for custom listview using SimpleAdapter i get only the last row of the result of the query.That last row's data gets repeated 8 times in the listview as there are 8 rows in the result of the query.

 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 : I was able to retrieve data using normal listview.So database and other layouts are working fine.

¿Fue útil?

Solución

Move the below out of do While

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

You get the data from database and you can populate Timetablelist in the do while. But there is no need to initialize adapter everytime and setListAdapter.

Edit:

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);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top