如何将自定义适配器中保存的位图列表添加到主活动中?

时间:2017-12-03 15:36:20

标签: android bitmap

我有一个图像库。这些图像是从在线数据库中提取的。我有每个图像的位图。

按下其中一个,它将放大显示所需的图像。

但是,如果单击(或滑动)当前放大的图像,将显示下一个图像,依此类推,直到达到我的图像库gridview中的最大元素(也许我可以通过执行最终的int来检查它) size = gv.getChildCount();)。

问题如下:包含所有位图的列表在我的GalleryCustomAdapter中,我需要它的位于一个片段Gallery_Restaurant中,我不知道如何从Gallery_Restaurant片段访问GalleryCustomAdapter中的列表

我需要从片段访问的列表是:

public List<Bitmap> pics;

我为它写了一个吸气剂

 public List<Bitmap> getPics()
    {
        return pics;
    }

我附上了部分代码:

1-GalleryCustomAdapter

public class GalleryCustomAdapter extends BaseAdapter {


    private final Context context;
    public View cv;

    public List<Bitmap> pics;


    private List<GalleryComposedObject> lstItems;

    public GalleryCustomAdapter(Context context, List<GalleryComposedObject> lstItems)
    {
        this.context = context;
        this.lstItems = lstItems;
    }


    public List<Bitmap> getPics()
    {
        return pics;
    }



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

    @Override
    public long getItemId (int position) {
        return position;
    }

    @Override
    public Object getItem (int position) {
        return lstItems.get(position);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        GalleryCustomAdapter.RowHoder holder = null;
        if(convertView == null)
        {

            convertView = LayoutInflater.from(context).inflate(R.layout.gallery_layout_instance, parent, false);
            cv= convertView;
            holder = new GalleryCustomAdapter.RowHoder();
            holder.imgIcon = (ImageView) convertView.findViewById(R.id.gallery_image);
            convertView.setTag(holder);
        }
        else
            holder = (GalleryCustomAdapter.RowHoder) convertView.getTag();


        new DownloadImageTask(holder.imgIcon).execute(lstItems.get(position).getIconsUri());
      // holder.imgIcon.setImageResource(lstItems.get(position).getIconsUri());

        return convertView;
    }

    static class RowHoder
    {
        ImageView imgIcon;

    }



    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }
        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon;
        }

        protected void onPostExecute(Bitmap result) {


            pics.add(result);

            bmImage.setImageBitmap(result);
        }
    }


}

2- Gallery_Restaurant片段

public class Gallery_Restaurant extends Fragment {


    private ArrayList<GalleryComposedObject> lstData;
    GridView GalleryGridview;

    private List<Bitmap> zoomimgs;
    View v;
    long currid;


    public Gallery_Restaurant() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.gallery_layout, container, false);

        String _id = getArguments().getString("restid");

        Log.d("this","n" + _id);

        //String _id = getActivity().get_id();

        String method = "getrestpic";
        BackgroundServerTask backgroundtask  = new BackgroundServerTask(view.getContext(),view);
        backgroundtask.execute(method, _id  ,"restaurant");


        /*GalleryGridview = (GridView) view.findViewById(R.id.GalleryGridview);
        lstData = new ArrayList<GalleryComposedObject>() ;
        int i;
        for (i=0; i< iconsUri.length; i++) {
            lstData.add(new GalleryComposedObject(iconsUri[i]));
        }
        GalleryCustomAdapter customAdapter = new GalleryCustomAdapter(view.getContext(), lstData);

        GalleryGridview.setAdapter(customAdapter);*/

        v=view;

        final GridView gv = (GridView) v.findViewById(R.id.GalleryGridview);
        final ImageView Zoom = (ImageView) v.findViewById(R.id.imagezoom);

        Zoom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               /* gv.setVisibility(View.VISIBLE);
                Zoom.setVisibility(View.GONE);*/


               //lenght of the gridview
               // final int size = gv.getChildCount();

                //GalleryCustomAdapter.RowHoder holder = (GalleryCustomAdapter.RowHoder) view.getTag();

            }
        });





        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView parent, View view,int position, long id) {
                GalleryCustomAdapter.RowHoder holder = (GalleryCustomAdapter.RowHoder) view.getTag();

                Bitmap bitmap = ((BitmapDrawable)holder.imgIcon.getDrawable()).getBitmap();
                Zoom.setVisibility(View.VISIBLE);
                Zoom.setImageBitmap(bitmap);
                gv.setVisibility(View.GONE);
                currid=id;

                CustomViewPager ViewPagerObj = (CustomViewPager)  getActivity().findViewById(R.id.pager);
                ViewPagerObj.setSwipeable(false);
            }
        });

        return view;

    }

}

1 个答案:

答案 0 :(得分:0)

您可以将适配器保存为片段中的参数:

private GalleryCustomAdapter customAdapter;

然后在适配器中创建一个公共getter,以便获取所有位图。 与您的getPics()

一样

然后当你需要点击时点击:

Bitmap bitmap = customAdapter.getPics()