'Android: How to convert drawable to int?
I get the icon of some installed applications using this code:
Drawable icon = getPackageManager().getApplicationIcon("com.example.example");
imageView.setImageDrawable(icon);
then I would like to add this image to a gridview formed by some image in res. i don't want to change the adapter used with my gridview.I use for each item an icon and image saved in two Arraylist.
ArrayList<String> listCountry = new ArrayList<String>();
ArrayList<Integer> listFlag = new ArrayList<Integer>();
I add element to my listFlag like that: listFlag.add(R.drawable.ppt);
So how can I convert the Drawble to int to add it to listFlag? my adapter to view the item of my gridview is given by the code below:
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;
}
}
Solution 1:[1]
There are no ways to do that... To solve you problem you should use ArrayList<Drawable> listFlag
Solution 2:[2]
Use getId()
listFlag.add(imageView.getId());
Solution 3:[3]
you can first set id by using:
imageView.setId(1);
then add to listFlag:
listFlag.add(imageView.getId());
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Uriel Frankel |
| Solution 3 | pooja |
