java - Searching and deleting rows in Custom ListView -
java - Searching and deleting rows in Custom ListView -
i'm trying create listview friends list. has search functiton in tthe user can search particular freind , delete them friend, message them , forth.
however, i'm having problem removing them. don't think understand positioning, or finding out right position on users freind in list.
i want create sure in cases, user removed right position. instance, if user uses search function , 1 user returned. don't want user removed @ position 0 (one user), want removed @ right position when user goes total list. position 0 in list isn't accidentaly removed.
could review code? , show slight indication going wrong this?
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); res = getresources(); searchfield = (edittext) findviewbyid(r.id.edittext01); lv = (listview) findviewbyid(android.r.id.list); //button = (button)findviewbyid(r.id.btnfriendlist); lv.setchoicemode(listview.choice_mode_single); //button.setfocusable(false); list = new arraylist<friend>(); nameblock = res.getstringarray(r.array.names); descblock = res.getstringarray(r.array.descriptions); names = new arraylist<string>(); for(int = 0; < nameblock.length; i++) { names.add((string)nameblock[i]); } descr = new arraylist<string>(); for(int = 0; < descblock.length; i++) { descr.add((string)descblock[i]); } images = new arraylist<integer>(); for(int = 0; < imageblock.length; i++) { images.add((integer)imageblock[i]); } //imageblock = res.getintarray(r.array.images); int size = nameblock.length; for(int = 0 ; < size; i++) { log.d("freindadd", "freind added" + i); list.add(new friend(i, names.get(i), descr.get(i), images.get(i))); //friendlist2.add(new friend(i, names.get(i), descr.get(i), images.get(i))); } log.i("application", "application started succesfully..."); adapter = new customadapter(this); setlistadapter(adapter); log.i("virtu", "count" + adapter.getcount()); //adapter.getcount(); searchfield.addtextchangedlistener(new textwatcher() { @override public void aftertextchanged(editable s) {} @override public void beforetextchanged(charsequence s, int start, int count, int after) {} @override public void ontextchanged(charsequence s, int start, int before, int count) { list.clear(); textlength = searchfield.gettext().length(); (int = 0; < names.size(); i++) { if (textlength <= names.get(i).length()) { if(names.get(i).tolowercase().contains(searchfield.gettext().tostring().tolowercase().trim())) { log.i("virtufriendlist", "list recyling in process... "); list.add(new friend(i, names.get(i), descr.get(i), images.get(i))); } } } appendlist(list); } }); } public void appendlist(arraylist<friend> list) { setlistadapter(new customadapter(this)); } class customadapter extends baseadapter { private context context; public customadapter(context context) { this.context = context; } @override public int getcount() { // todo auto-generated method stub homecoming list.size(); } @override public object getitem(int position) { // todo auto-generated method stub homecoming list.get(position); } @override public long getitemid(int position) { // todo auto-generated method stub homecoming list.size(); } class viewholder { textview username; textview userdesc; imageview userimage; button userbutton; viewholder(view view) { userimage = (imageview)view.findviewbyid(r.id.imageview); username = (textview)view.findviewbyid(r.id.title); userdesc = (textview)view.findviewbyid(r.id.mutualtitle); userbutton = (button)view.findviewbyid(r.id.btn); } } viewholder holder; view row; @override public view getview(int position, view convertview, viewgroup parent) { row = convertview; if(row == null) { // if visible user, deploy row(s) - allocated in local memory layoutinflater inflater = (layoutinflater)context .getsystemservice(context.layout_inflater_service); row = inflater.inflate(r.layout.search_list_item, parent, false); holder = new viewholder(row); row.settag(holder); log.d("virtu", "row deployed..."); } else { // recycle row if not visible to user - store in local memory holder = (viewholder)row.gettag(); log.d("virtu", "row recycled..."); } friend temp = list.get(position); // set resources each component in list holder.userimage.setimageresource(temp.getimage()); holder.username.settext(temp.getname()); holder.userdesc.settext(temp.getdesc()); ((button)row.findviewbyid(r.id.btn)).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { popupmenu pop = new popupmenu(getapplicationcontext(), v); menuinflater inflater = pop.getmenuinflater(); inflater.inflate(r.menu.firned_popup_action,pop.getmenu()); pop.show(); pop.setonmenuitemclicklistener(new onmenuitemclicklistener() { @override public boolean onmenuitemclick(menuitem item) { int selection = item.getitemid(); switch(choice) { case r.id.message: break; case r.id.unfollow: break; case r.id.unfriend: int position = (integer)row.gettag(); list.remove(position); names.remove(position); images.remove(position); descr.remove(position); adapter = new customadapter(context); lv.setadapter(adapter); adapter.notifydatasetchanged(); break; case r.id.cancel: } homecoming false; } }); } }); homecoming row; } } }
i think construction stands, go on have problem. suggestion assign friendid (or similar) each friend, , when building list, instead of passing userimage, username, userdesc , userbutton, pass along friendid well.
for example, have 5 friends, , here information:
userimage username userdesc userbutton friendid x jordyn x x 0 x sam x x 1 x connor x x 2 x paul x x 3 x raphael x x 4
but search (pretending can search 1 letter) 'o' in name returns,
userimage username userdesc userbutton friendid x jordyn x x 0 x connor x x 2
that way, when delete 1th row, removes friendid = 2 friend list instead of 1th row original friend list, would've been sam, not intention.
hope helps!
edit:
1: add together hidden textview rows called friendid in layout file (let me know if need help that).
now, viewholder this:
class viewholder { textview username; textview userdesc; imageview userimage; button userbutton; textview friendid; viewholder(view view) { userimage = (imageview)view.findviewbyid(r.id.imageview); username = (textview)view.findviewbyid(r.id.title); userdesc = (textview)view.findviewbyid(r.id.mutualtitle); userbutton = (button)view.findviewbyid(r.id.btn); friendid = (textview)view.findviewbyid(r.id.friendid); } }
2: add together arraylist friendids:
... descr = new arraylist<string>(); for(int = 0; < descblock.length; i++) { descr.add((string)descblock[i]); } images = new arraylist<integer>(); for(int = 0; < imageblock.length; i++) { images.add((integer)imageblock[i]); } friendids = new arraylist<integer>(); for(int = 0; < friendidsblock.length; i++) { images.add((integer)friendidsblock[i]); } ...
3: searchfield.addtextchangedlistener like:
int size = nameblock.length; for(int = 0 ; < size; i++) { log.d("freindadd", "freind added" + i); list.add(new friend(i, names.get(i), descr.get(i), images.get(i))); //friendlist2.add(new friend(i, names.get(i), descr.get(i), images.get(i), friendids.get(i))); } log.i("application", "application started succesfully...");
4: now, when unfriend someone, create sure friendid @ selected row opposed row index. then, remove friend search list given friendid friend general friend list given friendid.
you'll have forgive me, don't have ide in front end of me @ moment think covers it!
java android xml listview adapter
Comments
Post a Comment