android - Remove item in ArrayAdapter<String> in ListView -
this question has answer here:
i delete clicked item in listview.
the solution use arraylist instead of string[];
here code:
public class mydialogfragment extends dialogfragment {
@override public dialog oncreatedialog(bundle savedinstancestate) { alertdialog.builder builder = new alertdialog.builder(getactivity()); builder.settitle("critères supplémentaires"); builder.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { oncancel(dialog); } } ); layoutinflater inflater = (layoutinflater) getactivity().getsystemservice(context.layout_inflater_service); view mview = inflater.inflate(r.layout.fragment_dialog, null); listview mlistview = (listview) mview.findviewbyid(r.id.lvliste); string[] mvalues = {"orientation", "nombre de chambres", "nombre de salle de bains", "nombre de toilettes", "cave", "parking", "garage", "jardin"}; final arrayadapter<string> adapter = new arrayadapter<string>(getactivity(), android.r.layout.simple_list_item_1, mvalues); mlistview.setadapter(adapter); mlistview.setonitemclicklistener(new android.widget.adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string item = (string) parent.getitematposition(position); adapter.remove(adapter.getitem(position)); adapter.notifydatasetchanged(); toast.maketext(getactivity(), "you selected : " + item, toast.length_short).show(); } }); builder.setview(mlistview); return builder.create(); }
but adapter.remove() makes application crash error:
java.lang.unsupportedoperationexception
what should if want remove item ?
thanks in advance :)
string[] mvalues = {"orientation", "nombre de chambres", "nombre de salle de bains", "nombre de toilettes", "cave", "parking", "garage", "jardin"}; final arraylist<string> list =new arraylist<string>(arrays.aslist(mvalues)); final arrayadapter<string> adapter = new arrayadapter<string>(getactivity(), android.r.layout.simple_list_item_1, list); mlistview.setadapter(adapter); mlistview.setonitemclicklistener(new android.widget.adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string item = list.get(position); list.remove(position); adapter.notifydatasetchanged(); toast.maketext(getactivity(), "you selected : " + item, toast.length_short).show(); } });
Comments
Post a Comment