获取GridView项的图像名称

时间:2012-12-27 11:57:41

标签: android uiimageview

我有一个gridview,我在对话视图中膨胀。 现在,当我打开对话框(使用网格视图)时,它会显示一组10个图像。

当对话框被取消时,我选择的图像在ImageView上设置。

现在我的查询是否可以获取图像的名称?这是在ImageView ???

上设置的

我必须保存它。

@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;
        }
}

1 个答案:

答案 0 :(得分:4)

是的,如果您拥有图像的资源ID,则可以获取图像的名称。您可以使用getResourceEntryName(resourceId)

String name = getResources().getResourceEntryName(mThumbIds[position]);