Android Custom Adapter - ArrayAdapter requires resource ID to be a Textview error -
i created custom adapter in order have title, description , image list items, keeps throwing this:
java.lang.illegalstateexception: arrayadapter requires resource id textview
i understand why throw if using standard android adapter, thought making own adapter can pretty avoid this. there i'm missing?
here adapter class:
package homepage; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; import java.util.arraylist; public class adapter_homepage extends baseadapter { private final context context; private final arraylist<string> titles; private final arraylist<string> descriptions; static class viewholder { public textview title, description; public imageview image; } public adapter_homepage(context context, arraylist<string> titles, arraylist<string> descriptions) { this.context = context; this.titles = titles; this.descriptions = descriptions; } public string getitemtitle(int position) { return titles.get(position); } public string getitemdescription(int position) { return descriptions.get(position); } @override public int getcount() { return 0; } @override public object getitem(int position) { return null; } @override public long getitemid(int position) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { view rowview = convertview; viewholder holder; if (rowview == null) { layoutinflater inflater = (layoutinflater) context.getapplicationcontext().getsystemservice(context.layout_inflater_service); rowview = inflater.inflate(r.layout.home_activity_item, null); // configure view holder holder = new viewholder(); holder.title = (textview) rowview.findviewbyid(r.id.firstline); holder.description = (textview) rowview.findviewbyid(r.id.secondline); holder.image = (imageview) rowview.findviewbyid(r.id.icon); rowview.settag(holder); } else { holder = (viewholder) rowview.gettag(); } holder.title.settext(getitemtitle(position)); holder.description.settext(getitemdescription(position)); holder.image.setimagedrawable(context.getresources().getdrawable(r.drawable.icon)); return rowview; } }
here activity class:
public class activity_homepage extends activity { private listview listview; private adapter_homepage adapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.home_activity_listview); listview = (listview) findviewbyid(r.id.listview); arraylist<string> titles = new arraylist<string>(); titles.add("aaron rodgers"); titles.add("jordy nelson"); arraylist<string> descriptions = new arraylist<string>(); descriptions.add("quarterback"); descriptions.add("wide receiver"); adapter = new adapter_homepage(this, titles, descriptions); listview.setadapter(adapter); listview.setonitemclicklistener(global_onitemclicklistener); } final adapterview.onitemclicklistener global_onitemclicklistener = new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { } }; }
it seems using arrayadapter
instead of custom implementation , not providing id
of textview
.
also, need make sure returning correct values on baseadapter
methods. returning 0
on getcount()
, null
on getitem()
make app crash.
Comments
Post a Comment