获取GridView的项目映像文件名 [英] Getting Image Names of GridView Items
问题描述
我有我在对话框视图正在膨胀的GridView。
现在,当我打开对话框(与网格视图),它展示了一组10张图像。
当对话框被解雇,我选择设置在ImageView的形象。
现在我的查询可我得到的图像的名称?这是设置在ImageView的???
我要somehwere保存它。
@覆盖
公共无效的onClick(查看视图){
// TODO自动生成方法存根
最后一个对话框groupIconsDialog =新的对话框(CreateGroupActivity.this);
groupIconsDialog.setTitle(选择组图标);
groupIconsDialog.setContentView(R.layout.group_icons_layout);
//调用和图像图标设置为网格视图适配器
GridView控件groupIconsGrid =(GridView控件)groupIconsDialog.findViewById(R.id.grid_groupIcons);
groupIconsGrid.setAdapter(新GroupIconAdapter(CreateGroupActivity.this)); groupIconsGrid.setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>为arg0,ARG1查看,
INT位置,长ARG3){
// TODO自动生成方法存根 group_icon.setImageResource(新GroupIconAdapter(CreateGroupActivity.this).mThumbIds [位置]);
groupIconsDialog.dismiss(); } }); groupIconsDialog.show(); }
});
ImageAdapter
公共类GroupIconAdapter延伸BaseAdapter { 私人语境mContext; //图标图像阵列
公共整数[] = mThumbIds {
R.drawable.mogra,R.drawable.rhino,
R.drawable.zebra,R.drawable.lion,
R.drawable.mogra,R.drawable.rhino,
R.drawable.giraffee,R.drawable.giraffee,
R.drawable.lion,R.drawable.rhino
}; 公共GroupIconAdapter(上下文C){
// TODO自动生成构造函数存根
mContext = C;
} @覆盖
公众诠释的getCount(){
// TODO自动生成方法存根
返回mThumbIds.length;
} @覆盖
公共对象的getItem(INT位置){
// TODO自动生成方法存根
返回mThumbIds [位置]
} @覆盖
众长getItemId(INT为arg0){
// TODO自动生成方法存根
返回0;
} @覆盖
公共查看getView(INT位置,查看视图的ViewGroup父){
// TODO自动生成方法存根
ImageView的ImageView的=新ImageView的(mContext);
imageView.setImageResource(mThumbIds [位置]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(新GridView.LayoutParams(50,50));
返回ImageView的;
}
}
是的,你可以得到图像的名称,如果您有图像的资源ID。您可以使用 getResourceEntryName(RESOURCEID)
字符串名称= getResources()getResourceEntryName(mThumbIds [位置]);
I have a gridview which I am inflating in a Dialog view. Now when I open the dialog ( with Grid View ) it shows a set of 10 images.
when dialog is dismissed the image which I selected is set on the ImageView.
Now my query is can I get the name of the image?? which is set on the ImageView???
I have to save it somehwere.
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
final Dialog groupIconsDialog = new Dialog(CreateGroupActivity.this);
groupIconsDialog.setTitle("Choose Group Icon");
groupIconsDialog.setContentView(R.layout.group_icons_layout);
//calling and setting the image icons to the grid view adapter
GridView groupIconsGrid = (GridView)groupIconsDialog.findViewById(R.id.grid_groupIcons);
groupIconsGrid.setAdapter(new GroupIconAdapter(CreateGroupActivity.this));
groupIconsGrid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
group_icon.setImageResource(new GroupIconAdapter(CreateGroupActivity.this).mThumbIds[position]);
groupIconsDialog.dismiss();
}
});
groupIconsDialog.show();
}
});
ImageAdapter
public class GroupIconAdapter extends BaseAdapter {
private Context mContext;
//icons image array
public Integer[] mThumbIds = {
R.drawable.mogra,R.drawable.rhino,
R.drawable.zebra,R.drawable.lion,
R.drawable.mogra,R.drawable.rhino,
R.drawable.giraffee,R.drawable.giraffee,
R.drawable.lion,R.drawable.rhino
};
public GroupIconAdapter(Context c) {
// TODO Auto-generated constructor stub
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbIds[position];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
return imageView;
}
}
Yes you can get the name of the Image if you have the resource id of the Image. You can use getResourceEntryName(resourceId)
String name = getResources().getResourceEntryName(mThumbIds[position]);
这篇关于获取GridView的项目映像文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!