android - Error When Calling Data From SQLDatabase to ListView -
i have been trying find error in project still have no luck.as new programming can't pin point error. have list view within dialog in main project , doing here trying call data mysql database browser list view. have made new project code don't want mess main project code.
package com.pdb.projectdb; import java.util.arraylist; import android.app.listactivity; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class main1activity extends listactivity { private static final string db_name = "yourdb.sqlite3"; //a practice define database field names constants private static final string table_name = "friends"; private static final string friend_id = "_id"; private static final string friend_name = "name"; private sqlitedatabase database; private listview listview; private arraylist friends; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main1); //our key helper externaldbopenhelper dbopenhelper = new externaldbopenhelper(this, db_name); database = dbopenhelper.opendatabase(); //that’s it, database open! fillfreinds(); setuplist(); } private void setuplist() { //we use standard adapter , element layout brevity’s sake setlistadapter(new arrayadapter<string>(this, android.r.layout.simple_list_item_1, friends)); listview = getlistview(); //let’s set message shown upon tapping item listview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview parent, view view, int position,long id) { toast.maketext(getapplicationcontext(), ((textview) view).gettext() + " softeq's friend", toast.length_short).show(); } } } //extracting elements database private void fillfreinds() { friends = new arraylist<string>(); cursor friendcursor = database.query(table_name, new string[] {friend_id, friend_name}, null, null, null, null, friend_name); friendcursor.movetofirst(); if(!friendcursor.isafterlast()) { { string name = friendcursor.getstring(1); friends.add(name); } while (friendcursor.movetonext()); } friendcursor.close(); } }
xml.file
<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=".main1activity" > <imagebutton android:id="@+id/imagebutton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="114dp" android:src="@drawable/ic_launcher" /> </relativelayout>
dbhelper
package com.pdb.projectdb; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import android.content.context; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class externaldbopenhelper extends sqliteopenhelper { //path device folder databases public static string db_path; //database file name public static string db_name; public sqlitedatabase database; public final context context; public sqlitedatabase getdb() { return database; } public externaldbopenhelper(context context, string databasename) { super(context, databasename, null, 1); this.context = context; //write full path databases of application string packagename = context.getpackagename(); db_path = string.format("/data/data/newdb/databases/", packagename); db_name = databasename; opendatabase(); } //this piece of code create database if it’s not yet created public void createdatabase() { boolean dbexist = checkdatabase(); if (!dbexist) { this.getreadabledatabase(); try { copydatabase(); } catch (ioexception e) { log.e(this.getclass().tostring(), "copying error"); throw new error("error copying database!"); } } else { log.i(this.getclass().tostring(), "database exists"); } } //performing database existence check private boolean checkdatabase() { sqlitedatabase checkdb = null; try { string path = db_path + db_name; checkdb = sqlitedatabase.opendatabase(path, null, sqlitedatabase.open_readonly); } catch (sqlexception e) { log.e(this.getclass().tostring(), "error while checking db"); } //android doesn’t resource leaks, should // closed if (checkdb != null) { checkdb.close(); } return checkdb != null; } //method copying database private void copydatabase() throws ioexception { //open stream reading our ready-made database //the stream source located in assets inputstream externaldbstream = context.getassets().open(db_name); //path created empty database on android device string outfilename = db_path + db_name; //now create stream writing database byte byte outputstream localdbstream = new fileoutputstream(outfilename); //copying database byte[] buffer = new byte[1024]; int bytesread; while ((bytesread = externaldbstream.read(buffer)) > 0) { localdbstream.write(buffer, 0, bytesread); } //don’t forget close streams localdbstream.close(); externaldbstream.close(); } public sqlitedatabase opendatabase() throws sqlexception { string path = db_path + db_name; if (database == null) { createdatabase(); database = sqlitedatabase.opendatabase(path, null, sqlitedatabase.open_readwrite); } return database; } @override public synchronized void close() { if (database != null) { database.close(); } super.close(); } @override public void oncreate(sqlitedatabase db) {} @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {} }
project view http://img441.imageshack.us/img441/1936/projectg.jpg
p.s friend told me not waste question on here can banned please let me know if not formatting question right
thank you
replace this, correctly formatted you
import java.util.arraylist; import android.app.listactivity; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class main1activity extends listactivity { private static final string db_name = "yourdb.sqlite3"; // practice define database field names constants private static final string table_name = "friends"; private static final string friend_id = "_id"; private static final string friend_name = "name"; private sqlitedatabase database; private listview listview; private arraylist friends; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main1); // our key helper externaldbopenhelper dbopenhelper = new externaldbopenhelper(this, db_name); database = dbopenhelper.opendatabase(); // that’s it, database open! fillfreinds(); setuplist(); } private void setuplist() { // use standard adapter , element layout brevity’s sake setlistadapter(new arrayadapter<string>(this, android.r.layout.simple_list_item_1, friends)); listview = getlistview(); // let’s set message shown upon tapping item listview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview parent, view view, int position, long id) { toast.maketext( getapplicationcontext(), ((textview) view).gettext() + " softeq's friend", toast.length_short).show(); } }); } // extracting elements database private void fillfreinds() { friends = new arraylist<string>(); cursor friendcursor = database.query(table_name, new string[] { friend_id, friend_name }, null, null, null, null, friend_name); friendcursor.movetofirst(); if (!friendcursor.isafterlast()) { { string name = friendcursor.getstring(1); friends.add(name); } while (friendcursor.movetonext()); } friendcursor.close(); } }
Comments
Post a Comment