Here i update my Custom GridView and i also add the ItemClickListen In It,And Here you can also find the how to get Resources Name By Passing It ID in android.
For that you have to First Refer My Last Post Of Custom GridView at here http://android-vogue.blogspot.com/2011/06/custom-gridview-in-android-with.html so now let see what i update in that is here below
First add the this line in OnCreate Method
mGridViewl.setOnItemClickListener(MyClickListener);
and after that add the
private OnItemClickListener MyClickListener=new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long lg) {
myarray.add(COUNTRIES[position]);
resourcename=getResourceNameByID(R.drawable.class,mThumbIds[position]);
Toast.makeText(Gridview.this, "Image Name is "+ resourcename, 34).show();
//Toast.makeText(Gridview.this, "Mood Has been selected ", Toast.LENGTH_SHORT).show();
//Log.v("log_tag", "the string is " + temp);
}
};
add above function in GridView Class
Now the most Important Part For Getting Resource Name Of Image By Passing ID of that Image we add following method in GridView Class
public String getResourceNameByID(Class<?> aClass,int ResourceID) throws IllegalArgumentException{
Field[]drawableFields =aClass.getFields();
for(Field f:drawableFields){
try{
if(ResourceID==f.getInt(null)){
return f.getName();
}
}
catch(Exception e){
e.printStackTrace();
}
}
throw new IllegalArgumentException();
}
Now you are ready to get Resource Name By Id of Resource.
For that you have to First Refer My Last Post Of Custom GridView at here http://android-vogue.blogspot.com/2011/06/custom-gridview-in-android-with.html so now let see what i update in that is here below
First add the this line in OnCreate Method
mGridViewl.setOnItemClickListener(MyClickListener);
and after that add the
private OnItemClickListener MyClickListener=new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long lg) {
myarray.add(COUNTRIES[position]);
resourcename=getResourceNameByID(R.drawable.class,mThumbIds[position]);
Toast.makeText(Gridview.this, "Image Name is "+ resourcename, 34).show();
//Toast.makeText(Gridview.this, "Mood Has been selected ", Toast.LENGTH_SHORT).show();
//Log.v("log_tag", "the string is " + temp);
}
};
add above function in GridView Class
Now the most Important Part For Getting Resource Name Of Image By Passing ID of that Image we add following method in GridView Class
public String getResourceNameByID(Class<?> aClass,int ResourceID) throws IllegalArgumentException{
Field[]drawableFields =aClass.getFields();
for(Field f:drawableFields){
try{
if(ResourceID==f.getInt(null)){
return f.getName();
}
}
catch(Exception e){
e.printStackTrace();
}
}
throw new IllegalArgumentException();
}
Now you are ready to get Resource Name By Id of Resource.
No comments:
Post a Comment