Issue
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;
}
}
}
Solution
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();
Answered By - Leonidos
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.