我使用这个代码得到了一些已安装的应用程序的图标。
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;