0

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;


}
boudi
  • 682
  • 1
  • 8
  • 19
  • You posted irrelevant code. You have to determine position in the on click handler. So show please. – greenapps Sep 16 '14 at 20:36
  • irrelevant code ? ... My Images file list paths are saved in this code I gave (ImageAdapter) all the paths are saved in public ArrayList .. my question is how to get the postion of this path and open it as an Image .. I think you didn't read the code carefully – boudi Sep 16 '14 at 20:41
  • MyAdapter.getItem(position) is ok. What else? Why would this be wrong? You didn't tell that. Please tell the value (what is the path exactly)? And if it is really a file path you cannot use `setImageResource()` as it would not be from resources then. So use something (if that exists) like loadImageFromFile(). And if that does not exists use BitmapFactory to load from file and assign the bitmap. – greenapps Sep 16 '14 at 20:48
  • I would like from you to show me codes and I will accept it .. so far you are the only one who responses – boudi Sep 16 '14 at 23:21
  • You can set an onItemClickListener on your GridView. One of the parameters in this listener is the item position. Use it there. – joao2fast4u Sep 16 '14 at 23:45

2 Answers2

1

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!

DejanRistic
  • 2,039
  • 18
  • 13
0

MyAdapter.getItem(position) is ok. What else? Why would this be wrong? You didn't tell that. Please tell the value (what is the path exactly)? And if it is really a file path you cannot use setImageResource() as it would not be from resources then. So use something (if that exists) like loadImageFromFile(). And if that does not exists use BitmapFactory to load from file and assign the bitmap.

The title of your post is very confusing and not to the point. You know already 'position' and you know how to geth the right path. The only thing you didn't know is how to load an image from file into an ImageView. But now you know. And if you need code.. Just google this site for it as such code has been posted here many times.

greenapps
  • 11,154
  • 2
  • 16
  • 19