How to retrieve data from a database and display it in textview for android? Asynctask doesnt load my data? -
how display value database , display in textview in android. have got tutorial json parser , seems working fine. when application running, not load activity textview , not show status. have posted yesterday fault exception , helped. activity seems never load data textview. posting code.
here json. want parse. seems parsing correctly. not displaying correctly.
{"success":1,"message":"retrieve status successful!","status":[{"lock_op":"0","door_op":"0","date_modified":"2015-05-08 09:56:35"}]}
here status activity
package com.locking; import android.support.v7.app.actionbaractivity; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import org.json.simple.*; import org.json.simple.parser.*; import java.util.arraylist; import java.util.hashmap; import java.util.concurrent.executionexception; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import org.w3c.dom.text; import com.locking.libraries.lobbyfunctions; import com.locking.libraries.userfunctions; import android.app.activity; import android.app.progressdialog; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.compoundbutton; import android.widget.compoundbutton.oncheckedchangelistener; import android.widget.switch; public class statactivity extends activity { int userid; int temp; int lockset = 0; int templock; int doorset = 0; string lock_op; string door_op; string door; string lock; string date; jsonarray status_user = null; textview lockstat; textview doorstat; textview datestat; string statdisplay = "lock open"; string statdisplay2 = "lock closed"; string doordisplay = "door open"; string doordisplay2 = "door closed"; //arraylist<hashmap<string, string>> statuslist; private static final string tag_success = "success"; private static final string tag_message = "message"; private static final string tag_status = "status"; private static final string tag_lock = "lock_op"; private static final string tag_door = "door_op"; private static final string tag_date = "date_modified"; private progressdialog pdialog; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //statuslist = new arraylist<hashmap<string, string>>(); lockstat = (textview) findviewbyid(r.id.switchstat); doorstat = (textview) findviewbyid(r.id.textview1); datestat = (textview) findviewbyid(r.id.datefield); try { // lobbies json , parse them. wait until async task // complete before continuing new getstats().execute().get(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (executionexception e) { // todo auto-generated catch block e.printstacktrace(); } } class getstats extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(statactivity.this); pdialog.setmessage("fetching status.."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } @override protected void doinbackground(void... args) { try { lobbyfunctions lobbyfunction = new lobbyfunctions(); jsonobject json3 = lobbyfunction.getstatus(); int success = json3.getint(tag_success); string message = json3.getstring(tag_message); string status = json3.getstring(tag_status); jsonarray status_user = new jsonarray(status); jsonobject c = status_user.getjsonobject(0); lock =c.getstring(tag_lock); door =c.getstring(tag_door); date =c.getstring(tag_date); if (lock.equals("0")) { lock = statdisplay; } if (lock.equals("1")) { lock = statdisplay2; } if (door.equals("0")) { door = doordisplay; } if (door.equals("1")) { door = doordisplay2; } /* hashmap<string, string> statlist = new hashmap<string, string>(); statlist.put(tag_lock, lock); statlist.put(tag_door, door); statlist.put(tag_date, date); statuslist.add(statlist); */ } catch (jsonexception e) { e.printstacktrace(); } // todo auto-generated method stub return null; } protected void onpostexecute(void result) { // dismiss dialog once done pdialog.dismiss(); runonuithread(new runnable() { public void run() { log.d("am getting anything", lock); lockstat.settext(lock); doorstat.settext(door); datestat.settext(date); } }); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
here main activity.
package com.locking; import android.support.v7.app.actionbaractivity; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import org.json.simple.*; import org.json.simple.parser.*; import java.util.arraylist; import java.util.hashmap; import java.util.concurrent.executionexception; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import org.w3c.dom.text; import com.locking.libraries.lobbyfunctions; import com.locking.libraries.userfunctions; import android.app.activity; import android.app.progressdialog; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.compoundbutton; import android.widget.compoundbutton.oncheckedchangelistener; import android.widget.switch; public class mainactivity extends activity { int userid; int temp; int lockset = 0; int templock; int doorset = 0; string lock_op; string door_op; string str_lockset; string str_lockoff; jsonarray status_user = null; textview lockstat; textview doorstat; textview datestat; string statdisplay = "lock open"; string statdisplay2 = "lock closed"; string doordisplay = "door open"; string doordisplay2 = "door closed"; private static final string tag_success = "success"; private static final string tag_message = "message"; private progressdialog pdialog; lobbyfunctions lobbyfunction = new lobbyfunctions(); userfunctions userfunction = new userfunctions(); userfunctions socketconnect = new userfunctions(); userfunctions sktconnect = new userfunctions(); protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); intent extras = getintent(); lockset = extras.getintextra("lockset", 0); doorset = extras.getintextra("doorset", 0); // userid = extras.getintextra("userid", 0); // temp = extras.getintextra("temp", 0); button check = (button) findviewbyid(r.id.button1); button lock_on = (button) findviewbyid(r.id.lock_on); button lock_off = (button) findviewbyid(r.id.lock_off); // button click event lock_on.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { new poststatuson().execute(); intent restart = new intent(getapplicationcontext(), mainactivity.class); startactivity(restart); finish(); } }); lock_off.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { // creating new product in background thread new poststatusoff().execute(); intent restart = new intent(getapplicationcontext(), mainactivity.class); startactivity(restart); finish(); } }); check.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // creating new product in background thread intent gotocheck = new intent(getapplicationcontext(), statactivity.class); startactivity(gotocheck); finish(); } }); } class poststatuson extends asynctask<string, string, string> { protected string doinbackground(string... args) { if (lockset == 1) { str_lockset = "1"; } else { str_lockset = "1"; } // string str_lockset = integer.tostring(lockset); string str_doorsets = integer.tostring(doorset); jsonobject json = userfunction .sendstatus(str_lockset, str_doorsets); // jsonobject jsonmod = userfunction.sendstatusmod(str_lockset, // str_doorsets); socketconnect.sendstatusmod(str_lockset, str_doorsets); log.d("create response", json.tostring()); // check success tag try { int success = json.getint(tag_success); if (success == 1) { // registered user log.d("user registered!", json.tostring()); intent logon = new intent(getapplicationcontext(), mainactivity.class); startactivity(logon); // closing screen finish(); return json.getstring(tag_message); } else { // failed register user log.d("register failure!", json.getstring(tag_message)); return json.getstring(tag_message); } } catch (jsonexception e) { e.printstacktrace(); } return null; } } class poststatusoff extends asynctask<string, string, string> { protected string doinbackground(string... args) { if (lockset == 1) { str_lockoff = "0"; } else { str_lockoff = "0"; } // string str_lockoff = integer.tostring(lockoff); string str_doorsets = integer.tostring(doorset); jsonobject json2 = userfunction.sendstatus(str_lockoff, str_doorsets); sktconnect.sendstatusmod(str_lockoff, str_doorsets); log.d("create response", json2.tostring()); // check success tag try { int success = json2.getint(tag_success); if (success == 1) { // registered user log.d("status posted!", json2.tostring()); intent logon = new intent(getapplicationcontext(), mainactivity.class); startactivity(logon); // closing screen finish(); return json2.getstring(tag_message); } else { // failed register user log.d("register failure!", json2.getstring(tag_message)); return json2.getstring(tag_message); } } catch (jsonexception e) { e.printstacktrace(); } return null; } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
here manifest file. sure manifest , main activity correct.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.locking" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="14" android:targetsdkversion="21" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.write_external_storage" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".logonactivity" android:label="@string/app_name" android:launchmode="singletop" android:windowsoftinputmode="adjustpan"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".registeractivity" android:label="@string/app_name" android:launchmode="singletop" android:windowsoftinputmode="adjustpan" /> <activity android:name=".statactivity" android:label="@string/app_name" android:launchmode="singletop" /> <activity android:name=".mainactivity" android:label="@string/app_name" android:launchmode="singletop" android:windowsoftinputmode="adjustpan" /> </application> </manifest>
please me solve problem.
here new error now.
05-08 16:07:19.001: e/androidruntime(1866): fatal exception: main 05-08 16:07:19.001: e/androidruntime(1866): process: com.locking, pid: 1866 05-08 16:07:19.001: e/androidruntime(1866): java.lang.nullpointerexception 05-08 16:07:19.001: e/androidruntime(1866): @ com.locking.statactivity$getstats$1.run(statactivity.java:159) 05-08 16:07:19.001: e/androidruntime(1866): @ android.app.activity.runonuithread(activity.java:4713) 05-08 16:07:19.001: e/androidruntime(1866): @ com.locking.statactivity$getstats.onpostexecute(statactivity.java:156) 05-08 16:07:19.001: e/androidruntime(1866): @ com.locking.statactivity$getstats.onpostexecute(statactivity.java:1) 05-08 16:07:19.001: e/androidruntime(1866): @ android.os.asynctask.finish(asynctask.java:632) 05-08 16:07:19.001: e/androidruntime(1866): @ android.os.asynctask.access$600(asynctask.java:177) 05-08 16:07:19.001: e/androidruntime(1866): @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:645) 05-08 16:07:19.001: e/androidruntime(1866): @ android.os.handler.dispatchmessage(handler.java:102) 05-08 16:07:19.001: e/androidruntime(1866): @ android.os.looper.loop(looper.java:136) 05-08 16:07:19.001: e/androidruntime(1866): @ android.app.activitythread.main(activitythread.java:5017) 05-08 16:07:19.001: e/androidruntime(1866): @ java.lang.reflect.method.invokenative(native method) 05-08 16:07:19.001: e/androidruntime(1866): @ java.lang.reflect.method.invoke(method.java:515) 05-08 16:07:19.001: e/androidruntime(1866): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 05-08 16:07:19.001: e/androidruntime(1866): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 05-08 16:07:19.001: e/androidruntime(1866): @ dalvik.system.nativestart.main(native method)
updated error logcat saying lock closed. meaning lock_op 1 database. , able retrieve value. displaying has been error.
05-08 16:21:06.040: i/choreographer(1608): skipped 177 frames! application may doing work on main thread. 05-08 16:21:15.650: d/dalvikvm(1608): gc_for_alloc freed 186k, 6% free 3881k/4124k, paused 3ms, total 4ms 05-08 16:21:15.700: w/egl_emulation(1608): eglsurfaceattrib not implemented 05-08 16:21:15.730: w/egl_emulation(1608): eglsurfaceattrib not implemented 05-08 16:21:15.870: d/am getting anything(1608): lock closed 05-08 16:21:15.870: d/androidruntime(1608): shutting down vm 05-08 16:21:15.870: w/dalvikvm(1608): threadid=1: thread exiting uncaught exception (group=0xb2d0eb20) 05-08 16:21:15.870: e/androidruntime(1608): fatal exception: main 05-08 16:21:15.870: e/androidruntime(1608): process: com.locking, pid: 1608 05-08 16:21:15.870: e/androidruntime(1608): java.lang.nullpointerexception 05-08 16:21:15.870: e/androidruntime(1608): @ com.locking.statactivity$getstats$1.run(statactivity.java:160) 05-08 16:21:15.870: e/androidruntime(1608): @ android.app.activity.runonuithread(activity.java:4713) 05-08 16:21:15.870: e/androidruntime(1608): @ com.locking.statactivity$getstats.onpostexecute(statactivity.java:141) 05-08 16:21:15.870: e/androidruntime(1608): @ com.locking.statactivity$getstats.onpostexecute(statactivity.java:1) 05-08 16:21:15.870: e/androidruntime(1608): @ android.os.asynctask.finish(asynctask.java:632) 05-08 16:21:15.870: e/androidruntime(1608): @ android.os.asynctask.access$600(asynctask.java:177) 05-08 16:21:15.870: e/androidruntime(1608): @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:645) 05-08 16:21:15.870: e/androidruntime(1608): @ android.os.handler.dispatchmessage(handler.java:102) 05-08 16:21:15.870: e/androidruntime(1608): @ android.os.looper.loop(looper.java:136) 05-08 16:21:15.870: e/androidruntime(1608): @ android.app.activitythread.main(activitythread.java:5017) 05-08 16:21:15.870: e/androidruntime(1608): @ java.lang.reflect.method.invokenative(native method) 05-08 16:21:15.870: e/androidruntime(1608): @ java.lang.reflect.method.invoke(method.java:515) 05-08 16:21:15.870: e/androidruntime(1608): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 05-08 16:21:15.870: e/androidruntime(1608): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595)
there few things see here. first, onpostexecute
in getstats
asynctask
, doinbackground
s on both of asynctask
s in main activity not overridden. of should have @override
annotation. way compiler tell if there's mismatch between method , asynctask
's.
your progressdialog
not dismissing because dialog dismissing not dialog created. when .show()
dialog need assign pdialog
or else not holding right reference:
pdialog = pdialog.show();
another not waiting anywhere tasks complete or doing when do. looks in main activity when user presses lock_on
, lock_off
buttons executing tasks , restarting main activity. create callback interface , have activity restart in callback onpostexecute
. settext
s avoid using runnable
.
here's code on how in app:
this in fragment. same substitute oncreate
onattach
:
static interface taskcallbacks { void onpreexecute(); void onprogressupdate(int percent); void oncancelled(); void onpostexecute(); } @override public void onattach(activity activity) { super.onattach(activity); mcallbacks = (taskcallbacks) activity; } @override public void onpostexecute() { //anything need on ui thread lockstat.settext(lock); doorstat.settext(door); datestat.settext(date); }
and in asynctask:
@override protected void onpostexecute(void ignore) { if (mcallbacks != null) { mcallbacks.onpostexecute(); //whatever else need }
Comments
Post a Comment