android - Structure for SQLite database info to ListView -
i've seen few answers here, lot of answers either going on head or can't relate them app. wondering if break down little more me i'm kind of new programming .
i'm trying fill expandablelistview data sqlite database in android. have class called databasehelper creates database/tables , adds , removes data, can recall information through cursor display data in alertdialog. i'm trying have information dynamically loaded expandablelistview.
i have method in database helper:
public cursor getalldata() { sqlitedatabase db = this.getwritabledatabase(); cursor res = db.rawquery("select * "+ table_name, null); return res;
which returns cursor object table inside. fill listview understanding need use simplecursoradapter , apply listview. need create seperate class simplecursoradapter? , if how apply simplecursoradaptor expandablelistview?
all code below.
mainactivity.java
public class mainactivity extends appcompatactivity { databasehelper mydb; edittext editname, editsurname, editage; button btnadddata, btnviewall; listview listview; private simplecursoradapter dataadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mydb = new databasehelper(this); cursor c = mydb.getreadabledatabase().rawquery("select * " + databasehelper.table_name, null); editname = (edittext) findviewbyid(r.id.edittext_name); editsurname = (edittext) findviewbyid(r.id.edittext_surname); editage = (edittext) findviewbyid(r.id.edittext_age); btnadddata = (button) findviewbyid(r.id.button_add); btnviewall = (button) findviewbyid(r.id.button_viewall); listview = (listview) findviewbyid(r.id.expandablelistview); displaylistview(); adddata(); viewall(); } private void displaylistview() { cursor cursor = mydb.getalldata(); string[] columns = new string[] { databasehelper.col1, databasehelper.col2, databasehelper.col3, databasehelper.col4 }; int[] = new int[] { r.id.textview2, r.id.textview4, r.id.textview6, }; dataadapter = new simplecursoradapter( this, r.layout.listlayout, cursor, columns, to, 0); listview.setadapter(dataadapter); } public void viewall() { btnviewall.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { cursor res = mydb.getalldata(); if (res.getcount() == 0) { // show message (no data in table) showmessage("error", "no data found"); return; } stringbuffer buffer = new stringbuffer(); while (res.movetonext()) { buffer.append("id: "+ res.getstring(0) + "\n"); buffer.append("name: "+ res.getstring(1) + "\n"); buffer.append("surname: "+ res.getstring(2) + "\n"); buffer.append("age: "+ res.getstring(3) + "\n"); } showmessage("data", buffer.tostring()); } } ); } public void showmessage(string title, string message){ alertdialog.builder builder = new alertdialog.builder(this); builder.setcancelable(true); builder.settitle(title); builder.setmessage(message); builder.show(); } public void adddata() { btnadddata.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { boolean isinserted = mydb.insertdata(editname.gettext().tostring(), editsurname.gettext().tostring(), editage.gettext().tostring()); if(isinserted = true) { toast.maketext(mainactivity.this, "data inserted", toast.length_short).show(); } else { toast.maketext(mainactivity.this, "data not inserted", toast.length_short).show(); } } } ); }
}
databasehelper.java
public class databasehelper extends sqliteopenhelper{ public static final string database_name = "people.db"; public static final string table_name = "people_table"; public static final string col1 = "id"; public static final string col2 = "name"; public static final string col3 = "surname"; public static final string col4 = "age"; public databasehelper(context context) { super(context, database_name, null, 1); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table " + table_name + " (id integer primary key autoincrement,name text,surname text,age integer)"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + table_name); oncreate(db); } public boolean insertdata(string name, string surname, string age) { sqlitedatabase db = this.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put(col2, name); contentvalues.put(col3, surname); contentvalues.put(col4, age); long result = db.insert(table_name, null, contentvalues); if (result == -1) return false; else return true; } public cursor getalldata() { sqlitedatabase db = this.getwritabledatabase(); cursor res = db.rawquery("select * "+ table_name, null); return res; } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.welcometech.database.mainactivity"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="name" android:id="@+id/textview" android:layout_alignparenttop="true" android:layout_toleftof="@+id/edittext_name" android:layout_tostartof="@+id/edittext_name" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="surname" android:id="@+id/textview2" android:layout_below="@+id/textview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_margintop="44dp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="age" android:id="@+id/textview3" android:layout_margintop="37dp" android:layout_below="@+id/textview2" android:layout_toleftof="@+id/edittext_age" android:layout_tostartof="@+id/edittext_age" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edittext_name" android:layout_aligntop="@+id/textview" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_torightof="@+id/textview2" android:layout_toendof="@+id/textview2" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edittext_surname" android:layout_above="@+id/textview3" android:layout_alignright="@+id/edittext_name" android:layout_alignend="@+id/edittext_name" android:layout_torightof="@+id/textview2" android:layout_toendof="@+id/textview2" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edittext_age" android:layout_alignbottom="@+id/textview3" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_torightof="@+id/textview2" android:layout_toendof="@+id/textview2" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="add data" android:id="@+id/button_add" android:layout_centervertical="true" android:layout_below="@+id/textview3" android:layout_margintop="20dp" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="view all" android:id="@+id/button_viewall" android:layout_aligntop="@+id/button_add" android:layout_torightof="@+id/button_add" android:layout_toendof="@+id/button_add" /> <expandablelistview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/expandablelistview" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_below="@+id/button_add" /> </relativelayout>
listlayout.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.welcometech.database.mainactivity"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="name" android:id="@+id/textview" android:layout_alignparenttop="true" android:layout_toleftof="@+id/edittext_name" android:layout_tostartof="@+id/edittext_name" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="surname" android:id="@+id/textview2" android:layout_below="@+id/textview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_margintop="44dp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="age" android:id="@+id/textview3" android:layout_margintop="37dp" android:layout_below="@+id/textview2" android:layout_toleftof="@+id/edittext_age" android:layout_tostartof="@+id/edittext_age" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edittext_name" android:layout_aligntop="@+id/textview" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_torightof="@+id/textview2" android:layout_toendof="@+id/textview2" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edittext_surname" android:layout_above="@+id/textview3" android:layout_alignright="@+id/edittext_name" android:layout_alignend="@+id/edittext_name" android:layout_torightof="@+id/textview2" android:layout_toendof="@+id/textview2" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edittext_age" android:layout_alignbottom="@+id/textview3" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_torightof="@+id/textview2" android:layout_toendof="@+id/textview2" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="add data" android:id="@+id/button_add" android:layout_centervertical="true" android:layout_below="@+id/textview3" android:layout_margintop="20dp" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="view all" android:id="@+id/button_viewall" android:layout_aligntop="@+id/button_add" android:layout_torightof="@+id/button_add" android:layout_toendof="@+id/button_add" /> <expandablelistview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/expandablelistview" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_below="@+id/button_add" /> </relativelayout>
you need create 1 adapter. in need pass
arraylist <hashmap<string,string>>
your getalldata() method should :
public arraylist<hashmap<string,string>> getalldata(){ sqlitedatabase db = this.getwritabledatabase(); arraylist<hashmap<string,string>> list = new arraylist<hashmap<string,string>>(); hashmap<string,string> hashmap; string query_select = "select column1, column2 tablename ; cursor cursor = db.rawquery(query_select,null); if (cursor.movetofirst()) { { hashmap = new hashmap<string,string>(); hashmap.put("column1", cursor.getstring(0)); hashmap.put("column2",cursor.getstring(1)); list.add(hashmap); } while (cursor.movetonext()); } cursor.close(); return list; }
this method return arraylist , need set adapter , set adapter listview.
Comments
Post a Comment