Tuesday, 15 May 2012

Drag and sort Listview OnItemClick issues android -



Drag and sort Listview OnItemClick issues android -

i using drag , sort listview want click item,but problem when sort item, position gets changed.how solve issue.i used example. https://github.com/commonsguy/cwac-touchlist. coding follows:

java class using dragndroplistview

listview listview = getlistview(); listview.setonitemclicklistener(this); if (listview instanceof dragndroplistview) { ((dragndroplistview) listview).setdroplistener(mdroplistener); ((dragndroplistview) listview).setremovelistener(mremovelistener); ((dragndroplistview) listview).setdraglistener(mdraglistener); } } private droplistener mdroplistener = new droplistener() { public void ondrop(int from, int to) { listadapter adapter = getlistadapter(); if (adapter instanceof dragndropadapter) { ((dragndropadapter)adapter).ondrop(from, to); getlistview().invalidateviews(); } } }; private removelistener mremovelistener = new removelistener() { public void onremove(int which) { listadapter adapter = getlistadapter(); if (adapter instanceof dragndropadapter) { ((dragndropadapter)adapter).onremove(which); getlistview().invalidateviews(); } } }; private draglistener mdraglistener = new draglistener() { int backgroundcolor = 0xe0103010; int defaultbackgroundcolor; public void ondrag(int x, int y, listview listview) { // todo auto-generated method stub } public void onstartdrag(view itemview) { itemview.setvisibility(view.visible); defaultbackgroundcolor = itemview.getdrawingcachebackgroundcolor(); itemview.setbackgroundcolor(backgroundcolor); imageview iv = (imageview)itemview.findviewbyid(r.id.imageview01); if (iv != null) iv.setvisibility(view.visible); } public void onstopdrag(view itemview) { itemview.setvisibility(view.visible); itemview.setbackgroundcolor(defaultbackgroundcolor); imageview iv = (imageview)itemview.findviewbyid(r.id.imageview01); if (iv != null) iv.setvisibility(view.visible); } }; private static string[] mlistcontent={"item 1", "item 2", "item 3"}; @override public void onitemclick(adapterview<?> arg0, view arg1, int position, long arg3) { // todo auto-generated method stub if(position==0) { toast.maketext(this, "you have selected item 1",toast.length_short) .show(); } if(position==1) { toast.maketext(this, "item 2",toast.length_short) .show(); } if(position==2) { intent i=new intent(a.this,b.class); startactivity(i); }

but when sorting, certainly position varies, couldnt utilize intent pass next

activity.how rectify in drag , sort listview?

thanks in advance.

you have check position in droplistener this:

private int position = 0; private droplistener mdroplistener = new droplistener() { public void ondrop(int from, int to) { listadapter adapter = listview.getadapter(); if (adapter instanceof dragndropadapter) { ((dragndropadapter) adapter).ondrop(from, to); listview.invalidateviews(); if(position == from) position = to; else if(position == && ((from - to) == 1 || (to - from) == 1)) position = from; else if(position == && (from - to) > 1) position = (from - to)-1; else if(position == && (to - from) > 1) position = (to - from)-1; else if(position > && position < to) position -= 1; else if(position < && position >to) position += 1; } } };

android android-layout android-intent android-widget android-listview

No comments:

Post a Comment