Android:如何将drawable转换为int?

2 人关注

我使用这个代码得到了一些已安装的应用程序的图标。

Drawable icon = getPackageManager().getApplicationIcon("com.example.example");
imageView.setImageDrawable(icon);

然后我想把这个图片添加到一个由res中的一些图片组成的网格视图中。我不想改变我的网格视图使用的适配器。

ArrayList<String> listCountry = new ArrayList<String>();
 ArrayList<Integer> listFlag = new ArrayList<Integer>(); 

我就这样在我的listFlag上添加元素。【替换代码2

那么,我怎样才能将Drawble转换为int并将其添加到listFlag中呢?

public  class GridviewAdapter extends BaseAdapter
    private ArrayList<String> listCountry = new ArrayList<String>();
    private ArrayList<Integer> listFlag = new ArrayList<Integer>();
    private Activity activity;
    private Context context;
    public GridviewAdapter( ) {
         super();
    public GridviewAdapter(Activity activity, ArrayList<String> listC, ArrayList<Integer> listF) {
        super();
        this.listCountry.clear();
        this.listFlag.clear();
        //this.notifyDataSetChanged();
        this.listCountry.addAll(listC);
        this.listFlag.addAll(listF);
        this.activity = activity;
        this.context = activity.getApplicationContext();
      this.notifyDataSetChanged();}
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listCountry.size();
    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return listCountry.get(position);
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    public static class ViewHolder
        public ImageView imgViewFlag;
        public TextView txtViewTitle;
        public TextView txtViewDescription;
    public void setFile(ArrayList<String> listC, ArrayList<Integer> listF) {
        listFlag.clear();
        listCountry.clear();
         listCountry.addAll(listC);
        listFlag.addAll(listF);
        notifyDataSetChanged();
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();
        if(convertView==null)
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.gridview_row, null);
            view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
            view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
            view.txtViewDescription = (TextView) convertView.findViewById(R.id.textView2);
            convertView.setTag(view);
            convertView.setLayoutParams(new GridView.LayoutParams((int) context.getResources().getDimension(R.dimen.r_width), (int) context.getResources().getDimension(R.dimen.r_height)));
            view = (ViewHolder) convertView.getTag();
        view.txtViewTitle.setText(listCountry.get(position));
        view.txtViewTitle.setSelected(true);
        view.txtViewDescription.setText(SessionChoose.DESCRIPTION.get(position));
         view.txtViewDescription.setSelected(true);
        view.imgViewFlag.setImageResource(listFlag.get(position));
        return convertView;
    
2 个评论
你打算用 listFlag 项目做什么?作为 imageView.setImageResource(int resId) 的参数?
zied
我不想打印imageeview。这只是为了测试我的图标。
android
android-drawable
zied
zied
发布于 2014-05-13
3 个回答
gerbit
gerbit
发布于 2014-05-13
0 人赞同

没有任何方法可以做到这一点...为了解决你的问题,你应该使用 ArrayList<Drawable> listFlag

这一定是个评论,请删除答案
这就是答案。问题是 "如何将drawable转换为int"?- 答案是你不能。
Uriel Frankel
Uriel Frankel
发布于 2014-05-13
0 人赞同

Use getId()

listFlag.add(imageView.getId());
    
pooja
pooja
发布于 2014-05-13
0 人赞同

你可以首先通过使用来设置id。

imageView.setId(1);