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.

Recommended Answers

All 3 Replies

The problem is that you are adding a new SimpleAdapter each time you run through the loop.

You need to get all your data from the database first and store it in a memory structure. You then add a single adapter to the View, passing in this list of data.

Move this code :

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

Outside the loop to right at the end. That way, you will have populated Timetablelist with all the information and will be adding it only once :)

I changed my code to this and it worked perfect.

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

I have an arraylist of hashmap. I need hashmap everytime I get new data from database. so hashMap = new HashMap<String, String>(); was required too.And like you said i had to move the code for adapter outside the for loop.Now i am adding the data to the adapter once the list gets populated completely.Thank you for your help @Ewald Horn :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.