java - Android - Wanting to alternate icon images in a listview using cursorLoader -
i have application displays listview of contacts sorted last, first names. beside each contact image (icon). there 3 kinds of contacts i'd display 3 different images (customers/suppliers/other) have default image set customer. i'm wondering if there's way using cusorloader shown below alternate images on fly, or whether best add method involving cursor in onresume. (onresume called each time need display images). believe simplecursoradapter can take textviews args, if it's possible, maybe compound textview/image work. icons not stored in database, in drawables.
thanks in advance replies.
@override protected void onresume() { super.onresume(); //starts new or restarts existing loader in manager getloadermanager().restartloader(0, null, this); } /* * filldata method binds simplecursoraadapter listview. */ private void filldata() { string[] = new string[] { contactsdb.column_last_name, contactsdb.column_first_name }; //the xml views data bound to: int[] = new int[] {r.id.label2, r.id.label}; getloadermanager().initloader(0, null, this); adapter = new simplecursoradapter(this, r.layout.contact_row, null, from, to, 0); setlistadapter(adapter); } // sort names last name, first name string orderby = contactsdb.column_last_name + " collate nocase asc" + "," + contactsdb.column_first_name + " collate nocase asc" ; // creates new loader after initloader () call @override public loader<cursor> oncreateloader(int id, bundle args) { string[] projection = { contactsdb.row_id, contactsdb.column_last_name, contactsdb.column_first_name }; cursorloader cursorloader = new cursorloader(this, somecontentprovider.content_uri, projection, null, null, orderby); return cursorloader; } @override public void onloadfinished(loader<cursor> loader, cursor data) { // swap new cursor in. // (the framework take care of closing old cursor once return.) adapter.swapcursor(data); //call requires min api 11 } @override public void onloaderreset(loader<cursor> loader) { // called when last cursor provided onloadfinished() // above closed. // data no longer available, delete reference adapter.swapcursor(null); } }
here code use dynamically show drawable on listview, have use function setviewbinder on adapter:
madapter.setviewbinder(new viewbinder() { public boolean setviewvalue(view aview, cursor acursor, int acolumnindex) { //modification of icon display in list if (acolumnindex == acursor.getcolumnindex(databasehandler.rate_emotion)) { int emotionid = acursor.getint(acolumnindex); drawable emotiondrawable = resources.getdrawable(r.drawable.ic_unknown_rate); //if emotion set if(emotionid > 0){ string emotiondrawablepath = "ic_smi" + emotionid; int emotiondrawableid = resources.getidentifier(emotiondrawablepath,"drawable", getpackagename()); //if drawable found if(emotiondrawableid > 0){ emotiondrawable = resources.getdrawable(emotiondrawableid); } } imageview emotionimage = (imageview) aview; emotionimage.setimagedrawable(emotiondrawable); return true; } return false; } });
you can see in example change drawable according data cursor every row.
Comments
Post a Comment