android - set to listView in asynctask class -
i'm new in android , want have problem. have many adaper can load listview , want in asynctask. every adapter :
package com.example.mohammad.workclock; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.listview; import android.widget.textview; import com.example.mohammad.workclock.tbls.projects; import java.util.arraylist; import java.util.list; public class projectadapter extends baseadapter { private context context; private layoutinflater layoutinflater; private list<projects> listproject = new arraylist<>(); private listview listview; public projectadapter(context context) { this.context = context; layoutinflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); } @override public int getcount() { return listproject.size(); } @override public object getitem(int i) { return listproject.get(i); } @override public long getitemid(int i) { return i; } @override public view getview(int position, view view, viewgroup parent) { view row = layoutinflater.inflate(r.layout.itemsproject, parent, false); listview = (listview) parent; textview txt_title = (textview) row.findviewbyid(r.id.txt_title); projects p = (projects) getitem(position); txt_title.settext(p.gettitle()); return row; } public void updateinputdata(list<projects> listproject) { this.listproject = listproject; notifydatasetchanged(); }
}
for load data listview have class in asynctask , class:
class loadasynctasktolistview extends asynctask<void, void, arraylist<t>> { private sqlitedatabase db; private context context; private t adapter; private progressdialog progressdialog; private string tablename; private n object_; public loadasynctasktolistview(context context, t adapter, n object, string tablename){ this.context = context; db = getreadabledatabase(); this.adapter = adapter; this.tablename = tablename; this.object_ = object; } @override protected void onpreexecute() { progressdialog = progressdialog.show(this.context, "","در حال بارگذاری"); } @override protected arraylist doinbackground(void... voids) { string query = " select * " + tablename; list<n> listtobject = new arraylist<>(); cursor cursor = db.rawquery(query, null); if (cursor.movetofirst()) { n object = this.object_; (field field : listfield) { annotation annotationf = field.getannotation(annotationpartialclass.field.class); annotationpartialclass.field fieldannotation = (annotationpartialclass.field) annotationf; field.setaccessible(true); setvaluetoobject(object, field, cursor.getstring(cursor.getcolumnindex(fieldannotation.namefield()))); } } while (cursor.movetonext()); return (arraylist<t>) listtobject; } @override protected void onpostexecute(arraylist<t> inputobjects) { ((projectadapter)adapter).updateinputdata((list<projects>) inputobjects); progressdialog.dismiss(); } public <n>void setvaluetoobject(n object, field field, object fieldvalue){ field.setaccessible(true); try { field.set(object, fieldvalue); } catch (illegalaccessexception e) { e.printstacktrace(); } } }
know when want use method write code in activity:
projectadapter dd = new projectadapter(this); lv.setadapter(dd); // lv listview loadtolistview(lv,dd, new projects(),"");
when run code have error list :
08-18 13:18:28.999 26047-26163/com.example.mohammad.workclock e/androidruntime: fatal exception: asynctask #1 process: com.example.mohammad.workclock, pid: 26047 java.lang.runtimeexception: error occurred while executing doinbackground() @ android.os.asynctask$3.done(asynctask.java:325) @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:354) @ java.util.concurrent.futuretask.setexception(futuretask.java:223) @ java.util.concurrent.futuretask.run(futuretask.java:242) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:243) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1133) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:607) @ java.lang.thread.run(thread.java:761) caused by: java.lang.illegalargumentexception: field com.example.mohammad.workclock.tbls.projects.id has type int, got java.lang.string @ java.lang.reflect.field.set(native method) @ com.example.mohammad.workclock.asynctask.wholeworwithdb$1loadasynctasktolistview.setvaluetoobject(wholeworwithdb.java:383) @ com.example.mohammad.workclock.asynctask.wholeworwithdb$1loadasynctasktolistview.doinbackground(wholeworwithdb.java:357) @ com.example.mohammad.workclock.asynctask.wholeworwithdb$1loadasynctasktolistview.doinbackground(wholeworwithdb.java:306) @ android.os.asynctask$2.call(asynctask.java:305) @ java.util.concurrent.futuretask.run(futuretask.java:237) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:243) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1133) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:607) @ java.lang.thread.run(thread.java:761)
how can fix that?
as listed in logcat, problem here:
field com.example.mohammad.workclock.tbls.projects.id has type int, got java.lang.string expecting int, having string instead.
p.s. seems jackpot:
listview + assynctask + reflection)
try avoid stuff in future
Comments
Post a Comment