I know this question asked many time but I don't get answered after many research. in my main activity I have a view pager and I set an adapter for that:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager mPager;
PagerAdapter mPagerAdapter;
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
String tag = "onActivityResult";
String msg = "omad";
String timeStamp;
Bitmap reducedImage;
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();//content://media/external/images/media/40596
selectedImagePath = getPath(selectedImageUri);// /storage/emulated/0/DCIM/Camera/IMG_20131022_192859.jpg
//reducedImage = reduceImageSize(selectedImagePath);
Bitmap img = BitmapFactory.decodeFile(selectedImagePath);
File Images = new File(MainActivity.root + File.separator + "Images");
String[] Image = Images.list();
int ImageCount = Image.length;
try {
FileOutputStream fos = new FileOutputStream(new File(root + File.separator +
"/Images" + File.separator + String.valueOf(ImageCount) + ".jpg"));
img.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
} catch (IOException e) {
Log.e("onActivityResult", e.getMessage());
}
}
}
NUM_PAGES += 1;
ScreenSlidePagerAdapter test = new ScreenSlidePagerAdapter(getSupportFragmentManager());
test.notifyDataSetChanged();
}
I have adapter class in my main activity like a private class:
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
@Override
public Fragment getItem(int position) {
Log.v("adapter", "getitem" + String.valueOf(position));
return ScreenSlidePageFragment.create(position);
}
@Override
public int getCount() {
Log.v("adapter", "getcount" + String.valueOf(NUM_PAGES));
return num_pages;
}
and finally I have a fragment class that inflate my layout for each page in view pager.it have image view and text and... :
public class ScreenSlidePageFragment extends Fragment {
public static final String ARG_PAGE = "page";
private int mPageNumber;
private Context mContext;
private int Cimage;
private ArrayList<Uri> imageUri;
public static ScreenSlidePageFragment create(int pageNumber){
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
imageUri = new ArrayList<Uri>();
...
// image uri get uri of image that saved in directory of app
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout containing a title and body text.
//mContext = getActivity();
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.fragment_screen_slide_page, container, false);
// Set the title view to show the page number.
//View v;
ImageView imageView;
//if(convertView == null){
/*LayoutInflater li = LayoutInflater.from(mContext) ;
v = li.inflate(R.layout.imagetext, null);*/
rootView.setId(mPageNumber);
Button button = (Button) rootView.findViewById(R.id.ImageSetting);
button.setId(mPageNumber);
String title = MainActivity.imageSetting.getString(String.valueOf(mPageNumber), "default");
TextView tv = (TextView) rootView.findViewById(R.id.ImageText);
tv.setText(title);
imageView = (ImageView) rootView.findViewById(R.id.image); //imageView = new ImageView(mContext);
SetLayoutSize(mPageNumber);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(layoutWidth, layoutHeight));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(30, 50, 70, 30);
//imageView.setImageResource(mThumbIds[position]);
//imageView.setImageURI(imageUri.get(position));
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = 4;
Bitmap bmp = BitmapFactory.decodeFile(imageUri.get(mPageNumber).getPath() , option);
imageView.setImageBitmap(bmp);
/*((TextView) rootView.findViewById(android.R.id.text1)).setText(
getString(R.string.title_template_step, mPageNumber + 1));*/
return rootView;
}
// this method is not very important
private void SetLayoutSize(int position){
BitmapFactory.Options option = new BitmapFactory.Options();
option.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imageUri.get(position).getPath(), option);
float scale = (float) ((float) ((double)option.outHeight) / ((double)option.outWidth));
//Log.v("reduceimage", String.valueOf(imagetemp.getWidth()) + " * " + String.valueOf(imagetemp.getHeight()));
//Log.v("scale", String.valueOf(scale));
layoutWidth = MainActivity.display.getWidth();
layoutHeight = MainActivity.display.getHeight();
layoutHeight = (int) ( layoutWidth * scale);
//Log.v("size", String.valueOf(width) + " * " + String.valueOf(height));
//Bitmap imagebmp = BitmapFactory.decodeFile(path);
}
and this is layout that each fragment have :
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:id="@+id/ImageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" />
<Button
android:id="@+id/ImageSetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="SettingButton"
android:background="@drawable/ic_menu_moreoverflow_normal_holo_light" />
</RelativeLayout>
</ScrollView>
in my main activity I have button that get image from gallery and save in directory. I should update my adapter in onActivityResult but I get this error :
02-03 19:30:21.567: E/AndroidRuntime(3407): java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 1, found: 2 Pager id: com.sober.secret:id/pager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.example.test.MainActivity$ScreenSlidePagerAdapter
but after app crash , my images show correctly. I try notifyDataSetChanged() but no success and I try any solution in stackoverflow.
num_pages
also? – DragonflynotifyDataSetChanged
every time when num_pages changes should make the work. so Show lines where you are calling notifydatasetchanged – Dragonfly