Search code examples
androidimageloaded

How to get the position of loaded image - android


I'm trying to get the position of my loaded image so when I click it in my GridView it show me it in full screen.

The thing is I don't know how to get the position of my image from my Adapter ! I need to set the imageResurce .. imageView.setImageResource(MyAdapter.getItem(position));this is wrong . I still can't find my position of my loaded image...

My Adapter Code :

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    public ArrayList<String> f = new ArrayList<String>();// list of file paths
    File[] listFile;
    Bitmap myBitmap;
    int position;

    public ImageAdapter(Context context) {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return f.size();
    }

    public Object getItem(int position) {
        return f.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.galleryitem, null);
            holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }


        myBitmap = BitmapFactory.decodeFile(f.get(position));
        holder.imageview.setImageBitmap(myBitmap);
        return convertView;
    }


    public void getFromSdcard()
    {
        File file= new File(android.os.Environment.getExternalStorageDirectory(),"/InstaDownloader-");

        if (file.isDirectory())
        {
            listFile = file.listFiles();


            for (int i = 0; i < listFile.length; i++)
            {
                f.add(listFile[i].getAbsolutePath());
            }
        }
    }
}
class ViewHolder {
    ImageView imageview;


}

Solution

  • You just need to setup an OnItemClickListener and assign it to your GridView. It will give you the position of the item you selected.

     OnItemClickListener myOnItemClickListener = new OnItemClickListener(){
    
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position,
         long id) {
               //Do what you need here, you have the position.
       }};
    

    Then somewhere in your code make sure to assign it to the GridView.

      mGridview.setOnItemClickListener(myOnItemClickListener);
    

    Good Luck!