我正在使用View Pager来滑动图像,但View Pages重复显示相同的图像并从JSON URL Images正确获取索引值。 它只能获取重复显示的图像的最后一个索引。我不知道我做了什么只会错误只显示在我的视图寻呼机中的图像的最后一个索引。
代码:
ViewPager viewPager;
private ZoomAdapter adapter;
String oneimg;
String singlepro, multipleimg, productpaths, sellers;
ArrayList<ZoomimageModel> imgs;
ArrayList<HashMap<String, String>> adlist;
HashMap<String, String> map = new HashMap<String, String>();
// ArrayList<HashMap<String, String>> adlist
public static final String TAG_IMAGE = "original_res";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zoomproduct_);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.b2)
.showImageForEmptyUri(R.drawable.b2)
.showImageOnFail(R.drawable.b2)
.build();
/******************** this is for single page product ***********************/
Intent singleproductpage = getIntent();
singlepro = singleproductpage.getStringExtra("currentinedx");
Intent multipleimages = getIntent();
multipleimg = multipleimages.getStringExtra("multipleimages");
Intent productpath = getIntent();
productpaths = productpath.getStringExtra("productpath");
Intent seller = getIntent();
sellers = seller.getStringExtra("sellerid");
// pDialog = new ProgressDialog(Zoomproduct_Activity.this);
// pDialog.setMessage("Please wait...");
// pDialog.setCancelable(false);
singleproduct();
viewPager = (ViewPager) findViewById(R.id.pager);
// Pass results to ViewPagerAdapter Class
adapter = new ZoomAdapter(Zoomproduct_Activity.this, imgs);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
}
public void singleproduct() {
String productpath = productpaths;
String sid = sellers;
String cindex = singlepro;
int cindexs = Integer.parseInt(cindex);
String multiimg = multipleimg;
JSONArray multipleimage = null;
JSONArray singleimages = null;
try {
multipleimage = new JSONArray(multiimg);
singleimages = new JSONArray(multiimg);
horizontalimage = (LinearLayout) findViewById(R.id.gallery);
// final RelativeLayout r1=(RelativeLayout)findViewById(R.id.relative_border);
// frame=(FrameLayout)findViewById(R.id.framelayout);
ii = new ImageView[multipleimage.length()];
ZoomimageModel zoom = new ZoomimageModel();
imgs = new ArrayList<ZoomimageModel>();
adlist = new ArrayList<HashMap<String, String>>();
if (singleimages.length() > 0) {
for (int i = 0; i < singleimages.length(); i++) {
JSONObject singleimg = multipleimage.getJSONObject(i);
String oneimg = singleimg.getString("original_res");
String singleiamges = productpath + sid + '/' + oneimg;
/*map.put(TAG_IMAGE,singleiamges);
adlist.add(map);*/
zoom.setImageUrl(singleiamges);
imgs.add(zoom);
String ad = String.valueOf(adlist);
Toast.makeText(getApplicationContext(), ad, Toast.LENGTH_LONG).show();
Log.d(ad, "show");
}
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
适配器:
public class ZoomAdapter extends PagerAdapter {
LayoutInflater inflator;
ImageView imgDisplay;
Context context;
ArrayList<ZoomimageModel> slideShowImages;
DisplayImageOptions options;
public ZoomAdapter(Context context, ArrayList<ZoomimageModel> slideShowImages) {
this.context = context;
this.slideShowImages = slideShowImages;
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.slip1)
.showImageForEmptyUri(R.drawable.b2)
.showImageOnFail(R.drawable.ic_drawer)
.build();
}
@Override
public int getCount() {
return slideShowImages.size();
}
// @Override
/* public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}*/
// @Override
public Object instantiateItem(ViewGroup container, final int position) {
inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflator.inflate(R.layout.productzoomrow, container,
false);
imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgDisplay);
ZoomimageModel zooms = slideShowImages.get(position);
System.out.println("ImageUrl---------------------" + slideShowImages.get(position).getImageUrl());
YelloPage.imageLoader.displayImage(zooms.getImageUrl(), imgDisplay, options);
container.addView(viewLayout);
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
/* @Override
public void destroyItem(ViewGroup container, int position, Object object)
{
((ViewPager) container).removeView((ImageView) object);
}*/
}
logcat的:
-- -- -- -- - beginning of crash
05 - 06 07: 00: 43.434 15557 - 15557 / com.journaldev.navigationdrawer E / AndroidRuntime: FATAL EXCEPTION: main
Process: com.journaldev.navigationdrawer, PID: 15557
java.lang.RuntimeException: Unable to start activity ComponentInfo {
com.journaldev.navigationdrawer / com.journaldev.navigationdrawer.Activity.Zoomproduct_Activity
}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.journaldev.navigationdrawer.Adapter.ZoomAdapter.notifyDataSetChanged()'
on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2387)
at android.app.ActivityThread.access$800(ActivityThread.java: 151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1303)
at android.os.Handler.dispatchMessage(Handler.java: 102)
at android.os.Looper.loop(Looper.java: 135)
at android.app.ActivityThread.main(ActivityThread.java: 5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java: 372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.journaldev.navigationdrawer.Adapter.ZoomAdapter.notifyDataSetChanged()'
on a null object reference
at com.journaldev.navigationdrawer.Activity.Zoomproduct_Activity.singleproduct(Zoomproduct_Activity.java: 198)
at com.journaldev.navigationdrawer.Activity.Zoomproduct_Activity.onCreate(Zoomproduct_Activity.java: 102)
at android.app.Activity.performCreate(Activity.java: 5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2387)
at android.app.ActivityThread.access$800(ActivityThread.java: 151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1303)
at android.os.Handler.dispatchMessage(Handler.java: 102)
at android.os.Looper.loop(Looper.java: 135)
at android.app.ActivityThread.main(ActivityThread.java: 5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java: 372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 698)
任何人都乐意欣赏。
答案 0 :(得分:0)
您在初始化 ZoomAdapter(adapter.notifyDataSetChanged();
)之前呼叫adapter = new ZoomAdapter(Zoomproduct_Activity.this, imgs);
(在 singleproduct())。
初始化ZoomAdapter后请致电adapter.notifyDataSetChanged();
。
答案 1 :(得分:0)
用以下代码替换你的代码:
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new ZoomAdapter(Zoomproduct_Activity.this, imgs);
viewPager.setAdapter(adapter);
adapter.notifyDataSetChanged();