android - My listview won't update in fragment -


my listview won't refresh after key in new data. refresh if change tabs. put in notifydatasetchange() still doesn't work. help!

public class tab4 extends fragment {  textview title; textview datetitle; textview amounttitle; textview desctitle; listview expenseslist; list<expenses> expenses; expenses expenses; expenseslistadapter adapter; toast mtoast;  @override public view oncreateview(layoutinflater inflater, viewgroup container,         bundle savedinstancestate) {     // todo auto-generated method stub      view rootview = inflater.inflate(r.layout.tab4, container, false);      title = (textview) rootview.findviewbyid(r.id.title);     datetitle = (textview) rootview.findviewbyid(r.id.datetitle);     amounttitle = (textview) rootview.findviewbyid(r.id.amounttitle);     desctitle = (textview) rootview.findviewbyid(r.id.desctitle);     expenseslist = (listview) rootview.findviewbyid(r.id.expenseslist);      expenses = new arraylist<expenses>();     expenses = new expenses();     mtoast = toast.maketext(getactivity().getapplicationcontext(), "",             toast.length_short);     expenseslist.setadapter(adapter);     adapter = new expenseslistadapter(             getactivity().getapplicationcontext(), expenses);      if (expenses.getallexpenses().size() == 0) {         title.settext("total number of record 0");         datetitle.setvisibility(view.gone);         amounttitle.setvisibility(view.gone);         desctitle.setvisibility(view.gone);     } else {         (int = 0; < expenses.getallexpenses().size(); i++) {             expenses.add(expenses.getallexpenses().get(i));             system.out.println("expenses "                     + expenses.getallexpenses().get(i));              adapter.notifydatasetchanged();             expenseslist.invalidateviews();         }         title.settext("total number of record "                 + expenses.getallexpenses().size());     }      expenseslist.setonitemlongclicklistener(new onitemlongclicklistener() {          @override         public boolean onitemlongclick(adapterview<?> parent, view view,                 final int position, long id) {             // todo auto-generated method stub             alertdialog.builder builder = new alertdialog.builder(                     getactivity());             builder.setmessage("are sure want delete record?");             builder.setcancelable(false);             builder.setpositivebutton("yes",                     new dialoginterface.onclicklistener() {                          @override                         public void onclick(dialoginterface dialog,                                 int which) {                             // todo auto-generated method stub                             expenses = new expenses();                             expenses = expenses.getallexpenses().get(                                     position);                             expenses.delete();                             expenses.remove(position);                             adapter.notifydatasetchanged();                               title.settext("total number of record "                                     + expenses.getallexpenses().size());                              mtoast.settext("record has been deleted");                             mtoast.show();                         }                     });              builder.setnegativebutton("no",                     new dialoginterface.onclicklistener() {                          @override                         public void onclick(dialoginterface dialog,                                 int which) {                             // todo auto-generated method stub                             dialog.dismiss();                         }                     });              builder.show();             return false;         }     });      return rootview; } } 


class expenseslistadapter extends baseadapter {  list<expenses> expenses; context context; layoutinflater inflater;  public expenseslistadapter(context context, list<expenses> expenses) {     this.context = context;     this.expenses = expenses;      inflater = (layoutinflater) context             .getsystemservice(context.layout_inflater_service); }  @override public int getcount() {     // todo auto-generated method stub     return expenses.size(); }  @override public object getitem(int position) {     // todo auto-generated method stub     return position; }  @override public long getitemid(int position) {     // todo auto-generated method stub     return position; }  public class holder {     textview date;     textview amount;     textview desc; }  @override public view getview(int position, view convertview, viewgroup parent) {     // todo auto-generated method stub      holder holder = new holder();     convertview = inflater.inflate(r.layout.listview_expenseslist, null);      holder.date = (textview) convertview.findviewbyid(r.id.expensesdate);     holder.amount = (textview) convertview             .findviewbyid(r.id.expensesamount);     holder.desc = (textview) convertview.findviewbyid(r.id.expensesdesc);      holder.date.settext(expenses.get(position).date);     holder.amount.settext(string.valueof(expenses.get(position).amount));     holder.desc.settext(expenses.get(position).desc);      return convertview; }  } 

my listview won't refresh after key in new data. refresh if change tabs.

becuase adding new data in expenses arraylist.

to update new item in listview adapter, need add item in data-sorcude used inside adapter getting data , getcount` method.

to achive same create method in expenseslistadapter :

public void addallitem(list<expenses> stritem){   this.expenses.addall(stritem);   this.notifydatasetchanged();  } 

now call addnewitem method add new item when alertdialog positive button click :

 adapter.addnewitem(expenses.getallexpenses()); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -