android - How to refresh the ListView in Drag-Sort ListView? -
i have implement drag-sort listview(dslv) , lazylist in project, download demo lazylist , drag-sort listview github integrate , modify per requirement ,
i use dslv drag , sort items of listview , lazylist displaying image url, implement "basic usage playground" dslv drag , sort,
i have implement search in testbeddslv.java, problem when search content list, can't update list, have tried notifydatasetchanged method not works, create new adapter , pass listview lv.setadapter(adapter) , here set listadapter, cant lv.setadapter(adapter)
testbeddslv.java
package com.mobeta.android.demodslv; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.text.editable; import android.text.textwatcher; import android.view.menu; import android.view.menuinflater; import android.widget.button; import android.widget.edittext; import com.mobeta.android.dslv.dragsortcontroller; import com.mobeta.android.dslv.dragsortlistview; public class testbeddslv extends fragmentactivity { private int mnumheaders = 0; private int mnumfooters = 0; private int mdragstartmode = dragsortcontroller.on_down; private boolean mremoveenabled = false; private int mremovemode = dragsortcontroller.fling_right_remove; private boolean msortenabled = true; private boolean mdragenabled = true; private string mtag = "dslvtag"; button search; edittext search_customer; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.test_bed_main); search = (button) findviewbyid(r.id.btn_search); search_customer = (edittext) findviewbyid(r.id.search_product); search_customer.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { // here logic refresh list view } @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void aftertextchanged(editable s) { } }); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.test_bed, getnewdslvfragment(), mtag).commit(); } } @override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.mode_menu, menu); return true; } private fragment getnewdslvfragment() { dslvfragmentclicks f = dslvfragmentclicks.newinstance(mnumheaders, mnumfooters); f.removemode = mremovemode; f.removeenabled = mremoveenabled; f.dragstartmode = mdragstartmode; f.sortenabled = msortenabled; f.dragenabled = mdragenabled; return f; } }
dslvfragmentclicks.java
package com.mobeta.android.demodslv; import android.view.view; import android.widget.adapterview; import android.widget.listview; import android.os.bundle; import android.widget.toast; public class dslvfragmentclicks extends dslvfragment { public static dslvfragmentclicks newinstance(int headers, int footers) { dslvfragmentclicks f = new dslvfragmentclicks(); bundle args = new bundle(); args.putint("headers", headers); args.putint("footers", footers); f.setarguments(args); return f; } adapterview.onitemlongclicklistener mlongclicklistener = new adapterview.onitemlongclicklistener() { @override public boolean onitemlongclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { string message = string.format("long-clicked item %d", arg2); toast.maketext(getactivity(), message, toast.length_short).show(); return true; } }; @override public void onactivitycreated(bundle savedstate) { super.onactivitycreated(savedstate); listview lv = getlistview(); lv.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { string message = string.format("clicked item %d", arg2); toast.maketext(getactivity(), message, toast.length_short).show(); } }); lv.setonitemlongclicklistener(new adapterview.onitemlongclicklistener() { @override public boolean onitemlongclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { string message = string.format("long-clicked item %d", arg2); toast.maketext(getactivity(), message, toast.length_short).show(); return true; } }); } }
dslvfragment.java
package com.mobeta.android.demodslv; import java.util.arraylist; import java.util.arrays; import android.os.bundle; import android.support.v4.app.listfragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import com.mobeta.android.dslv.dragsortcontroller; import com.mobeta.android.dslv.dragsortlistview; public class dslvfragment extends listfragment { arrayadapter<string> adapter; private string[] array; public static arraylist<string> list; private dragsortlistview.droplistener ondrop = new dragsortlistview.droplistener() { @override public void drop(int from, int to) { if (from != to) { string item = adapter.getitem(from); adapter.remove(item); adapter.insert(item, to); } } }; private dragsortlistview.removelistener onremove = new dragsortlistview.removelistener() { @override public void remove(int which) { adapter.remove(adapter.getitem(which)); } }; protected int getlayout() { return r.layout.dslv_fragment_main; } /** * return list item layout resource passed arrayadapter. */ protected int getitemlayout() { return r.layout.list_item_handle_right; } private dragsortlistview mdslv; private dragsortcontroller mcontroller; public int dragstartmode = dragsortcontroller.on_down; public boolean removeenabled = false; public int removemode = dragsortcontroller.fling_right_remove; public boolean sortenabled = true; public boolean dragenabled = true; public static dslvfragment newinstance(int headers, int footers) { dslvfragment f = new dslvfragment(); bundle args = new bundle(); args.putint("headers", headers); args.putint("footers", footers); f.setarguments(args); return f; } public dragsortcontroller getcontroller() { return mcontroller; } /** * called dslvfragment.onactivitycreated(). override set different * adapter. */ public void setlistadapter() { array = getresources().getstringarray(r.array.jazz_artist_names); list = new arraylist<string>(arrays.aslist(array)); adapter = new arrayadapter<string>(getactivity(), getitemlayout(), r.id.text, list); setlistadapter(adapter); } /** * called in oncreateview. override provide custom * dragsortcontroller. */ public dragsortcontroller buildcontroller(dragsortlistview dslv) { dragsortcontroller controller = new dragsortcontroller(dslv); controller.setdraghandleid(r.id.drag_handle); controller.setclickremoveid(r.id.click_remove); controller.setremoveenabled(removeenabled); controller.setsortenabled(sortenabled); controller.setdraginitmode(dragstartmode); controller.setremovemode(removemode); return controller; } /** called when activity first created. */ @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { mdslv = (dragsortlistview) inflater.inflate(getlayout(), container, false); mcontroller = buildcontroller(mdslv); mdslv.setfloatviewmanager(mcontroller); mdslv.setontouchlistener(mcontroller); mdslv.setdragenabled(dragenabled); return mdslv; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); mdslv = (dragsortlistview) getlistview(); mdslv.setdroplistener(ondrop); mdslv.setremovelistener(onremove); bundle args = getarguments(); int headers = 0; int footers = 0; if (args != null) { headers = args.getint("headers", 0); footers = args.getint("footers", 0); } setlistadapter(); } }
test_bed_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <linearlayout android:id="@+id/search_lay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:focusable="true" android:focusableintouchmode="true" android:orientation="horizontal" > <edittext android:id="@+id/search_product" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginbottom="2dp" android:layout_marginleft="5dp" android:layout_margintop="2dp" android:layout_weight="1" android:background="@drawable/search_back" android:hint="enter firstname" android:imeoptions="actionsearch" android:inputtype="text" android:paddingbottom="2dp" android:paddingleft="5dp" android:paddingtop="2dp" android:visibility="visible" /> <button android:id="@+id/btn_search" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:layout_weight="2.5" android:text="cancel" android:textcolor="#155280" android:textsize="14sp" android:textstyle="bold" android:visibility="visible" /> </linearlayout> <framelayout android:id="@+id/test_bed" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <!-- add dslvfragment inside framelayout in code --> </linearlayout>
and other require class can download github link given above.....
actually if see code closly there's method have mention
public void setlistadapter() { array = getresources().getstringarray(r.array.jazz_artist_names); list = new arraylist<string>(arrays.aslist(array)); adapter = new arrayadapter<string>(getactivity(), getitemlayout(), r.id.text, list); setlistadapter(adapter); }
so said : "generally create new adapter , pass listview lv.setadapter(adapter) , here set listadapter, cant lv.setadapter(adapter)"
can't easily generating array based on search , set code ??
list = new arraylist<string>(arrays.aslist(array)); adapter = new arrayadapter<string>(getactivity(), getitemlayout(), r.id.text, list); setlistadapter(adapter);
edit: think asking wrong question. main concern should how communicate between activity , fragments...since search functionality in activity , list in fragment. need setup interface passes on search string fragment , there can make array , set same way doing right now.
read this question , answer. in answer you'll find 1 similar approach on passing data between activity , fragment.
edit 2: main concern communicate activity fragment this..declare interface in activity this:
public interface fragmentcommunicator{ public void passdatatofragment(string somevalue); } call interface @ text change listener..for example fragmentcommunicator mfragmentcommunicator; //your oncreate function { //your textchangelistenr { ontextchanged call if(mfragmentcommunicator != null) mfragmentcommunicator.passdatatofragment(pass string here) } make fragement implement interface public class somefragment extends fragment implements fragmentcommunicator { //this rest of fragment @override public void passdatatofragment(string somevalue) { //this function fired each time text changed since in activity calling same function in textchange listener. string somevalue string passed activity } //rest of code . . . //don't forget initialize interface in fragment itself. in onattach @override public void onattach(activity activity){ super.onattach(activity); context = getactivity(); ((mainactivity)context).fragmentcommunicator = this; } } }
you can refer this link further clarifications. check implementation of fragmentcommunicator
Comments
Post a Comment