lundi 26 octobre 2015

Android SQLite to listview

I want to get the ID and Title from my SQLite database to a listview. I only need to show the title and need the ID for an onclickListener. I want the MyDbHandler.COLUMN_TITLE to the Textview with the ID: workshopTitle1.

How would I get that to a listview? I've searched everywhere but nothing worked :( Also I'm fairly new to this..

MyDbHandler.java

public class MyDbHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 6;
private static final String DATABASE_NAME = "workshopDB";
private static final String TABLE_WORKSHOPS = "workshops";

public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE= "title";
public static final String COLUMN_BESCHRIJVING = "beschrijving";
public static final String COLUMN_MGT = "mgt";
public static final String COLUMN_DOC = "doc";
public static final String COLUMN_OOP = "oop";
public static final String COLUMN_BEGELEIDER = "begeleider";
public static final String COLUMN_DEELNEMERS = "deelnemers";
public static final String COLUMN_RONDE1 = "ronde1";
public static final String COLUMN_RONDE2 = "ronde2";
public static final String COLUMN_RONDE3 = "ronde3";
public static final String COLUMN_LOKAAL1 = "lokaal1";
public static final String COLUMN_LOKAAL2 = "lokaal2";
public static final String COLUMN_LOKAAL3 = "lokaal3";

public MyDbHandler(Context context, String name,
                   SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_WORKSHOPS_TABLE = "CREATE TABLE " +
            TABLE_WORKSHOPS + "("
            + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_TITLE
            + " TEXT," + COLUMN_BESCHRIJVING + " TEXT,"
            + COLUMN_MGT + " TEXT," + COLUMN_DOC + " TEXT ," + COLUMN_OOP + " TEXT," + COLUMN_BEGELEIDER + " TEXT," + COLUMN_DEELNEMERS + " TEXT," + COLUMN_RONDE1 + " TEXT,"
            + COLUMN_RONDE2 + " TEXT," + COLUMN_RONDE3 + " TEXT," + COLUMN_LOKAAL1 + " TEXT," + COLUMN_LOKAAL2 + " TEXT," + COLUMN_LOKAAL3 + " TEXT" +")";
    db.execSQL(CREATE_WORKSHOPS_TABLE);
}

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

}

public void addWorkshop(Workshop workshop) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(COLUMN_TITLE, workshop.get_title());
    values.put(COLUMN_BESCHRIJVING, workshop.get_beschrijving());
    values.put(COLUMN_MGT, workshop.get_mgt());
    values.put(COLUMN_DOC, workshop.get_doc());
    values.put(COLUMN_OOP, workshop.get_oop());
    values.put(COLUMN_BEGELEIDER, workshop.get_begeleider());
    values.put(COLUMN_DEELNEMERS, workshop.get_deelnemers());
    values.put(COLUMN_RONDE1, workshop.get_ronde1());
    values.put(COLUMN_RONDE2, workshop.get_ronde2());
    values.put(COLUMN_RONDE3, workshop.get_ronde3());
    values.put(COLUMN_LOKAAL1, workshop.get_lokaal1());
    values.put(COLUMN_LOKAAL2, workshop.get_lokaal2());
    values.put(COLUMN_LOKAAL3, workshop.get_lokaal3());

    db.insert(TABLE_WORKSHOPS, null, values);
    db.close();
}

// Getting All Contacts
public List<Workshop> getAllContacts() {
    List<Workshop> workshoplijst = new ArrayList<Workshop>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_WORKSHOPS + "";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Workshop workshop = new Workshop();
            workshop.set_id(Integer.parseInt(cursor.getString(0)));
            workshop.set_beschrijving(cursor.getString(1));
            workshop.set_mgt(cursor.getString(2));
            workshop.set_doc(cursor.getString(3));
            workshop.set_oop(cursor.getString(4));
            workshop.set_begeleider(cursor.getString(5));
            workshop.set_deelnemers(cursor.getString(6));
            workshop.set_ronde1(cursor.getString(7));
            workshop.set_ronde2(cursor.getString(8));
            workshop.set_ronde3(cursor.getString(9));
            workshop.set_lokaal1(cursor.getString(10));
            workshop.set_lokaal2(cursor.getString(11));
            workshop.set_lokaal3(cursor.getString(12));
            // Adding contact to list
            workshoplijst.add(workshop);
        } while (cursor.moveToNext());
    }

    // return contact list
    return workshoplijst;
}
}

And here's the fragment I want to show it in: FragmentRonde1.java

public class FragmentRonde1 extends android.support.v4.app.ListFragment{

private ListView listViewRonde1;

public FragmentRonde1() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootview = inflater.inflate(R.layout.fragment_ronde1, container, false);
    ((MainActivity) getActivity()).setTitle("Workshops Ronde 1");
    listViewRonde1 = (ListView) rootview.findViewById(R.id.listView1);

    MyDbHandler db = new MyDbHandler(getActivity(), null, null, 1);

    db.addWorkshop(new Workshop(1, "Title", "Description", "etc", "etc", "etc", "etc", "etc", "etc", "etc", "etc", "etc", "etc", "etc"));

    return rootview;
    }

}

FragmentRonde1.xml

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
         xmlns:tools="http://ift.tt/LrGmb4"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context="justin.drawer.FragmentRonde1"
android:orientation="horizontal">

<ListView
    android:id="@+id/listViewRonde1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:divider="#dedede"
    android:dividerHeight="0.1dp"/>
</LinearLayout>

row_layout_workshop

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<TextView
    android:id="@+id/workshopTitle1"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:text="Hoe ondersteunt het OSB bij plannen/monitoren van CE's T&amp;R"
    android:textSize="14sp"
    android:textColor="#000"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/ic_chevron_right"
    android:layout_centerVertical="true"/>
</RelativeLayout>

Aucun commentaire:

Enregistrer un commentaire