In my project I use View pager to show my pics from database.In my database I have two page that every page has one pics. I can get the first pic in my first page but view pager doesn't show my second pic from next page. I know the reason and the reason is that my state don't go to the second page. I don't know where I jump to second page.
here is my code:
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);
}
}
}
my load method before I add the view pager was like this:
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();
}
and I had one button to change my pages .(I could go to the second page by this button.
here is my button (But now I need view pager and I dont need this button anymore)
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.
Can you change onCreate method this line
load(sea, name, Page2); with this load(sea, name, 2);
and in adapter instantiateItem() method change your code String p = db.getpic("content", sea, Name, img_count + ""); with this code String p = db.getpic("content", sea, Name, position + "");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.