1

I would like to display data from sqlite as children in ExpandableListView. I have 2 tables - 1 contains days and 1 contains school courses for whole week.

I would like to have day names as parents, and courses as children to it.

I have codes like that:

DBHelper:

    package pl.mapapwr.utils;

import java.util.ArrayList;

import pl.mapapwr.models.CourseModel;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

public static String DATABASENAME = "androidadvancesqlite";
public static String COURSETABLE = "courses";
public static String colCourseName = "coursename";
public static String colCourseType = "coursetype";
public static String colWeek = "week";
public static String colWeekDay = "weekday";
public static String colStartHour = "starthour";
public static String colStartMin = "startmin";
public static String colEndHour = "endhour";
public static String colEndMin = "endmin";
public static String colBuilding = "building";
public static String colRoom = "room";
public static String colLecturer = "lecturer";

private ArrayList<CourseModel> courseList = new ArrayList<CourseModel>();
Context c;

public DatabaseHelper(Context context) {
    super(context, DATABASENAME, null, 1);
    c = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE if not exists DAYS (id_day INTEGER PRIMARY KEY,"
            + "day_name" + "TEXT)");
    db.execSQL("INSERT INTO DAYS (id_day, day_name) VALUES ('0', 'Poniedziałek')");
    db.execSQL("INSERT INTO DAYS (id_day, day_name) VALUES ('1', 'Wtorek')");
    db.execSQL("INSERT INTO DAYS (id_day, day_name) VALUES ('2', 'Środa')");
    db.execSQL("INSERT INTO DAYS (id_day, day_name) VALUES ('3', 'Czwartek')");
    db.execSQL("INSERT INTO DAYS (id_day, day_name) VALUES ('4', 'Piątek')");

    db.execSQL("CREATE TABLE if not exists COURSETABLE(id INTEGER PRIMARY KEY AUTOINCREMENT,"
            + "coursename"
            + " TEXT,"
            + "coursetype"
            + " TEXT,"
            + "week"
            + " TEXT,"
            + "weekday"
            + " TEXT references DAYS,"
            + "starthour"
            + " TEXT,"
            + "startmin"
            + " TEXT,"
            + "endhour"
            + " TEXT,"
            + "endmin"
            + " TEXT,"
            + "building"
            + " TEXT,"
            + "room"
            + " TEXT," + "lecturer" + " TEXT)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + COURSETABLE);
    onCreate(db);
}

public void addCourse(CourseModel scheduleitem) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("coursename", scheduleitem.coursename);
    contentValues.put("coursetype", scheduleitem.coursetype);
    contentValues.put("week", scheduleitem.week);
    contentValues.put("weekday", scheduleitem.weekday);
    contentValues.put("starthour", scheduleitem.starthour);
    contentValues.put("startmin", scheduleitem.startmin);
    contentValues.put("endhour", scheduleitem.endhour);
    contentValues.put("endmin", scheduleitem.endmin);
    contentValues.put("building", scheduleitem.building);
    contentValues.put("room", scheduleitem.room);
    contentValues.put("lecturer", scheduleitem.lecturer);
    db.insert("COURSETABLE", null, contentValues);

    db.close();

}

public void emptyDatabase() {
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("delete from COURSETABLE");
        db.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public ArrayList<CourseModel> getCourses() {

    courseList.clear();

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("select * from COURSETABLE", null);
    if (cursor.getCount() != 0) {
        if (cursor.moveToFirst()) {
            do {
                CourseModel item = new CourseModel();

                item.coursename = cursor.getString(cursor
                        .getColumnIndex("coursename"));

                item.week = cursor.getString(cursor.getColumnIndex("week"));

                courseList.add(item);

            } while (cursor.moveToNext());
        }
    }
    cursor.close();
    db.close();
    return courseList;
}
}

Fragment to display:

package pl.mapapwr.fragments;

import pl.mapapwr.R;
import pl.mapapwr.utils.DatabaseHelper;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ScheduleFragment extends BaseFragment {

Context mCtx;
DatabaseHelper dbh = new DatabaseHelper(mCtx);

public static ScheduleFragment newInstance(Context ctx) {
    ScheduleFragment fragment = new ScheduleFragment(ctx);
    return fragment;
}

public ScheduleFragment(Context ctx) {
    mCtx = ctx;
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_schedule, container,
            false);

    return rootView;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

}

Child item xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/navigate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="22dp"
    android:src="@android:drawable/ic_dialog_map" />

<TextView
    android:id="@+id/courseName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="15dp"
    android:layout_toRightOf="@+id/navigate"
    android:text="Course Name"
    android:textColor="@color/main" />


<TextView
    android:id="@+id/courseType"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="15dp"
    android:layout_toRightOf="@+id/courseName"
    android:text="Course Type"
    android:textColor="@color/main" />

<TextView
    android:id="@+id/courseTime"
    style="@style/DetailText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/courseName"
    android:layout_marginLeft="15dp"
    android:layout_toRightOf="@+id/navigate"
    android:text="11:15 - 13:00" />

<TextView
    android:id="@+id/coursePlace"
    style="@style/DetailText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/courseName"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@+id/courseTime"
    android:text="Budynek D-1, sala 311a" />


<TextView
    android:id="@+id/Lecturer"
    style="@style/DetailText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/courseTime"
    android:layout_marginLeft="15dp"
    android:layout_toRightOf="@+id/navigate"
    android:text="dr inż. Krzysztof Nowak" />

</RelativeLayout>

And group layout:

    <?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:background="@drawable/divider_item"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/dayName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="30dp"
        android:text="Day Name"
        android:textColor="@color/main"
        android:textSize="25sp" />

    </LinearLayout>

Can anyone help me with creating adapter for it and displaying it? I am quite fresh in programming and this Android ExpandableListView and SQLite Database didn't help me too much :(

Community
  • 1
  • 1
Tymcio
  • 13
  • 4

0 Answers0