在自定义适配器中设置图像资源

时间:2015-03-25 15:59:22

标签: android

我使用此代码在活动中设置图像资源:

ImageView img_categlist = (ImageView) row.findViewById(R.id.img_categlist);
int ImageResource = getResources().getIdentifier(Image[position],"drawable", getPackageName());
img_categlist.setImageResource(ImageResource);

现在我想在自定义适配器中做同样的事情,那么我应该如何更改代码呢? (因为自定义适配器没有上下文,所以getResources()和getPackageName()不起作用)

我的完整代码:

public class CustomAdapter extends BaseAdapter {

    Context context;
    List<RowItem> rowItem;
    private database db;
    private String[] Name;
    private String[] Price;
    private String[] Tarikh;
    private String[] Hotel;
    private String[] Hstars;
    private String[] Image;


    public CustomAdapter(Context context, List<RowItem> rowItem) {
        this.context = context;
        this.rowItem = rowItem;


    }

    @Override
    public int getCount() {

        return rowItem.size();
    }

    @Override
    public Object getItem(int position) {

        return rowItem.get(position);
    }

    @Override
    public long getItemId(int position) {

        return rowItem.indexOf(getItem(position));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_item, null);
        }
        db = new database(context);
        db.open();

        int save = db.sh_categ("charterdb", "داخلی");
        Name = new String [save];
        Price = new String [save];
        Tarikh = new String [save];
        Hotel = new String [save];
        Hstars = new String [save];
        Image = new String [save];


        for (int i = 0; i <save; i++) {

            Name[i]= db.nam_name("charterdb", "داخلی", i, 2);
            Price[i]= db.nam_name("charterdb", "داخلی", i, 7);
            Tarikh[i]= db.nam_name("charterdb", "داخلی", i, 5);
            Hotel[i]= db.nam_name("charterdb", "داخلی", i, 11);
            Hstars[i]= db.nam_name("charterdb", "داخلی", i, 12);
            Image[i]= db.nam_name("charterdb", "داخلی", i, 12);

            }


        ImageView img_categlist = (ImageView) convertView.findViewById(R.id.Img);
        int ImageResource = getResources().getIdentifier(Image[position],"drawable", getPackageName());
        img_categlist.setImageResource(ImageResource);


        // setting the image resource and title
        TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
        TextView txtPrice = (TextView) convertView.findViewById(R.id.price);
        TextView txtTarikh = (TextView) convertView.findViewById(R.id.tarikh);
        TextView txtHotel = (TextView) convertView.findViewById(R.id.hotel);
        // setting the image resource and title
        txtTitle.setText(Name [position]);
        txtPrice.setText(Price [position]);
        txtTarikh.setText(Tarikh [position]);
        txtHotel.setText(Hotel [position]);

        ImageView hstars = (ImageView) convertView.findViewById(R.id.hstar);

        if (Hstars[position].equals("5")) {
            hstars.setImageResource(R.drawable.star5);
        } else if (Hstars[position].equals("4")) {
            hstars.setImageResource(R.drawable.star4);
        } else if (Hstars[position].equals("3")) {
            hstars.setImageResource(R.drawable.star3);
        } else if (Hstars[position].equals("2")) {
            hstars.setImageResource(R.drawable.star2);
        }else {
            hstars.setImageResource(R.drawable.star1);
        } 


        db.close();

        return convertView;

    }

}

0 个答案:

没有答案