Android listview not updated -
i working viewpager.i have pages.i have listview witch adaptered images , texts. wrote function witch has int position witch means each position try adapter different values. use function in viewpager's setonpagechangelistener method,but can not updated listview
this adapter source code
public class myadapter extends baseadapter { private context activity; private list<copyofserveritems> items; private copyofserveritems objbean; private int row; public imageloader imageloader; public myadapter(context act, int resource, list<copyofserveritems> arraylist ) { super(); this.activity = act; this.row = resource; imageloader = new imageloader(act.getapplicationcontext()); this.items = arraylist; } @override public view getview(final int position, view convertview, viewgroup parent) { view view = convertview; viewholder holder; if (view == null) { layoutinflater inflater = (layoutinflater) activity .getsystemservice(context.layout_inflater_service); view = inflater.inflate(row, null); holder = new viewholder(); view.settag(holder); } else { holder = (viewholder) view.gettag(); } if ((items == null) || ((position + 1) > items.size())) return view; objbean = items.get(position); holder.title = (textview) view.findviewbyid(r.id.cat_title); holder.img = (imageview) view.findviewbyid(r.id.cat_image); holder.title.settext(objbean.gettitle()); imageloader.displayimage(objbean.getthumbnail(), holder.img); //holder.img.setimageresource(imageid[position]); return view; } public class viewholder { public textview title; public imageview img; } @override public int getcount() { return items.size(); } @override public object getitem(int position) { return null; } @override public long getitemid(int position) { return 0; } }
this java codee
public class categoryviewpager extends fragment { public final static string tag = categoryviewpager.class .getsimplename(); public static list<copyofserveritems> arrayoflist; public static list<copyofserveritems> arrayoflist1; private myadapter objadapter; public static categoryviewpager newinstance() { return new categoryviewpager(); } private viewpager mviewpager; private listview cat_listview; private pagertitlestrip mpagertitlestrip; string result; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.azercell_category_viewpager, container, false); arrayoflist = new arraylist<copyofserveritems>(); arrayoflist1 = new arraylist<copyofserveritems>(); final list<view> pages = new arraylist<view>(); final arraylist<string> titles = new arraylist<string>(); mviewpager = (viewpager) rootview.findviewbyid(r.id.viewpager); mpagertitlestrip = (pagertitlestrip) rootview .findviewbyid(r.id.pagertitle); layoutinflater inflater1 = layoutinflater.from(getactivity()); view page = inflater1.inflate(r.layout.azercell_category_pages, null); page = inflater.inflate(r.layout.azercell_category_pages, null); cat_listview = (listview) page.findviewbyid(r.id.cat_listview); mpagertitlestrip.settextcolor(color.blue); (int = 0; < arrayoflist.size(); i++) { page = inflater.inflate(r.layout.azercell_category_pages, null); cat_listview = (listview) page.findviewbyid(r.id.cat_listview); cat_listview.setadapter(objadapter); titles.add(arrayoflist.get(i).gettitle()); pages.add(page); } mviewpager.setonpagechangelistener(new onpagechangelistener() { @override public void onpageselected(int arg0) { try { log.e("position viewpager", string.valueof(arg0)); parsejsonfromserver(result, arg0); objadapter.notifydatasetchanged(); } catch (jsonexception e) { e.printstacktrace(); } } @override public void onpagescrolled(int arg0, float arg1, int arg2) { } @override public void onpagescrollstatechanged(int arg0) { } }); pageradapter p_adapter = new pageradapter() { @override public boolean isviewfromobject(view arg0, object arg1) { return arg0 == arg1; } @override public int getcount() { return pages.size(); } @override public void destroyitem(view container, int position, object object) { ((viewpager) container).removeview(pages.get(position)); } @override public charsequence getpagetitle(int position) { return titles.get(position); } @override public object instantiateitem(view container, int position) { ((viewpager) container).addview(pages.get(position)); return pages.get(position); } }; mviewpager.setadapter(p_adapter); return rootview; } public string parsejsonfromserver(string server, int position) throws jsonexception { jsonarray jsonarray = null; jsonobject mainjson = new jsonobject(server); jsonarray = mainjson.getjsonarray("result"); jsonarray children = jsonarray.getjsonobject(position).getjsonarray( "children"); (int j = 0; j < children.length(); j++) { copyofserveritems objitem = new copyofserveritems(); jsonobject data = children.getjsonobject(j).getjsonobject("data"); objitem.settitle(data.getstring("title")); objitem.setthumbnail(data.getstring("thumbnail")); log.e("123123123123", data.getstring("title")); arrayoflist1.add(objitem); } setadaptertolistview(); return server; } public void setadaptertolistview() { objadapter = new myadapter(getactivity(), r.layout.azercell_category_adapter, arrayoflist1); objadapter.notifydatasetchanged(); } }
my parsejsonfromserver parse server position.when position changed must change json.i did it.but if use position 0 , position 1 adapter can not updated.i can show values (i mean 0 , 1 position) how can solved problem? if knows solution please me
edit: of code looks wrong sorry may not work.
firstly better return position object id:
@override public long getitemid(int position) { return position; }
secondly, shouldn't change adapter update listview. should update adapter data.
add adapter:
updatedata( int resource, list<copyofserveritems> arraylist ){ this.row = resource; this.items = arraylist; }
and change setadaptertolistview this:
updatelistview( int resource, list<copyofserveritems> arraylist ){ objadapter.updatedata( resource, arraylist ); objadapter.notifydatasetchanged(); }
i want explain in turkish or azerbaijani site rules doesn't allow me. best can you. hope works.
edit: change code that:
public class categoryviewpager extends fragment { public final static string tag = categoryviewpager.class .getsimplename(); public static list<copyofserveritems> arrayoflist; public static list<copyofserveritems> arrayoflist1; private myadapter[] objadapter; public static categoryviewpager newinstance() { return new categoryviewpager(); } private viewpager mviewpager; private listview[] cat_listview; private pagertitlestrip mpagertitlestrip; string result; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.azercell_category_viewpager, container, false); arrayoflist = new arraylist<copyofserveritems>(); arrayoflist1 = new arraylist<copyofserveritems>(); final list<view> pages = new arraylist<view>(); final arraylist<string> titles = new arraylist<string>(); mviewpager = (viewpager) rootview.findviewbyid(r.id.viewpager); mpagertitlestrip = (pagertitlestrip) rootview .findviewbyid(r.id.pagertitle); mpagertitlestrip.settextcolor(color.blue); cat_listview = new listview[ arrayoflist.size() ]; objadapter = new myadapter[ arrayoflist.size() ]; (int = 0; < arrayoflist.size(); i++) { view page = inflater.inflate(r.layout.azercell_category_pages, null); cat_listviews[ ] = (listview) page.findviewbyid(r.id.cat_listview); objadapter[ ] = new myadapter( [put paremeters here] ); cat_listview[ ].setadapter(objadapter[ ] ); titles.add(arrayoflist.get(i).gettitle()); pages.add(page); } ......... public string parsejsonfromserver(string server, int position) throws jsonexception { jsonarray jsonarray = null; jsonobject mainjson = new jsonobject(server); jsonarray = mainjson.getjsonarray("result"); jsonarray children = jsonarray.getjsonobject(position).getjsonarray( "children"); (int j = 0; j < children.length(); j++) { copyofserveritems objitem = new copyofserveritems(); jsonobject data = children.getjsonobject(j).getjsonobject("data"); objitem.settitle(data.getstring("title")); objitem.setthumbnail(data.getstring("thumbnail")); log.e("123123123123", data.getstring("title")); arrayoflist1.add(objitem); } updatelistview( resource, obj, position ); return server; } updatelistview( int resource, list<copyofserveritems> arraylist ){ objadapter[ position ].updatedata( resource, arraylist ); objadapter[ position ].notifydatasetchanged(); }
and code send before adapter.
Comments
Post a Comment