My View寻呼机只显示我的第一张图片

时间:2016-11-11 16:09:18

标签: java android

在我的项目中,我想将我的图片从数据库加载到view pager中。 我的应用程序是这样的:首先我有一个列表视图显示一些标题,当你点击每个项目时,它进入第二个活动来显示我的图片(我刚刚 两张图片)但我可以将第一张图片显示到查看寻呼机中。

这是我的代码:

public class main_matn2 extends Activity {
    private ImageView share;
    private ImageView test;

    //            , img;
    private TextView matn;
    //  ImageButton page;
    private database db;
    private String sea;
    private String name;
    private int Page1;
    private int Page2 = 1;

    private SharedPreferences sp;
    TextView Lesson_Name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main_matn2);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.matn_title);
        Lesson_Name = (TextView) findViewById(R.id.lesson_name);

        db = new database(this);

        test = (ImageView) findViewById(R.id.more);
        share = (ImageView) findViewById(R.id.main_matn_share);
        matn = (TextView) findViewById(R.id.main_matn_matn);

        Bundle ex = getIntent().getExtras();

        sea = ex.getString("sea");
        name = ex.getString("name");
        Page1 = Integer.parseInt(ex.getString("page"));
        Lesson_Name.setText(name);
        Typeface type = Typeface.createFromAsset(getAssets(), "font/font_bold.ttf");
        Lesson_Name.setTypeface(type);

        load(sea, name, Page2);
    }

    private void load(String sea, String Name, int page3) {
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ImagePagerAdapter adapter = new ImagePagerAdapter(page3, Name);
        viewPager.setAdapter(adapter);
        db.open();
    }


    @Override
    public void onBackPressed() {
        sp = getApplicationContext().getSharedPreferences("user", 0);
        Editor ed = sp.edit();
        ed.putString("name", name);
        ed.putString("sea", sea);
        ed.putInt("page", Page1);
        ed.commit();

        finish();
    }

    @Override
    protected void onPause() {
        sp = getApplicationContext().getSharedPreferences("user", 0);
        Editor ed = sp.edit();
        ed.putString("name", name);
        ed.putString("sea", sea);
        ed.putInt("page", Page1);
        ed.commit();

        super.onPause();
    }

    public class ImagePagerAdapter extends PagerAdapter {
        int img_count;
        String Name;

        public ImagePagerAdapter(int count, String Name) {
            img_count = count;
            this.Name = Name;
        }

        @Override
        public int getCount() {
            return img_count; //mImages.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((ImageView) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Context context = main_matn2.this;

            ImageView imageView = new ImageView(context);

            int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);

            imageView.setPadding(padding, padding, padding, padding);

            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

            String p = db.getpic("content", sea, Name, img_count + "");
            if (p != null) {

                int ImageResource = getResources().getIdentifier(p, "drawable", getPackageName());
                imageView.setImageResource(ImageResource);
            }

            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((ImageView) object);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您只看到第一页,因为img_count总是== 1.您应该根据要显示的项目编号返回一个int。

private int Page2 = 1;

load(sea, name, Page2); // Page2 is == 1

private void load(String sea, String Name, int page3) {
    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    ImagePagerAdapter adapter = new ImagePagerAdapter(page3, Name);  // page3 is == 1
    viewPager.setAdapter(adapter);
    db.open();
}

public ImagePagerAdapter(int count, String Name) {
    img_count = count;  // count is == 1
    this.Name = Name;
}

@Override
public int getCount() {
    return img_count; // img_count is always == 1
}