在viewpager中显示具有相同ID的图像

时间:2013-03-28 07:30:53

标签: android image arraylist android-viewpager universal-image-loader

我的应用是在gridview上点击图片并在viewpager中显示。我的数据库有两个表。 tbl_cafe和tbl_pictures。 tbl_cafe已经存储了id,title,desc和tbl_picture,已经存储了cafe_id和图片。

ImagePagerActivity我查询了tbl_pictures并存储在<ArrayList<HashMap<String, Object>>中。 ImagePagerActivity我使用Viewpager的Universal Image Loader库。在ViewpagerActivity中,我查询了图像名称并将其与url连接以下载图像。 此时它会显示我数据库中的所有图像。

我使用不同的表格导致我在viewpager中仅显示相同的id。

我想要的是,在仅具有相同ID的Viewpager图片中显示图片后,点击TopActivity中我的gridview上的图片。

TopActivity

gridView1.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            Intent i = new Intent(TopActivity.this, ImagePagerActivity.class);
            i.putExtra(CafeDbOpenHelper.CAFE_ID, position);
            i.putExtra(CafeDbOpenHelper.CAFE_TITLE, position);
            i.putExtra(CafeDbOpenHelper.CAFE_ADDRESS, position);
            i.putExtra(CafeDbOpenHelper.CAFE_DESCRIPTION, position);
            startActivity(i);
        }
    });

tbl_cafe&amp; tbl_pictures

enter image description here

ImagePagerActivity Full ImagePagerActivity

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ac_image_pager);

    dataSource = new CafeDataSource(this);
    dataSource.open();
    arrCursor = dataSource.getArrCursor();
    arrCursorPicture = dataSource.getArrCursorPicture();
    pagerPosition  = arrCursorPicture.size();

    ImageLoader.getInstance().init(config);

    if(savedInstanceState != null){
        position = savedInstanceState.getInt(STATE_POSITION);
    }   

    pager = (ViewPager) findViewById(R.id.pager);
    arrImageUrl = getArrImageUrl();
    pager.setAdapter(new ImagePagerAdapter(arrImageUrl));
    pager.setCurrentItem(0);

} // End onCreate

public ArrayList<HashMap<String, Object>> getArrImageUrl(){
    arrImageUrl = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map;
    for (int i = 0; i < pagerPosition; i++) {
        map = new HashMap<String, Object>();
        url = "http://xxx.xxx/xxx/place_pictures/"+arrCursorPicture.get(i).get(CafeDbOpenHelper.PICTURE_URL);
        map.put("image_url", url);
        arrImageUrl.add(map);
    }
    return arrImageUrl;

}

@Override
public void onSaveInstanceState(Bundle outState){
    outState.putInt(STATE_POSITION, pager.getCurrentItem());
}

private class ImagePagerAdapter extends PagerAdapter {

    private ArrayList<HashMap<String, Object>> myArr = new ArrayList<HashMap<String,Object>>();;
    private LayoutInflater inflater;


    ImagePagerAdapter(ArrayList<HashMap<String, Object>> myArr) {
        this.myArr = myArr;
        inflater = getLayoutInflater();
        imageLoader = ImageLoader.getInstance();
    }

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

    @Override
    public void finishUpdate(View container) {
    }

    @Override
    public int getCount() {
        return myArr.size();
    }

    @Override
    public Object instantiateItem(ViewGroup view, int position) {
        View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);
        ImageView imageView = (ImageView) imageLayout.findViewById(R.id.imv_pager);
        final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

        imageLoader.displayImage((String) myArr.get(position).get("image_url"), imageView, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                spinner.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                String message = null;
                switch (failReason.getType()) {
                    case IO_ERROR:
                        message = "Input/Output error";
                        break;
                    case DECODING_ERROR:
                        message = "Image can't be decoded";
                        break;
                    case NETWORK_DENIED:
                        message = "Downloads are denied";
                        break;
                    case OUT_OF_MEMORY:
                        message = "Out Of Memory error";
                        break;
                    case UNKNOWN:
                        message = "Unknown error";
                        break;
                }
                Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();

                spinner.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                spinner.setVisibility(View.GONE);
            }
        });

        ((ViewPager) view).addView(imageLayout, 0);
        return imageLayout;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View container) {
    }
}

}

1 个答案:

答案 0 :(得分:0)

你甚至想在这做什么?

        i.putExtra(CafeDbOpenHelper.CAFE_ID, position);
        i.putExtra(CafeDbOpenHelper.CAFE_TITLE, position);
        i.putExtra(CafeDbOpenHelper.CAFE_ADDRESS, position);
        i.putExtra(CafeDbOpenHelper.CAFE_DESCRIPTION, position);

你所做的就是把你的信息放在一个数字上。显示列表中项目位置的数字。

您希望使用它来从列表中获取原始对象及其数据。