Ja jeg vil gerne have det løst er kommet lidt længere med min kode, så paster lige det jeg har fået lavet. Forhånd tak for jeres hjælp :0)
here goes:
Items.java- package com.example.rene.houseenabler.View;
-
- import android.app.Activity;
- import android.content.Context;
-
- import android.database.Cursor;
-
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteStatement;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
-
- import android.view.View;
- import android.widget.ExpandableListAdapter;
- import android.widget.ExpandableListView;
- import android.widget.SimpleCursorTreeAdapter;
-
- import com.example.rene.houseenabler.Database.Connection;
- import com.example.rene.houseenabler.R;
-
-
-
- public class Items extends Activity {
-
- Connection conn;
- SQLiteDatabase db;
- ExpandableListAdapter listAdapter;
- ExpandableListView elv;
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_items);
-
-
- elv = (ExpandableListView)findViewById(R.id.lvExp);
-
-
- // open the connection
- openDB();
-
-
- fillData();
-
- }
-
- private void openDB()
- {
- conn = new Connection(this);
- conn.open();
- }
-
- private void fillData()
- {
-
- Cursor mGroupsCursor;
-
- mGroupsCursor = conn.fetchParrent();
-
-
- startManagingCursor(mGroupsCursor);
-
- mGroupsCursor.moveToFirst();
-
- ExpandableListView elv = (ExpandableListView) findViewById(R.id.lvExp);
-
- MyExpandableListAdapter madapter;
-
-
-
-
- madapter = new MyExpandableListAdapter(mGroupsCursor, Items.this,
- R.layout.activity_parrent, // Your row layout for a group
- R.layout.activity_child, // Your row layout for a child
- new String[] { "_idchild" }, // Field(s) to use from group cursor
- new int[] { R.id.lblListHeader }, // Widget ids to put group data into
- new String[] { "childname" }, // Field(s) to use from child cursors
- new int[] { R.id.lblListItem }); // Widget ids to put child data into
-
- elv.setAdapter(madapter); // set the list adapter.
-
-
-
-
-
- elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener()
- {
- @Override
- public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
- {
- // Your child click code here
- return true;
- }
- });
-
-
-
-
- }
-
-
-
- public class MyExpandableListAdapter extends SimpleCursorTreeAdapter
- {
- public MyExpandableListAdapter(Cursor cursor, Context context, int groupLayout, int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom, int[] childrenTo)
- {
- super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childrenFrom, childrenTo);
- }
-
- @Override
- protected Cursor getChildrenCursor(Cursor groupCursor)
- {
- Cursor childCursor = conn.fetchChildren(groupCursor.getString(groupCursor.getColumnIndex("category")));
- startManagingCursor(childCursor);
- childCursor.moveToFirst();
- return childCursor;
- }
-
- }
-
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_items, menu);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
-
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
-
- return super.onOptionsItemSelected(item);
- }
- }
Connection.java- package com.example.rene.houseenabler.Database;
-
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.database.sqlite.SQLiteStatement;
- import android.nfc.Tag;
- import android.util.Log;
- import com.example.rene.houseenabler.Model.User;
-
- import java.sql.SQLException;
-
-
- /**
- * Created by Rene on 14-10-2015.
- */
- public class Connection
- {
- private static final String TAG = "Connection"; //used for logging database version changes
-
-
- // table user
- public static final String TABLE_USERS = "users";
- public static final String COLUMN_USER_ID = "_id";
- public static final String COLUMN_USERNAME = "username";
- public static final String COLUMN_PASSWORD = "password";
-
- // table parent
- public static final String TABLE_ITEMS_PARRENT = "parrent";
- public static final String COLUMN_ITEM_PARRENT_ID = "_idparrent";
- public static final String COLUMN_ITEM_PARRENT_NAME = "parrentname";
-
-
- // table child
- public static final String TABLE_ITEM_CHILD = "child";
- public static final String COLUMN_ITEM_CHILD_ID = "_idchild";
- public static final String COLUMN_ITEM_CHILD_CATEGORY = "category";
- public static final String COLUMN_ITEM_CHILD_NAME = "childname";
- public static final String COLUMN_ITEM_CHILD_DESCRIPTION = "description";
-
-
- private static final String DATABASE_NAME = "house_enabler.db";
-
- private static final int DATABASE_VERSION = 1;
-
-
- private final Context context;
- private Connection conn;
- private DatabaseHelper myDBHelper;
- private SQLiteDatabase db;
-
-
- public Connection(Context ctx)
- {
- this.context = ctx;
- myDBHelper = new DatabaseHelper(context);
- }
-
- // Open the database connection.
- public Connection open()
- {
- db = myDBHelper.getWritableDatabase();
- return this;
- }
-
-
- // Close the database connection.
- public void close()
- {
- myDBHelper.close();
- }
-
- public Cursor fetchParrent()
- {
- String query = "SELECT * FROM parrent";
- return myDBHelper.getReadableDatabase().rawQuery(query, null);
- }
-
- public Cursor fetchChildren(String _idchild)
- {
- String query = "SELECT * FROM child WHERE category = '" + _idchild + "'";
- return myDBHelper.getReadableDatabase().rawQuery(query, null);
- }
-
-
-
-
-
-
-
-
- private static class DatabaseHelper extends SQLiteOpenHelper
- {
- DatabaseHelper(Context context)
- {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
-
- @Override
- public void onCreate(SQLiteDatabase _db)
- {
-
-
- // create table user.
-
- String query_user = "CREATE TABLE " + TABLE_USERS + "(" + COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_USERNAME + " TEXT, " + COLUMN_PASSWORD + " TEXT " + ");";
-
- String query_parrent = "CREATE TABLE " + TABLE_ITEMS_PARRENT + "(" + COLUMN_ITEM_PARRENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_ITEM_PARRENT_NAME + " TEXT " + ");";
-
- String query_child = "CREATE TABLE " + TABLE_ITEM_CHILD + "(" + COLUMN_ITEM_CHILD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_ITEM_CHILD_CATEGORY + " INTEGER, " + COLUMN_ITEM_CHILD_NAME + " TEXT, " + COLUMN_ITEM_CHILD_DESCRIPTION + " TEXT, " + " FOREIGN KEY ("+COLUMN_ITEM_CHILD_CATEGORY+") REFERENCES "+ TABLE_ITEMS_PARRENT +"(" +COLUMN_ITEM_PARRENT_ID +"));";
-
- _db.execSQL(query_user);
- _db.execSQL(query_parrent);
- _db.execSQL(query_child);
-
-
-
-
-
- // insert to tables
-
- String A = "INSERT INTO parrent (_idparrent, parrentname) VALUES (1, 'A')";
- String B = "INSERT INTO parrent (_idparrent, parrentname) VALUES (2, 'B')";
- String C = "INSERT INTO parrent (_idparrent, parrentname) VALUES (3, 'C')";
- SQLiteStatement statement1 = _db.compileStatement(A);
- SQLiteStatement statement2 = _db.compileStatement(B);
- SQLiteStatement statement3 = _db.compileStatement(C);
- long rowId1 = statement1.executeInsert();
- long rowId2 = statement2.executeInsert();
- long rowId3 = statement3.executeInsert();
-
- //Insert to child ABC from 1 - 161 for each
-
- int category_a = 1;
- String childname_a = "A";
-
- int category_b = 2;
- String childname_b = "B";
-
- int category_c = 3;
- String childname_c = "C";
-
- String description = "Indsæt beskrivelse her";
-
-
- try
-
- {
-
- _db.beginTransaction();
- String sql = "INSERT INTO child (_idchild, category, childname, description) VALUES (?, ?, ?, ?)";
- SQLiteStatement statement = _db.compileStatement(sql);
-
- int increase_a = 1;
-
- for (int i = 1; i < 162; i++)
- {
- statement.clearBindings();
- statement.bindLong(1, i);
- statement.bindLong(2, category_a); //+ i
- statement.bindString(3, childname_a + increase_a++);
- statement.bindString(4, description);
- statement.executeInsert();
-
- }
-
-
- int increase_b = 1;
-
- for (int j = 162; j < 323; j++)
- {
- statement.clearBindings();
- statement.bindLong(1, j);
- statement.bindLong(2, category_b);
- statement.bindString(3, childname_b + increase_b++);
- statement.bindString(4, description);
- statement.executeInsert();
-
- }
-
- int increase_c = 1;
-
- for (int k = 323; k < 484 ; k++)
- {
- statement.clearBindings();
- statement.bindLong(1, k);
- statement.bindLong(2, category_c);
- statement.bindString(3, childname_c + increase_c++);
- statement.bindString(4, description);
- statement.executeInsert();
-
- }
-
- _db.setTransactionSuccessful(); // This commits the transaction if there were no exceptions
-
- }
- catch (Exception e)
- {
- Log.w("Exception:", e);
- } finally {
- _db.endTransaction();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion)
- {
- Log.w(TAG, "Upgrading application's database from version " + oldVersion
- + " to " + newVersion + ", which will destroy all old data!");
-
- // Destroy old database:
- _db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
- _db.execSQL("DROP TABLE IF EXISTS " + TABLE_ITEMS_PARRENT);
- _db.execSQL("DROP TABLE IF EXISTS " + TABLE_ITEM_CHILD);
-
- // Recreate new database:
- onCreate(_db);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
Error full stack10-29 23:40:23.138 951-951/? D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so
10-29 23:40:23.141 951-951/? D/: HostConnection::get() New Host Connection established 0xb70921c0, tid 951
10-29 23:40:23.835 1025-1030/? D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so
10-29 23:40:23.844 1025-1030/? D/: HostConnection::get() New Host Connection established 0xb655c2b0, tid 1030
10-29 23:40:23.850 951-979/? D/: HostConnection::get() New Host Connection established 0xb7092580, tid 979
10-29 23:40:24.117 951-982/? D/: HostConnection::get() New Host Connection established 0xb7092c20, tid 982
10-29 23:40:31.331 1293-1293/? I/InstallerConnection: connecting...
10-29 23:40:31.344 1293-1293/? I/InstallerConnection: disconnecting...
10-29 23:40:31.457 1293-1293/? I/InstallerConnection: connecting...
10-29 23:40:39.639 1293-1293/? I/SystemServer: BackgroundDexOptService
10-29 23:40:39.641 1293-1363/? D/: HostConnection::get() New Host Connection established 0xa0886540, tid 1363
10-29 23:40:39.818 1293-1314/? W/KeyguardServiceDelegate: onScreenTurningOn(): no keyguard service!
10-29 23:40:39.819 951-1334/? D/: HostConnection::get() New Host Connection established 0xb708b090, tid 1334
10-29 23:40:39.826 1293-1293/? W/MountService: Failed to remount UID 10001 as none: com.android.server.NativeDaemonConnector$NativeDaemonFailureException: command '17 volume remount_uid 10001 none' failed with '400 17 Command failed'
10-29 23:40:39.837 1293-1312/? D/: HostConnection::get() New Host Connection established 0x9fabf210, tid 1312
10-29 23:40:40.092 1293-1329/? I/ActivityManager: Launching preboot mode app: ProcessRecord{37ac7e4 1391:android.process.acore/u0a2}
10-29 23:40:40.101 1293-1293/? D/: HostConnection::get() New Host Connection established 0x9fabf550, tid 1293
10-29 23:40:44.430 1488-1879/? D/OpenGLRenderer: ambientRatio = 1.50
10-29 23:40:44.433 1488-1488/? D/: HostConnection::get() New Host Connection established 0xab78aea0, tid 1488
10-29 23:40:44.501 1488-1488/? D/ViewRootImpl: changeCanvasOpacity: opaque=true
10-29 23:40:44.539 1488-1879/? D/: HostConnection::get() New Host Connection established 0xac03df30, tid 1879
10-29 23:40:48.641 1721-1721/? I/Telephony: AccountEntry: Registered phoneAccount: [[ ] PhoneAccount: ComponentInfo{com.android.phone/com.android.services.telephony.TelephonyConnectionService}, [e0184adedf913b076626646d3f52c3b49c39ad6d], UserHandle{0} Capabilities: 54 Schemes: tel voicemail ] with handle: ComponentInfo{com.android.phone/com.android.services.telephony.TelephonyConnectionService}, [e0184adedf913b076626646d3f52c3b49c39ad6d], UserHandle{0}
10-29 23:40:49.319 1293-1470/? I/Telecom: PhoneAccountRegistrar: New phone account registered: [[ ] PhoneAccount: ComponentInfo{com.android.phone/com.android.services.telephony.TelephonyConnectionService}, [e2f7d48dd2b5ca523e7313cf4ba0f6ea830b6281], UserHandle{0} Capabilities: 54 Schemes: tel voicemail ]
10-29 23:40:49.334 1721-1721/? I/Telephony: AccountEntry: Registered phoneAccount: [[ ] PhoneAccount: ComponentInfo{com.android.phone/com.android.services.telephony.TelephonyConnectionService}, [e2f7d48dd2b5ca523e7313cf4ba0f6ea830b6281], UserHandle{0} Capabilities: 54 Schemes: tel voicemail ] with handle: ComponentInfo{com.android.phone/com.android.services.telephony.TelephonyConnectionService}, [e2f7d48dd2b5ca523e7313cf4ba0f6ea830b6281], UserHandle{0}
10-29 23:40:49.337 1721-1721/? I/Telephony: TelecomAccountRegistry: Unregistering phone account ComponentInfo{com.android.phone/com.android.services.telephony.TelephonyConnectionService}, [e0184adedf913b076626646d3f52c3b49c39ad6d], UserHandle{0}.
10-29 23:40:51.091 1752-1752/? E/GmsWearableLS: GoogleApiClient connection failed: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}
10-29 23:40:53.283 1752-1752/? E/GmsWearableLS: GoogleApiClient connection failed: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}
10-29 23:40:53.308 1752-2177/? E/MDM: [106] b.run: Couldn't connect to Google API client: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}
10-29 23:40:57.362 2281-2340/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-29 23:40:57.364 2281-2281/? D/: HostConnection::get() New Host Connection established 0xab78ae20, tid 2281
10-29 23:40:57.392 1488-1488/? D/PhoneStatusBar: disable: < expand* icons alerts system_info back* home* recent* clock search* quick_settings >
10-29 23:40:57.423 2281-2340/? D/: HostConnection::get() New Host Connection established 0xaef3cf30, tid 2340
10-29 23:40:59.606 1752-2357/? E/MDM: [108] b.run: Couldn't connect to Google API client: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}
10-29 23:41:00.390 1752-2177/? E/MDM: [106] b.run: Couldn't connect to Google API client: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}
10-29 23:41:00.417 1752-2357/? E/MDM: [108] b.run: Couldn't connect to Google API client: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}
10-29 23:41:01.599 1293-1304/? I/AccountManagerService: getTypesVisibleToCaller: isPermitted? true
10-29 23:41:01.603 1946-2391/? D/: HostConnection::get() New Host Connection established 0x9c9ffb40, tid 2391
10-29 23:41:02.382 2489-2502/? W/System: ClassLoader referenced unknown path:
10-29 23:41:02.385 2489-2502/? D/: HostConnection::get() New Host Connection established 0x9dafd0e0, tid 2502
10-29 23:41:03.930 1752-2177/? E/MDM: [106] b.run: Couldn't connect to Google API client: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}
10-29 23:42:21.214 2836-2857/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-29 23:42:21.218 2836-2836/? D/: HostConnection::get() New Host Connection established 0xad77ad90, tid 2836
10-29 23:42:21.319 2836-2857/? D/: HostConnection::get() New Host Connection established 0xb3f883e0, tid 2857
10-29 23:42:22.322 1706-2868/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-29 23:42:22.340 1706-1706/? D/: HostConnection::get() New Host Connection established 0xab724e20, tid 1706
10-29 23:42:22.490 1706-1706/? I/LatinIME: Starting input. Cursor position = 0,0
10-29 23:42:22.501 1706-2868/? D/: HostConnection::get() New Host Connection established 0xab724aa0, tid 2868
10-29 23:42:24.905 1293-2870/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-29 23:42:24.941 1293-2870/? D/: HostConnection::get() New Host Connection established 0x9e1bfbe0, tid 2870
10-29 23:42:32.186 1293-1469/? I/ActivityManager: Process com.example.rene.houseenabler (pid 2836) has died
10-29 23:42:32.217 951-2874/? D/: HostConnection::get() New Host Connection established 0xb708bce0, tid 2874
10-29 23:43:06.005 1864-2915/? W/System.err: at com.android.okhttp.Connection.connect(Connection.java:143)
10-29 23:43:06.005 1864-2915/? W/System.err: at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:185)
10-29 23:43:06.005 1864-2915/? W/System.err: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:341)
10-29 23:43:06.005 1864-2915/? W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
10-29 23:43:06.005 1864-2915/? W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
10-29 23:43:06.005 1864-2915/? W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245)
10-29 23:43:06.005 1864-2915/? W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
10-29 23:43:06.005 1864-2915/? W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java)
10-29 23:43:06.165 1864-1886/? E/StrictMode: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:439)
10-29 23:43:06.165 1864-1886/? E/StrictMode: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
10-29 23:43:06.165 1864-1886/? E/StrictMode: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
10-29 23:43:06.165 1864-1886/? E/StrictMode: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
10-29 23:43:06.165 1864-1886/? E/StrictMode: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java)
10-29 23:43:18.718 2973-2989/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-29 23:43:18.719 2973-2973/? D/: HostConnection::get() New Host Connection established 0xad77ae10, tid 2973
10-29 23:43:18.760 2973-2989/? D/: HostConnection::get() New Host Connection established 0xb401ca30, tid 2989
10-29 23:43:18.810 2973-2989/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xab78ca20, error=EGL_SUCCESS
10-29 23:43:18.813 951-2875/? D/: HostConnection::get() New Host Connection established 0xb708bc90, tid 2875
-