android - how to make listview from JSON -> Sqlite? -
i working in android application in data in form of json , json data saved in sqlite. till have saved json sqlite when trying data in listview , application crashes
mainactivity
public class mainactivity extends activity { private string[] navmenutitles; private typedarray navmenuicons; private edittext edittextname; sharedpreferences sp; private string jsonresult; private listview listview; private button b; edittext etname, et; textview tv; string myjson; private static final string tag = "mainactivity.java"; private static final string tag_name = "notice"; private categoryhelper databasehelper; button get, store, select; jsonarray peoples = null; arraylist<hashmap<string, string>> personlist; listview list; public static final string user_name = "username"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.noticelist); //select = (button) findviewbyid(r.id.button3); databasehelper=new categoryhelper(mainactivity.this); databasehelper.gettimerecordlist(); //sharedpreferences myprefs= getsharedpreferences("user", mode_world_readable); // string session_id= myprefs.getstring("session_id", null); //textview textview = (textview) findviewbyid(r.id.fname); //textview.settext("welcome "+session_id); /*select.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub select_seqlite(); } });*/ // load icons // strings.xml list = (listview) findviewbyid(r.id.listview); personlist = new arraylist<hashmap<string,string>>(); getdata(); }
//send messages stop
//get json data start protected void showlist(){ try { jsonarray peoples = new jsonarray(myjson); for(int i=0;i<peoples.length();i++){ jsonobject c = peoples.getjsonobject(i); string name=null, date=null; if(c.has("notice")) name = c.getstring("notice"); databasehelper.savecategoryrecord(name); cursor c1 = databasehelper.gettimerecordlist(); if( c1 != null && c1.movetofirst()) { date = c1.getstring(c1.getcolumnindexorthrow("name")); } hashmap<string,string> persons = new hashmap<string,string>(); persons.put(tag_name,date); log.i("tagconvertstr", "[" + date + "]"); personlist.add(persons); } /*listadapter adapter = new simpleadapter( messages.this, personlist, r.layout.list_item, new string[]{tag_name,tag_add}, new int[]{r.id.name, r.id.address} );*/ listadapter adapter = new simpleadapter( mainactivity.this, personlist, r.layout.list_item1, new string[]{tag_name}, new int[]{r.id.name} );
// http://www.vogella.com/tutorials/androidlistview/article.html // custom adapter list view
list.setadapter(adapter); /* list.setadapter(adapter);*/ } catch (jsonexception e) { log.i("tagconvertstr", "["+myjson+"]"); } } private void select_seqlite() { // todo auto-generated method stub databasehelper.gettimerecordlist(); cursor c = databasehelper.gettimerecordlist(); if (c.movetofirst()) { { displaycontact(c); } while (c.movetonext()); } } private void displaycontact(cursor c) { // todo auto-generated method stub toast.maketext(getbasecontext(),"name " + c.getstring(0), toast.length_long).show(); } public void getdata(){ class getdatajson extends asynctask<string, void, string>{ @override protected string doinbackground(string... params) { sharedpreferences myprefs= getsharedpreferences("user", mode_world_readable); string session_id= myprefs.getstring("session_id", null); inputstream inputstream = null; string result = null; try { string postreceiverurl = "http://10.0.2.2/progress_card/teacher/teacher_notice.php"; // httpclient httpclient httpclient = new defaulthttpclient(); // post header httppost httppost = new httppost(postreceiverurl); // add data list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("username", "suyash1")); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); httpentity resentity = response.getentity(); inputstream = resentity.getcontent(); // json utf-8 default bufferedreader reader = new bufferedreader(new inputstreamreader(inputstream, "utf-8"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } result = sb.tostring(); } catch (exception e) { log.i("tagconvertstr", "["+result+"]"); system.out.println(e); } { try{if(inputstream != null)inputstream.close();}catch(exception squish){} } return result; } @override protected void onpostexecute(string result){ myjson = result; showlist(); } } getdatajson g = new getdatajson(); g.execute(); }
//get json data stop }
catergoryhelper (sqlite)
public class categoryhelper { private static final int database_version = 2; private static final string database_name = "category.db"; private static final string table_name = "tbcategory"; public static final string category_column_name = "name"; category openhelper; private sqlitedatabase database; public categoryhelper(context context){ openhelper = new category(context); database = openhelper.getwritabledatabase(); } public void savecategoryrecord(string name) { contentvalues contentvalues = new contentvalues(); contentvalues.put(category_column_name, name); database.insert(table_name, null, contentvalues); } public cursor gettimerecordlist() { return database.rawquery("select * " + table_name, null); } private class category extends sqliteopenhelper { public category(context context) { // todo auto-generated constructor stub super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub db.execsql("create table " + table_name + "( " + category_column_name + " text )" ); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub db.execsql("drop table if exists"+ table_name); oncreate(db); } } }
logcat
01-16 15:04:21.735 29380-29380/org.pitechnologies.jsontosqlite e/androidruntime﹕ fatal exception: main process: org.pitechnologies.jsontosqlite, pid: 29380 android.database.cursorindexoutofboundsexception: index -1 requested, size of 1 @ android.database.abstractcursor.checkposition(abstractcursor.java:426) @ android.database.abstractwindowedcursor.checkposition(abstractwindowedcursor.java:136) @ android.database.abstractwindowedcursor.getstring(abstractwindowedcursor.java:50) @ org.pitechnologies.jsontosqlite.mainactivity.showlist(mainactivity.java:131) @ org.pitechnologies.jsontosqlite.mainactivity$1getdatajson.onpostexecute(mainactivity.java:249) @ org.pitechnologies.jsontosqlite.mainactivity$1getdatajson.onpostexecute(mainactivity.java:196) @ android.os.asynctask.finish(asynctask.java:636) @ android.os.asynctask.access$500(asynctask.java:177) @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:653) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5257) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698)
regards error:
index -1 requested, size of 1
check cursor in line,
cursor c1 = databasehelper.gettimerecordlist(); hashmap<string,string> persons = new hashmap<string,string>(); persons.put(tag_name,c1.getstring(0)); personlist.add(persons);
you should move next before call
c1.getstring(0)
Comments
Post a Comment