Getting Image Names of GridView Items

£可爱£侵袭症+ 提交于 2019-12-12 23:16:57

问题


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

回答1:


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]);


来源:https://stackoverflow.com/questions/14054316/getting-image-names-of-gridview-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!