Monday, 15 February 2010

android - ListView in Fragment won't refresh -



android - ListView in Fragment won't refresh -

i trying create list in fragment, able grow when pressing button.

the list within fragment.

here's part of mainactivity:

public class mainactivity extends fragmentactivity{

public static string[] values = { "" }; private static listview listview; public static notesdbadapter helper; public static arraylist<fragment> fragments; public static viewpager viewpager; public static custompageradapter pageradapter; public static customlistadapter listadapter; @override protected void oncreate(bundle arg0) { super.oncreate(arg0); setcontentview(r.layout.activity_main); viewpager = (viewpager)findviewbyid(r.id.viewpager); listadapter = new customlistadapter(this); helper = new notesdbadapter(this); helper.open(); pageradapter = new custompageradapter(getsupportfragmentmanager()); fragments = new arraylist<fragment>(); fragments.add(new listfragment()); log.d("debug", "helper count: " + integer.tostring(helper.fetchallnotes().getcount())); values = new string[helper.fetchallnotes().getcount()]; for(int = 0; < values.length; i++) { values[i] = "klappt"; } updatelist(); viewpager.setadapter(pageradapter); } public void addnote() { log.d("debug", "new note"); helper.createnote("", ""); updatelist(); } public class customlistadapter extends arrayadapter<string> { private context context; private int res; private textview textview; public customlistadapter(context context) { super(context, r.layout.list_item, values); this.context = context; this.res = r.layout.list_item; } @override public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) context.getsystemservice(layout_inflater_service); view rowview = inflater.inflate(res, parent, false); textview = (textview)rowview.findviewbyid(r.id.list_item_textview); textview.settext(values[position]); homecoming rowview; } } public static class listfragment extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.fragment_list, container, false); listview = (listview)v.findviewbyid(r.id.fragment_list_listview); listview.setadapter(listadapter); homecoming v; } } public void updatelist() { values = new string[helper.fetchallnotes().getcount()]; for(int = 0; < values.length; i++) { values[i] = "klappt"; } listadapter.notifydatasetchanged(); }

}

so add together item list pressing on actionbar item. works, can confirm that. see alter have leave app , re-enter see changes or alter screen orientation landscape , back. want immeadiately show updated list, don't know how.

listadapter.notifydatasetchanged();

does not work me, don't know why. tried create new listadapter so:

listadapter = new customlistadapter(getapplicationcontext());

and set listview adapter:

listview.setadapter(listadapter);

but forcefulness close (i don't know why).

so know haven't got thought how prepare problem. able refresh listview when calling updatelist() method.

i logcat when trying apply new adapter listview:

06-19 18:17:32.506: e/androidruntime(12259): java.lang.runtimeexception: unable start activity componentinfo{com.ochs.androidnotepad/com.ochs.androidnotepad.mainactivity}: java.lang.nullpointerexception

android listview android-fragments android-listview

No comments:

Post a Comment