android - ViewPager doesn't update ImageView even when i finish its activity and start it again with different values -
i'm using viewpager make scrollable photos gallery take photos urls sqlite database depending on intent value passed previous activity.
my problem : when start photo gallery first time , works correctly when close , start again different urls images ,viewpager doesn't update imageview , page title.
this code :
import java.util.arraylist; import java.util.locale; import android.content.intent; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteexception; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.support.v4.view.viewpager; import android.view.layoutinflater; import android.view.menu; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import android.widget.toast; public class photogalleryactivity extends fragmentactivity { /** * {@link android.support.v4.view.pageradapter} provide * fragments each of sections. use * {@link android.support.v4.app.fragmentpageradapter} derivative, * keep every loaded fragment in memory. if becomes memory * intensive, may best switch * {@link android.support.v4.app.fragmentstatepageradapter}. */ sectionspageradapter msectionspageradapter; /** * {@link viewpager} host section contents. */ viewpager mviewpager; string pid; int photoscount; intent context; sqlitedatabase db; static arraylist<placephoto> photos = new arraylist<placephoto>(); placephoto ph; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.photo_gallery); context = getintent(); //if (context.getextras().getstring("pid") != null ) pid = context.getextras().getstring("pid"); //else pid="0"; photoscount = context.getextras().getint("photos_count"); try { db = sqlitedatabase.opendatabase( "/data/data/"+getpackagename()+"/itartus.db3", null, sqlitedatabase.open_readwrite); cursor c = db.rawquery("select * photos place_id="+pid,null); while(c.movetonext()) { ph = new placephoto(c.getint(0),c.getint(1),c.getstring(3),c.getstring(2)); photos.add(ph); } } catch(sqliteexception e){ toast.maketext(getapplicationcontext(),"حدث خطأ ما",toast.length_long).show(); } // create adapter return fragment each of 3 // primary sections of app. msectionspageradapter = new sectionspageradapter( getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.pager); mviewpager.setadapter(msectionspageradapter); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.photo_gallery, menu); return true; } /** * {@link fragmentpageradapter} returns fragment corresponding * 1 of sections/tabs/pages. */ public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { // getitem called instantiate fragment given page. // return dummysectionfragment (defined static inner class // below) page number lone argument. fragment fragment = new dummysectionfragment(); bundle args = new bundle(); args.putint("position", position); fragment.setarguments(args); return fragment; } @override public int getcount() { return photoscount; } @override public charsequence getpagetitle(int position) { locale l = locale.getdefault(); return photos.get(position).photoname; } } /** * dummy fragment representing section of app, * displays dummy text. */ public static class dummysectionfragment extends fragment { /** * fragment argument representing section number * fragment. */ public static final string arg_section_number = "section_number"; public dummysectionfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate( r.layout.fragment_photo_gallery_dummy, container, false); imageview img = (imageview) rootview.findviewbyid(r.id.photo_result); bundle args = getarguments(); int position = args.getint("position"); img.settag(photos.get(position)); imagedownloader = new imagedownloader(img,this.getactivity()); i.execute(photos.get(position).url); return rootview; } }
}
solved myself
solution : initialize static arraylist photos inside oncreate() .
Comments
Post a Comment