android - updating setListAdapter from AsyncTask -
i using tutorial , sourcecodes make listview in android. had implemented using tutorial during updating setlistadapter
cannot update listview
dynamically. have gone through many stackoverflow questions , giving adapter.notifydatasetchanged()
solution. unable implement in asynctask.
itemlistfragment.java
public class itemlistfragment extends listfragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = super.oncreateview(inflater, container, savedinstancestate); setlistadapter(new itemadapter(getactivity(), setarray(getactivity()))); return v; } public arraylist<item> setarray(context context) { arraylist<item> items = new arraylist<item>(); realm realm = realm.getinstance(context); realmresults<books> bs = realm.where(books.class).findall(); (int = 0; < bs.size(); i++) { bitmap url = bitmapfactory.decodefile(bs.get(i).getcover()); string title = bs.get(i).getname(); string description = bs.get(i).getauthor(); item item = new item(url, title, description); items.add(item); } realm.close(); return items; } }
itemadapter.java
public class itemadapter extends arrayadapter<item> { public itemadapter(context context, list<item> items) { super(context, 0, items); } public view getview(int position, view convertview, viewgroup parent) { itemview itemview = (itemview) convertview; if (null == itemview) { itemview = itemview.inflate(parent); } itemview.setitem(getitem(position)); return itemview; } }
mainactivity.java
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_list); if (buildconfig.debug) viewserver.get(this).addwindow(this); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } else if (id == r.id.update) { update update = new update(getapplicationcontext(), mainactivity.this); update.execute(); return true; } return super.onoptionsitemselected(item); } @override protected void ondestroy() { super.ondestroy(); if (buildconfig.debug) viewserver.get(this).removewindow(this); } @override protected void onresume() { super.onresume(); if (buildconfig.debug) viewserver.get(this).setfocusedwindow(this); } }
update.java
public class update extends asynctask<void,void,void>{ private context context; public activity activity; public update(context context, activity activity) { this.context = context; this.activity = activity; } @override protected void doinbackground(void... params) { serveraddress = context.getstring(r.string.serveraddress); stringextras = context.getstring(r.string.checkbook); realm realm = null; try { url url = new url(serveraddress + stringextras); httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection(); httpurlconnection.setrequestmethod("get"); httpurlconnection.setrequestproperty("content-length", "0"); httpurlconnection.setusecaches(false); httpurlconnection.setallowuserinteraction(false); httpurlconnection.setconnecttimeout(1000); httpurlconnection.setreadtimeout(1000); httpurlconnection.connect(); int responsecode = httpurlconnection.getresponsecode(); log.i("response code",responsecode+""); bufferedreader br = new bufferedreader(new inputstreamreader(httpurlconnection.getinputstream())); string inputline; stringbuilder response = new stringbuilder(); while ((inputline = br.readline()) != null) { response.append(inputline); } br.close(); httpurlconnection.disconnect(); log.i("response data", response.tostring()); gsonbuilder gsonbuilder = new gsonbuilder(); gson gson = gsonbuilder.create(); jsonarray array = new jsonarray(response.tostring()); //bookslist = arrays.aslist(books); //jsonarray jsonarray = object.getjsonarray("cover"); realm = realm.getinstance(context); (int i=0; i< array.length(); i++ ) { jsonobject object = array.getjsonobject(i); books books = gson.fromjson(object.tostring(), books.class); publish(books, realm); } } catch (exception e) { e.printstacktrace(); } { if (realm != null){ realm.close(); } } return null; } @override protected void onpostexecute(void avoid) { super.onpostexecute(avoid); //i want call setarray contains database data has been updated on `doinbackground` , update listview accordingly. //the below codes codes have tried should update listview. activity.runonuithread(new runnable() { @targetapi(build.version_codes.honeycomb) @override public void run() { itemlistfragment activity1 = new itemlistfragment(); arraylist<item> arraylist = activity1.setarray(context); list<item> list = new arraylist<item>(); int = 0; (item item : arraylist) { list.add(arraylist.get(i)); i++; } activity1.arrayadapter.addall(list); activity1.arrayadapter.notifydatasetchanged(); } }); }
i trying update listview postexcution. so, how can update view through it.
error asynctask
just incase need it's not problem
8-30 16:31:47.976 1167-1167/? e/androidruntime﹕ fatal exception: main java.lang.nullpointerexception @ np.com.thefourthgeneration.update$1.run(update.java:124) @ android.app.activity.runonuithread(activity.java:4673) @ np.com.thefourthgeneration.update.onpostexecute(update.java:108) @ np.com.thefourthgeneration.update.onpostexecute(update.java:34) @ android.os.asynctask.finish(asynctask.java:631) @ android.os.asynctask.access$600(asynctask.java:177) @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:644) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5103) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:525) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) @ dalvik.system.nativestart.main(native method)
layout/activity_list
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <framelayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent"/> </linearlayout>
layout/list_fragment
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <listview android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content"/> </linearlayout>
mainactivity
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_list); fragmentmanager fm = getfragmentmanager(); itemlistfragment = (itemlistfragment) fm.findfragmentbytag(itemlistfragment.class.getsimplename()); if (itemlistfragment == null) { itemlistfragment = new itemlistfragment(); fragmenttransaction fragmenttransaction = fm.begintransaction(); fragmenttransaction.replace(r.id.content_frame, itemlistfragment, itemlistfragment.class.getsimplename()); fragmenttransaction.commit(); } if (buildconfig.debug) { viewserver.get(this).addwindow(this); } } @override protected void ondestroy() { super.ondestroy(); if (buildconfig.debug) { viewserver.get(this).removewindow(this); } } @override protected void onresume() { super.onresume(); if (buildconfig.debug) { viewserver.get(this).setfocusedwindow(this); } } }
itemlistfragment
public class itemlistfragment extends fragment { private listview listview; private itemadapter itemadapter; private arraylist<item> itemarraylist = new arraylist<>(); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setretaininstance(true); sethasoptionsmenu(true); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.list_fragment, container, false); itemadapter = new itemadapter(getactivity(), itemarraylist); listview = (listview) view.findviewbyid(r.id.listview); listview.setadapter(itemadapter); return view; } @override public void oncreateoptionsmenu(menu menu, menuinflater inflater) { inflater.inflate(r.menu.menu_main, menu); } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.action_settings: return true; case r.id.update: new update(getactivity()).execute(); return true; default: return super.onoptionsitemselected(item); } } public class update extends asynctask<void, void, void> { private context context; public update(context context) { this.context = context; } @override protected void doinbackground(void... params) { serveraddress = context.getstring(r.string.serveraddress); stringextras = context.getstring(r.string.checkbook); realm realm = null; try { url url = new url(serveraddress + stringextras); httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection(); httpurlconnection.setrequestmethod("get"); httpurlconnection.setrequestproperty("content-length", "0"); httpurlconnection.setusecaches(false); httpurlconnection.setallowuserinteraction(false); httpurlconnection.setconnecttimeout(1000); httpurlconnection.setreadtimeout(1000); httpurlconnection.connect(); int responsecode = httpurlconnection.getresponsecode(); log.i("response code", responsecode + ""); bufferedreader br = new bufferedreader(new inputstreamreader(httpurlconnection.getinputstream())); string inputline; stringbuilder response = new stringbuilder(); while ((inputline = br.readline()) != null) { response.append(inputline); } br.close(); httpurlconnection.disconnect(); log.i("response data", response.tostring()); gsonbuilder gsonbuilder = new gsonbuilder(); gson gson = gsonbuilder.create(); jsonarray array = new jsonarray(response.tostring()); //bookslist = arrays.aslist(books); //jsonarray jsonarray = object.getjsonarray("cover"); realm = realm.getinstance(context); (int = 0; < array.length(); i++) { jsonobject object = array.getjsonobject(i); books books = gson.fromjson(object.tostring(), books.class); publish(books, realm); } } catch (exception e) { e.printstacktrace(); } { if (realm != null) { realm.close(); } } return null; } @override protected void onpostexecute(void avoid) { itemarraylist.clear(); itemarraylist.addall(getarray(context)); itemadapter.notifydatasetchanged(); } public arraylist<item> getarray(context context) { arraylist<item> items = new arraylist<item>(); realm realm = realm.getinstance(context); realmresults<books> bs = realm.where(books.class).findall(); (int = 0; < bs.size(); i++) { bitmap url = bitmapfactory.decodefile(bs.get(i).getcover()); string title = bs.get(i).getname(); string description = bs.get(i).getauthor(); item item = new item(url, title, description); items.add(item); } realm.close(); return items; } } public class itemadapter extends arrayadapter<item> { public itemadapter(context context, list<item> items) { super(context, 0, items); } public view getview(int position, view convertview, viewgroup parent) { itemview itemview = (itemview) convertview; if (null == itemview) { itemview = itemview.inflate(parent); } itemview.setitem(getitem(position)); return itemview; } } }
its not 100% work code, because dont understand structures (like realm) , dont test it, main idea.
Comments
Post a Comment