在我的项目中,我使用View寻呼机从数据库中显示我的照片。在我的数据库中,我有两个页面,每个页面都有一个图片。 我可以在我的第一页中获得第一张图片,但查看寻呼机不会显示下一页的第二张图片。我知道原因和原因是我的州没有进入第二页。 我不知道我跳到第二页的位置。
这是我的代码:
public class main_matn2 extends Activity {
private database db;
private String sea;
private String name;
private int Page1;
public 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);
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;
}
@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);
}
}
}
在添加视图寻呼机之前,我的加载方法是这样的:
private void load(String sea, String Name, int page3) {
db.open();
String p = db.getpic("content", sea, Name, page3 + "");
if (p != null) {
int ImageResource = getResources().getIdentifier(p, "drawable", getPackageName());
img.setImageResource(ImageResource);
} else {
img.setVisibility(View.GONE);
}
db.close();
}
我有一个按钮来更改我的页面。(我可以通过此按钮转到第二页。
这是我的按钮(但现在我需要查看寻呼机,我不再需要这个按钮了)
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (Page1 == Page2) {
Toast.makeText(getApplicationContext(), "last page", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(main_matn1.this, CustomTabActivity.class);
startActivity(intent);
} else {
Page2++;
load(sea, name, Page2);
}
}
});
any help?
I am new with android and now I'm very confused.
答案 0 :(得分:0)
你可以改变onCreate方法这一行
加载(海,名称,第2页); 有了这个 加载(海,名称,2);
并在适配器instantiateItem()方法中更改您的代码 String p = db.getpic(" content",sea,Name,img_count +""); 用这个代码 String p = db.getpic(" content",sea,Name,position +"");