EMPTY data inserted into the Android SQLITE table -
i have doubts on programs. i'm unable insert date "producttable".
i can add table directly not thru "edittext". once click add.
the id of records increase other column remains empty.
- following code of program.
enter code here
+++thanks+++
stockmanagement.java
package ping.stock.check; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.util.arraylist; import ping.stock.check.dbadapter.databasehelper; import com.google.zxing.integration.android.intentintegrator; import com.google.zxing.integration.android.intentresult; import android.r.id; import android.support.v4.app.fragment; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.os.build; import android.os.bundle; import android.app.activity; import android.content.intent; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast; import android.app.activity; import android.content.context; import android.content.intent; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.*; import android.database.*; public class stockmanagement extends activity implements onclicklistener{ button button; final context context = this; public button scanbtn, addbtn; public edittext proidtxt, pronametxt, prounittxt, prolowtxt, proracktxt; dbadapter db = new dbadapter(this); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.stock_man); cleartextfield(); tomenu(); scanbtn = (button)findviewbyid(r.id.scanbttn); scanbtn.setonclicklistener(this); prounittxt = (edittext)findviewbyid(r.id.prqtxt); prolowtxt = (edittext)findviewbyid(r.id.lsatxt); addbtn = (button)findviewbyid(r.id.addbttn); addbtn.setonclicklistener(new onclicklistener() { string name = prounittxt.gettext().tostring(); string code = prolowtxt.gettext().tostring(); @override public void onclick(view view) { db.open(); //long id = db.insertproduct("data1","data2"); long id = db.insertproduct(name, code); db.close(); } }); } private void tomenu() { button mmenubutton = (button) findviewbyid(r.id.mmenubttn); mmenubutton.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { intent smintent = new intent(context, welcome.class); startactivity(smintent); stockmanagement.this.finish(); } }); } public void cleartextfield() { button clrbutton = (button)findviewbyid(r.id.clrbttn); final edittext pridtext = (edittext) findviewbyid(r.id.pridtxt); final edittext prnametext = (edittext) findviewbyid(r.id.prnametxt); final edittext prqtext = (edittext) findviewbyid(r.id.prqtxt); final edittext lsatext = (edittext) findviewbyid(r.id.lsatxt); final edittext racktext = (edittext) findviewbyid(r.id.racktxt); clrbutton.setonclicklistener(new onclicklistener(){ public void onclick(view v){ pridtext.settext(""); prnametext.settext(""); prqtext.settext(""); lsatext.settext(""); racktext.settext(""); } }); } @override public void onclick(view v) { if(v.getid()==r.id.scanbttn){ intentintegrator scanintegrator = new intentintegrator(this); scanintegrator.initiatescan(); } } public void onactivityresult(int requestcode, int resultcode, intent intent) { intentresult scanningresult = intentintegrator.parseactivityresult(requestcode, resultcode, intent); if (scanningresult != null) { string scancontent = scanningresult.getcontents(); string scanformat = scanningresult.getformatname(); pronametxt.settext("format: " + scanformat); proidtxt.settext("content: " + scancontent); } else{ toast toast = toast.maketext(getapplicationcontext(), "no scan data received!", toast.length_short); toast.show(); } } } dbadapter.java
package ping.stock.check; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class dbadapter { static final string key_rowid = "_id"; static final string key_name = "proname"; static final string key_code= "procode"; static final string tag = "dbadapter"; static final string database_name = "pingstockcheckdata"; static final string database_table = "producttable"; static final int database_version = 2; public static final string database_create = "create table " + database_table +"(" + key_rowid + "integer primary key autoincrement," + key_name + "text not null," + key_code + "text not null);"; public final context context; public databasehelper dbhelper; public sqlitedatabase db; public dbadapter (context ctx){ this.context = ctx; dbhelper = new databasehelper (context); } public static class databasehelper extends sqliteopenhelper { databasehelper (context context){ super (context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { try{ db.execsql(database_create); } catch(exception e){ e.printstacktrace(); } } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { log.w(tag, "upgrading database version " + oldversion + " " + newversion + ", destroy old data"); db.execsql("drop table if exists producttable"); oncreate(db); } } // open database public dbadapter open() throws sqlexception { dbhelper = new databasehelper(context); db = dbhelper.getwritabledatabase(); return this; } public void close(){ dbhelper.close(); } public long insertproduct(string name, string code) { contentvalues initialvalues = new contentvalues(); initialvalues.put(key_name, name); initialvalues.put(key_code, code); return db.insert(database_table, null, initialvalues); } stock_man.xml
<?xml version="1.0" encoding="utf-8"?> <tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabletlayoutx" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchcolumns="2" android:orientation="vertical" android:layout_gravity="center" android:gravity="center" android:background="@drawable/bg_pic" > <tablerow android:id="@+id/tablerowx" android:layout_width="wrap_content" android:layout_height="5dip" > </tablerow> <scrollview android:layout_width="wrap_content" android:layout_height="wrap_content"> <relativelayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" > <tablelayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:gravity="center" android:stretchcolumns="2" > <tablerow android:id="@+id/tablerow0" android:layout_width="wrap_content" android:layout_height="wrap_content" > </tablerow> <view android:layout_height="5dip" android:background="#ff0000" /> <textview android:id="@+id/textview8" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="stock management" android:textappearance="?android:attr/textappearancelarge" android:textcolor="#0000cc" android:textstyle="bold" android:typeface="monospace" /> <view android:layout_height="5dip" android:background="#ff0001" /> <tablerow android:id="@+id/tablerow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <textview android:id="@+id/textview6" android:layout_width="10dip" android:layout_height="wrap_content" android:layout_margin="3dip" android:layout_weight="1" android:gravity="center" android:padding="0dip" android:text="product" android:textappearance="?android:attr/textappearancemedium" android:textstyle="bold" /> <textview android:id="@+id/textview7" android:layout_width="5dip" android:layout_height="wrap_content" android:gravity="center" android:layout_margin="3dip" android:layout_weight="1" android:padding="0dip" android:text="description" android:textappearance="?android:attr/textappearancemedium" android:textstyle="bold" /> </tablerow> <tablerow android:id="@+id/tablerow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <textview android:id="@+id/textview1" android:layout_width="10dip " android:layout_height="wrap_content" android:layout_margin="4dip" android:layout_weight="1" android:padding="5dip" android:text="id no." android:textappearance="?android:attr/textappearancemedium" /> <edittext android:id="@+id/pridtxt" android:layout_width="5dip " android:layout_height="wrap_content" android:layout_margin="0dip" android:layout_weight="1" android:ems="10" android:hint="the product id" android:padding="5dip" android:textappearance="?android:attr/textappearancesmall"/> </tablerow> <tablerow android:id="@+id/tablerow3" android:layout_width="wrap_content" android:layout_height="wrap_content" > <textview android:id="@+id/textview2" android:layout_width="30dip " android:layout_height="wrap_content" android:layout_margin="4dip" android:layout_weight="1" android:padding="5dip" android:text="name" android:textappearance="?android:attr/textappearancemedium" /> <edittext android:id="@+id/prnametxt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="4dip" android:layout_weight="1" android:ems="10" android:hint="the product name" android:padding="5dip" android:textappearance="?android:attr/textappearancesmall"/> </tablerow> <tablerow android:id="@+id/tablerow4" android:layout_width="wrap_content" android:layout_height="wrap_content" > <textview android:id="@+id/textview3" android:layout_width="10dip " android:layout_height="wrap_content" android:layout_margin="4dip" android:layout_weight="1" android:padding="5dip" android:text="unit" android:textappearance="?android:attr/textappearancemedium" /> <edittext android:id="@+id/prqtxt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="4dip" android:layout_weight="1" android:ems="10" android:hint="quantites (number)" android:padding="0dip" android:maxlength="9" android:textappearance="?android:attr/textappearancesmall"/> </tablerow> <tablerow android:id="@+id/tablerow5" android:layout_width="wrap_content" android:layout_height="wrap_content" > <textview android:id="@+id/textview4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="4dip" android:layout_weight="1" android:padding="5dip" android:text="low stock alert" android:textappearance="?android:attr/textappearancesmall" /> <edittext android:id="@+id/lsatxt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dip" android:layout_weight="1" android:ems="10" android:hint="alert low stock" android:padding="0dip" android:textappearance="?android:attr/textappearancesmall"/> </tablerow> <tablerow android:id="@+id/tablerow6" android:layout_width="wrap_content" android:layout_height="wrap_content" > <textview android:id="@+id/textview5" android:layout_width="10dip " android:layout_height="wrap_content" android:layout_margin="4dip" android:layout_weight="1" android:padding="5dip" android:text="rack no." android:textappearance="?android:attr/textappearancemedium" /> <edittext android:id="@+id/racktxt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="4dip" android:layout_weight="1" android:ems="10" android:hint="product rack location" android:padding="0dip" android:textappearance="?android:attr/textappearancesmall"/> </tablerow> <tablerow android:id="@+id/tablerow7" android:layout_width="wrap_content" android:layout_height="wrap_content" > <button android:id="@+id/addbttn" style="?android:attr/buttonstylesmall" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_margin="1dip" android:layout_weight="1" android:padding="5dip" android:text="add" android:textappearance="?android:attr/textappearancemedium"/> <button android:id="@+id/clrbttn" style="?android:attr/buttonstylesmall" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_margin="1dip" android:layout_weight="1" android:padding="5dip" android:text="clear" android:textappearance="?android:attr/textappearancesmall"/> </tablerow> <tablerow android:id="@+id/tablerow07" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"> <button android:id="@+id/scanbttn" style="?android:attr/buttonstylesmall" android:layout_width="1dip" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" android:text="scan" /> </tablerow> <tablerow android:id="@+id/tablerow8" android:layout_width="wrap_content" android:layout_height="wrap_content" > <button android:id="@+id/mmenubttn" style="?android:attr/buttonstylesmall" android:layout_width="1dip" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" android:text="main menu" android:textappearance="?android:attr/textappearancesmall"/> </tablerow> <view android:layout_height="5dip" android:background="#ff0000" /> </tablelayout> </relativelayout> </scrollview> </tablelayout>
you're calling gettext() on edittexts when setting onclick() listener. move gettext() onclick() data on click.
Comments
Post a Comment