android - arrayadapter.clear() deletes underlying data -
i got following problem. got listview , 2 buttons. @ start of fragment data stored in 2 arraylists. @ first content of first arraylist shown in listview works fine. when use clicks first button listview shows second arraylist. works fine user call adapter.clear(). first arraylist got deleted. therefor cant switch first list clicking on first button. never delete arraylist wonder why adapter.clear() it. nice. code underneath.
public class messagesfragment extends fragment { private arraylist<message> outbound; private arraylist<message> inbound; private messageadapter messageadapter; private int box; private view rootview; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_messages, container, false); outbound = new arraylist<message>(); inbound = new arraylist<message>(); this.rootview = rootview; box = 0; messageadapter = new messageadapter(getactivity(), r.layout.item_message, inbound); ((listview) rootview.findviewbyid(r.id.lv_message)) .setadapter(messageadapter); initializelisteners(); return rootview; } private void initializelisteners() { rootview.findviewbyid(r.id.rb_message_inbox).setonclicklistener( new onclicklistener() { @override public void onclick(view v) { showbox(0); } }); rootview.findviewbyid(r.id.rb_message_outbox).setonclicklistener( new onclicklistener() { @override public void onclick(view v) { showbox(1); } }); } private void showbox(int box) { messageadapter.clear(); switch (box) { case 0: messageadapter.addall(inbound); break; case 1: messageadapter.addall(outbound); break; } this.box = box; messageadapter.notifydatasetchanged(); } }
so @ beginning list gets populated arraylist inbound. when showbox(1) called inbound gets deleted messageadapter.clear() gets called. iutbound shown calling showbox(0) after inbound wont because empty.
this happened because class member arraylist same reference set adapter. add data adapter
adapter.addall(new arraylist<message>(inbound);
Comments
Post a Comment