android - sqLite Record Not Moving previous -
when click on next button goes next record. when click on previous button won't move record.
java file
package com.example.akshay.eventmanager; import android.app.activity; import android.database.cursor; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.textview; import android.widget.toast; import java.text.simpledateformat; import java.util.calendar; import java.util.date; import java.util.locale; /** * created akshay on 7/13/2015. */ public class open extends activity implements view.onclicklistener { int year, month, day; public databasehelper mydb; simpledateformat simpledateformat; date mydate; public cursor cursor; cursor forprevious; string id, title, venue, displaydate, date, time, datefromdb, monthfromdb, yearfromdb, gotdate, minutes, hours, addr; string[] timeparts, split; button next, previous; textview tvenue, tvtime, tvdate, tvaddress; string idtopass; public int idd = 1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.open); getcurrentdate(); casting(); mydb = new databasehelper(this); /* toast.maketext(this, venue, toast.length_long).show(); toast.maketext(this, date, toast.length_long).show(); toast.maketext(this, time, toast.length_long).show(); toast.maketext(this, addr, toast.length_long).show();*/ } public void getcurrentdate() { calendar = calendar.getinstance(); year = now.get(calendar.year); month = now.get(calendar.month); // note: 0 based! day = now.get(calendar.day_of_month); } public void casting() { next = (button) findviewbyid(r.id.bnext); previous = (button) findviewbyid(r.id.bprevious); tvtime = (textview) findviewbyid(r.id.tvtime); tvenue = (textview) findviewbyid(r.id.tvvenue); tvaddress = (textview) findviewbyid(r.id.tvaddress); tvdate = (textview) findviewbyid(r.id.tvdate); next.setonclicklistener(this); previous.setonclicklistener(this); } @override public void onclick(view v) { switch (v.getid()) { case r.id.bnext: cursor = mydb.getdatabyid(idd); idtopass = string.valueof(idd); if (cursor.movetonext()) { venue = cursor.getstring(3); date = cursor.getstring(7); time = cursor.getstring(8); addr = cursor.getstring(6); idd++; toast.maketext(this, idtopass , toast.length_long).show(); tvtime.settext(time); tvdate.settext(date); tvenue.settext(venue); tvaddress.settext(addr); } break; case r.id.bprevious: cursor = mydb.getdatabyid(idd); if (cursor.movetoprevious()) { idd = idd - 1; toast.maketext(this, idtopass , toast.length_long).show(); venue = cursor.getstring(3); date = cursor.getstring(7); time = cursor.getstring(8); addr = cursor.getstring(6); tvtime.settext(time); tvdate.settext(date); tvenue.settext(venue); tvaddress.settext(addr); } break; } } }
databasehelper.java
package com.example.akshay.eventmanager; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import java.sql.sqlexception; /** * created akshay on 7/12/2015. */ public class databasehelper extends sqliteopenhelper { static final string db_name = "myeventmanagerfinal.db"; static final string table_name = "myevents"; public static final string col_1 = "id"; public static final string col_2 = "title"; public static final string col_3 = "description"; public static final string col_4 = "venue"; public static final string col_5 = "latitude"; public static final string col_6 = "longitude"; public static final string col_7 = "address"; public static final string col_8 = "date"; public static final string col_9 = "time"; sqlitedatabase mydb; cursor res; string mainid; public databasehelper(context context) { super(context, db_name, null, 1); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table " + table_name + "(" + col_1 + " integer primary key," + col_2 + " text," + col_3 + " text," + col_4 + " text," + col_5 + " text," + col_6 + " text," + col_7 + " text," + col_8 + " text," + col_9 + " text)"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exist " + table_name); oncreate(db); } public integer insertvalues(string title, string desc, string venue, string lat, string long, string addr,string date, string time) { mydb = this.getwritabledatabase(); contentvalues cv = new contentvalues(); cv.put(col_2,title); cv.put(col_3,desc); cv.put(col_4,venue); cv.put(col_5,lat); cv.put(col_6,long); cv.put(col_7,addr); cv.put(col_8,date); cv.put(col_9, time); long isinserted = mydb.insert(table_name, null, cv); if ( isinserted == -1) { return 0; } else { return 1; } } /*public string getdataforbrowse() { string[] columns = new string[]{col_1,col_4,col_5,col_6,col_7}; cursor c = mydb.query(table_name,columns,null,null,null,null,null); string result= " "; int iid = c.getcolumnindex(col_1); int ivenue = c.getcolumnindex(col_4); int ilat = c.getcolumnindex(col_5); int ilong = c.getcolumnindex(col_6); int iaddress = c.getcolumnindex(col_7); for(c.movetofirst(); !c.isafterlast(); c.movetonext()) { result = c.getstring(iid) +"," + c.getstring(ivenue) + "," + c.getstring(ilat) + "," + c.getstring(ilong) + "," + c.getstring(iaddress); } return result; }*/ public cursor getdata() { sqlitedatabase db = this.getwritabledatabase(); cursor res = db.rawquery("select * "+table_name, null); return res; } public cursor getdatabyid(int idd) { sqlitedatabase db = this.getwritabledatabase(); res = db.rawquery("select * "+table_name + " " + col_1 + " = " + idd, null); return res; } }
i want record move previous, whenever click on previous button.
i think problem in switch case increase idd++.
it should increase before call
cursor = mydb.getdatabyid(idd);
so @ last code case r.id.bnext: should like
case r.id.bnext: idd = idd + 1; cursor = mydb.getdatabyid(idd); idtopass = string.valueof(idd); if (cursor.movetonext()) { venue = cursor.getstring(3); date = cursor.getstring(7); time = cursor.getstring(8); addr = cursor.getstring(6); toast.maketext(this, idtopass , toast.length_long).show(); tvtime.settext(time); tvdate.settext(date); tvenue.settext(venue); tvaddress.settext(addr); } break;
Comments
Post a Comment