android - adapter.add(event) throws java.lang.UnsupportedOperationException -



android - adapter.add(event) throws java.lang.UnsupportedOperationException -

i making application. want add together more item after getting info server in asynctask , save object defined me. code.

eventcheck.java

private void put(final event event){ if(event!=null){ events = push(events,event); log.d("no after= ",integer.tostring(event.getno())); if(!firsttime){ context.runonuithread(new runnable() { @override public void run() { adapter = new eventlist(context,getevents()); adapter.setnotifyonchange(true); list.setadapter(adapter); } }); firsttime=true; }else{ context.runonuithread(new runnable() { @override public void run() { log.d("no = ",integer.tostring(event.getno())); log.d("title = ",event.gettitle()); log.d("description = ",event.getdescription()); adapter.add(event); adapter.notifydatasetchanged(); } }); } } }

adapter.java

import android.app.activity; import android.app.alertdialog; import android.content.dialoginterface; import android.content.intent; import android.provider.settings; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.textview; public class eventlist extends arrayadapter<event>{ private final activity context; private final event[] events; private final string[] title; private final string[] description; public eventlist(activity context,event[] events) { super(context, r.layout.list_event, events); this.context = context; this.events = events; this.title = new string[events.length]; this.description = new string[events.length]; for(int i=0;i<events.length;i++){ title[i]=events[i].gettitle(); description[i] = events[i].getdescription(); } }; @override public view getview(int position, view view, viewgroup parent) { layoutinflater inflater = context.getlayoutinflater(); view rowview = inflater.inflate(r.layout.list_event, null, true); textview txtcontent = (textview) rowview.findviewbyid(r.id.belisttitle); string line = new stringbuilder().append(position+1).append(" .").append(" ").append(title[position]).append(system.getproperty ("line.separator")).tostring(); line = new stringbuilder().append(line).append(description[position]).tostring(); txtcontent.settext(line); homecoming rowview; } }

it throws next exception:

10-04 16:07:56.855: e/androidruntime(14835): java.lang.unsupportedoperationexception 10-04 16:07:56.855: e/androidruntime(14835): @ java.util.abstractlist.add(abstractlist.java:404) 10-04 16:07:56.855: e/androidruntime(14835): @ java.util.abstractlist.add(abstractlist.java:425) 10-04 16:07:56.855: e/androidruntime(14835): @ android.widget.arrayadapter.add(arrayadapter.java:179) 10-04 16:07:56.855: e/androidruntime(14835): @ com.xxx.xxx.eventcheck$2.run(eventcheck.java:257) 10-04 16:07:56.855: e/androidruntime(14835): @ android.os.handler.handlecallback(handler.java:733) 10-04 16:07:56.855: e/androidruntime(14835): @ android.os.handler.dispatchmessage(handler.java:95) 10-04 16:07:56.855: e/androidruntime(14835): @ android.os.looper.loop(looper.java:136) 10-04 16:07:56.855: e/androidruntime(14835): @ android.app.activitythread.main(activitythread.java:5140) 10-04 16:07:56.855: e/androidruntime(14835): @ java.lang.reflect.method.invokenative(native method) 10-04 16:07:56.855: e/androidruntime(14835): @ java.lang.reflect.method.invoke(method.java:515) 10-04 16:07:56.855: e/androidruntime(14835): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:795) 10-04 16:07:56.855: e/androidruntime(14835): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:611) 10-04 16:07:56.855: e/androidruntime(14835): @ dalvik.system.nativestart.main(native method)

line 257 line adapter.add(event) want inquire wrong code? can help me find out problem?

i want inquire wrong code?

there's nil inherently wrong code. short reply you're running java-specific quirk. bear me:

the source code arrayadapter constructor you're invoking looks this:

public arrayadapter(context context, int textviewresourceid, t[] objects) { init(context, textviewresourceid, 0, arrays.aslist(objects)); }

not happens lastly parameter, array supply: 'converted' arraylist calling arrays.aslist(objects).

now, reply can found looking @ the documentation method call:

returns list of objects in specified array. the size of list cannot modified, i.e. adding , removing unsupported, elements can set. setting element modifies underlying array.

basically happens array pass in converted fixed-size list (if breakpoint code @ runtime, should see unmodifiablecollection instance - note: different immutable). can't add together or remove items, set items (which doesn't alter list size replaces existing item), although latter operation not exposed arrayadapter class default.

the easiest solution phone call equivalent arrayadapter constructor takes arraylist instead of array yourself:

public arrayadapter(context context, int resource, list<t> objects)

this require minor changes current code, arrays , list similar work with. also, remember implement viewholder/rowwrapper pattern in adapter - plenty of examples out there. finally, there's not benefit storing titles & description separate arrays, , it's kind of in contradiction thought of object-oriented programming , encapsulation. allow object 'own' properties , utilize getter methods access them.

//edit:

here's illustration code illustrates changes arrayadapter, including on how utilize viewholder pattern. typed straight browser, please mind types/syntax mistakes.

you'll still have alter put() method works list of events instead of array, i'll leave figure out. there's plethora of examples available online help out.

public class eventlist extends arrayadapter<event>{ private static final string line_separator = system.getproperty ("line.separator"); public eventlist(context context, list<event> events) { super(context, r.layout.list_event, events); } @override public view getview(int position, view view, viewgroup parent) { view rowview = view; viewholder holder; if (rowview == null) { holder = new viewholder(); layoutinflater inflater = layoutinflater.from(getcontext()); rowview = inflater.inflate(r.layout.list_event, null, true); holder.txtcontent = (textview) rowview.findviewbyid(r.id.belisttitle); rowview.settag(holder); } else { holder = (viewholder) rowview.gettag(); } event event = getitem(position); stringbuilder line = new stringbuilder(position+1) .append(" . ") .append(event.gettitle()) .append(line_separator) .append(event.getdescription()); holder.txtcontent.settext(line); homecoming rowview; } static class viewholder { private textview txtcontent; } }

java android android-listview android-asynctask android-arrayadapter

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -