0

Finally, i change my code and use univeral-image-loader library

I use paperadapter to display url image like the code ,it works fine.

@Override
public Object instantiateItem(final ViewGroup container, final int position) {
Drawable[] imageDrawable = new Drawable[3];
for (int i = 0; i < 3; i++) {
imageDrawable[i] = LoadImageFromWebOperations(server_url+ image_name.replace(" ","")+ "_0" +String.valueOf(i + 1) + ".jpg");
}
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageDrawable(imageDrawable[position]);
container.addView(imageView, 0);
return imageView;
}

but i want to use asynctask to do this,like the code

@Override
public Object instantiateItem(final ViewGroup container, final int position) {

    AsyncTask<String, Void, Drawable[]> loadingImage = new AsyncTask<String, Void, Drawable[]>(){

        @Override
        protected Drawable[] doInBackground(String... params) {
            // TODO Auto-generated method stub
            Drawable imageDrawable[] = new Drawable[3];
            for (int i = 0; i < 3; i++) {
                imageDrawable[i] = LoadImageFromWebOperations(server_url
                        + image_name.replace(" ","")+ "_0" + String.valueOf(i + 1) + ".jpg");
                System.out.println("doInBackground="+position);
            }
            return imageDrawable;
        }
        @Override
        protected void onPostExecute(Drawable[] result) {
            System.out.println("onPostExecute="+position);
            imageView = new ImageView(getBaseContext());
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setImageDrawable(result[position]);
            container.addView(imageView, 0);
                }
            });
        }

    }; loadingImage.execute();
    return imageView;
}

it do not work fine.i found that the position "1" is null without image,but position 0 and 2 is not null,waiting for some suggestion,thanks!

Giggle
  • 7
  • 5
  • why do you have a loop of loading multiple images, per page? Which of the codes above is the one you wish to use, and what exactly do you want to do? – android developer Jun 19 '14 at 11:46
  • @androiddeveloper thanks for your reply.i use viewpaper indicator to display image from url. use loop because the 3 images have the similar file name only different the number – Giggle Jun 19 '14 at 12:11
  • but instantiateItem doesn't mean you are currently on this page. it just loads the page. for example, if you go to page 1, it means pages 0,1,2 will need to call this function (so that the user will be able to see them when the screen is being scrolled). If you wish to listen to the page being changed, you need to call setOnPageChangeListener . instantiateItem is used to initialize the page, similar to getView of AdapterView (yet without re-using of views). Also, please answer the questions I've asked (or modify your post, which is better), so that I could help you... – android developer Jun 19 '14 at 12:19
  • Also, if LoadImageFromWebOperations is used to show the indicators, it has a misleading name ... – android developer Jun 19 '14 at 12:22

1 Answers1

0

this is the all code :

 public class ProductDetailActivity1 extends FragmentActivity {

        ImageAdapter mAdapter;
        ViewPager mPager;
        CirclePageIndicator mIndicator;
        private String server_url;
        ImageView imageView;    
      protected ScrollView mScrollView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_product_detail1);

            mScrollView = (ScrollView)findViewById(R.id.card_scrollview);


            getActionBar();
            getActionBar().setHomeButtonEnabled(true);
            getActionBar().setDisplayHomeAsUpEnabled(true);
            getActionBar().setDisplayShowTitleEnabled(true);
            getActionBar().setTitle(R.string.product_detail);

            Intent intent = getIntent();
            server_url = intent.getStringExtra("SERVER_URL");

            mAdapter = new ImageAdapter(getApplicationContext());
            mPager = (ViewPager) findViewById(R.id.imageViewpager);
            mPager.setAdapter(mAdapter);


            mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
            mIndicator.setViewPager(mPager);
            mIndicator.setFillColor(0xFFFFFFFF);
            mIndicator.setStrokeColor(0xFFFFFFFF);
            mIndicator.setStrokeWidth(1);
            mIndicator.setRadius(6 * getResources().getDisplayMetrics().density);
            ((CirclePageIndicator) mIndicator).setSnap(true);
            mIndicator
                    .setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageSelected(int position) {
                        }

                        @Override
                        public void onPageScrolled(int position,
                                float positionOffset, int positionOffsetPixels) {
                        }

                        @Override
                        public void onPageScrollStateChanged(int state) {
                        }
                    });
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

            switch (item.getItemId()) {
            case android.R.id.home:
                Intent intent = new Intent(ProductDetailActivity1.this,
                        com.wangrui.ams.MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.product_detail, menu);
            return true;
        }

        public class ImageAdapter extends PagerAdapter {
            private Context mContext;
            private Drawable[] imageDrawable  = new Drawable[3];;

            public ImageAdapter(Context cx) {
                mContext = cx.getApplicationContext();
            }

            @Override
            public Object instantiateItem(final ViewGroup container, final int position) {
                AsyncTask<Void, Void, Drawable[]> loadingImage = new AsyncTask<Void, Void, Drawable[]>(){

                    @Override
                    protected Drawable[] doInBackground(Void... params) {
                        // TODO Auto-generated method stub  

                        for (int i = 0; i < 3; i++) {
                            imageDrawable[i] = LoadImageFromWebOperations(server_url
                                    + image_name.replace(" ","")+ "_0" + String.valueOf(i + 1) + ".jpg");
                            System.out.println("position="+i);
                        }
                        return imageDrawable;
                    }
                    @Override
                    protected void onPostExecute(Drawable[] result) {
                        imageView = new ImageView(mContext);
                        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    imageView.setImageDrawable(result[position]);
                        mPager.getAdapter().notifyDataSetChanged();
                         ((ViewPager) container).addView(imageView, position);

                    }               
                }; loadingImage.execute();

                return imageView;
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return 3;
            }

            @Override
            public boolean isViewFromObject(final View view, final Object object) {
                // TODO Auto-generated method stub
                //return arg0 == (View) arg1;
                return view == ((ImageView) object);
            }

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

        }

        private void showAlertMessage(String msg){

            SuperToast superToast = new SuperToast(getApplicationContext());
            superToast.setAnimations(SuperToast.Animations.FLYIN);
            superToast.setDuration(SuperToast.Duration.LONG);
            superToast.setBackground(SuperToast.Background.RED);
            superToast.setTextSize(SuperToast.TextSize.EXTRA_SMALL);
            superToast.setText(msg);
            superToast.show();
        }

        private Drawable LoadImageFromWebOperations(String url) {
            try {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                        .permitAll().build();
                StrictMode.setThreadPolicy(policy);
                InputStream is = (InputStream) new URL(url).getContent();
                Drawable d = Drawable.createFromStream(is, "src name");
                return d;
            } catch (Exception e) {
                System.out.println("Exc=" + e);
                return null;
            }
        }
    }
Giggle
  • 7
  • 5
  • why did you write it in an answer instead of editing your original question? also, you still didn't make it clear what is the problem and what you are trying to do. do you wish to show an image per page, that will be shown when it's downloaded from the internet? – android developer Jun 19 '14 at 14:15
  • @androiddeveloper sorry for my mistake,i'm a beginner.yes ,i want to display different image in per page using viewpaper indicator library,only to show ,not to download. – Giggle Jun 19 '14 at 14:36
  • If everything is local , why do you have URLs and functions that are named "LoadImageFromWebOperations" ? anyway, if the files are within the app, you can use "getIdentifier", as shown here: http://stackoverflow.com/a/3476470/878126 and then call getResources().getDrawable(...) on what you get from it. – android developer Jun 19 '14 at 18:27