我正在使用Viewpager创建一个可滚动的照片库,其中包含照片网址 来自SQLite数据库,具体取决于从上一个活动传递的意图值。
我的问题是:当我第一次启动此照片库时,它可以正常工作 但当我关闭它并使用不同的图像URL重新开始时,ViewPager不会更新ImageView和页面标题。
这是我的代码:
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 {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the 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 * from photos where 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 the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.photo_gallery, menu);
return true;
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its 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;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* 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 i = new ImageDownloader(img,this.getActivity());
i.execute(photos.get(position).url);
return rootView;
}
}
}
答案 0 :(得分:0)
由我自己解决
解决方案是:初始化onCreate()中的静态ArrayList照片。