Android:如何将drawable转换为int?

时间:2014-05-13 11:22:59

标签: android android-drawable

我使用以下代码获取了一些已安装应用程序的图标:

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

然后我想将此图像添加到由res中的某个图像形成的网格视图中。我不想更改我的gridview使用的适配器。我为每个项目使用一个图标和图像保存在两个Arraylist中。

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

我将listFlag中的元素添加到:listFlag.add(R.drawable.ppt);

那么如何将Drawble转换为int以将其添加到listFlag?我的适配器来查看我的gridview的项目由下面的代码给出:

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)));
        }
        else
        {
            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;
    }
}

3 个答案:

答案 0 :(得分:0)

没有办法做到这一点......要解决您的问题,您应该使用ArrayList<Drawable> listFlag

答案 1 :(得分:0)

使用getId()

listFlag.add(imageView.getId());

答案 2 :(得分:0)

您可以先使用以下方式设置ID:

imageView.setId(1);

然后添加到listFlag:

listFlag.add(imageView.getId());