0

I am trying to create one android application using gridView. In this application I am trying to add images randomly in to the GridView. I have 5 images and that images added in to the drawable like R.drawable.c0, R.drawable.c1, R.drawable.c2, R.drawable.c3, R.drawable.c4.

While we are clicking the each grid the images in each grid will change randomly.My coding is working properly to change the images randomly in each grid click.But I need the help to get the image name.

    public class MainActivity extends Activity {
 GridView gridview;
 ImageView imageView ;

 public static Integer[] mThumbIds = {
    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,


    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,

    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,

    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,

    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,


    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,

    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,

    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,


    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
    R.drawable.c4,


    R.drawable.c0, R.drawable.c1,
    R.drawable.c2, R.drawable.c3,
   };
   @Override
   public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
    gridview = (GridView) findViewById(R.id.gridview);
   gridview.setAdapter(new ImageAdapter(this));
   gridview.setOnItemClickListener(new OnItemClickListener() {
       public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        //gridview.getId();
         imageView = (ImageView) v;
         Random r=new Random();
            int i=r.nextInt(16);
            Log.e("i",""+i);
          imageView.setImageResource(mThumbIds[i]);

          ImageAdapter im = (ImageAdapter)parent.getAdapter();


       }

      @Override
     public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
     }



    public class ImageAdapter extends BaseAdapter {
      private Context mContext;

  // private GridItem[] items;


   public ImageAdapter(Context c) {
       mContext = c;
   }

   public int getCount() {
       return mThumbIds.length;
   }

   public Object getItem(int position) {
       return position;
   }

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

   // create a new ImageView for each item referenced by the Adapter
   public View getView(int position, View convertView, ViewGroup parent) {
       ImageView imageView;
       if (convertView == null) {  // if it's not recycled, initialize some attributes
           imageView = new ImageView(mContext);
           imageView.setLayoutParams(new GridView.LayoutParams(40,40 ));
           imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
           imageView.setPadding(3, 3, 3, 3);
       } else {
           imageView = (ImageView) convertView;
       }

       Random r=new Random();
       int i=r.nextInt(5);

       imageView.setImageResource(mThumbIds[i]);
       return imageView;
     }


   }
    }
Ann
  • 727
  • 1
  • 7
  • 19

2 Answers2

1

Use set/getTag method of View class. You can attach any object to a view and retrieve it later.

imageView.setImageResource(mThumbIds[i]);
imageView.setTag(mThumbIds[i]);

later

Integer thumbId = (Integer)imageView.getTag();
Leonidos
  • 10,482
  • 2
  • 28
  • 37
0

Use holder inside base adapter and change your getview() and also use xml for your cell

 public  class ViewHolder {
        ImageView img;
        }

 public View getView(int position, View convertView, ViewGroup parent) {
               ViewHolder holder;
               if(convertView==null){
                    holder=new ViewHolder();
                    LayoutInflater li = getLayoutInflater();
                    convertView = li.inflate(R.layout.list_item, null);
                    holder.img = (ImageView)convertView.findViewById(R.id.imgView);
                    convertView.setTag(holder); 
                  }
                else
                {
                    holder = (ViewHolder)convertView.getTag();
                }

   Random r=new Random();
   int i=r.nextInt(5);

   holder.img.setImageResource(mThumbIds[i]);//edited
   return convertView;
}

ViewHolder allows you handling recycling. and XML for your cell will give you more flexibility and less complications and calculations at run time.

Siddhesh
  • 1,370
  • 11
  • 28