0

Viewpager を使用して、前のアクティビティから渡されたインテント値に応じて SQLite データベースから写真の URL を取得するスクロール可能な写真ギャラリーを作成しています。

私の問題は次のとおりです。このフォト ギャラリーを初めて起動すると正しく動作しますが、閉じて画像の別の 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;
    }

}

}

4

1 に答える 1

0

自力で解決

解決策: onCreate() 内の静的 ArrayList 写真を初期化します。

于 2013-08-10T20:12:06.767 に答える