Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是否在自定义适配器中设置映像资源?_Android - Fatal编程技术网

Android 是否在自定义适配器中设置映像资源?

Android 是否在自定义适配器中设置映像资源?,android,Android,我使用以下代码设置活动中的图像资源: ImageView img_categlist = (ImageView) row.findViewById(R.id.img_categlist); int ImageResource = getResources().getIdentifier(Image[position],"drawable", getPackageName()); img_categlist.setImageResource(ImageResource); 现在我想在自定义适配器

我使用以下代码设置活动中的图像资源:

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;

    }

}
公共类CustomAdapter扩展了BaseAdapter{
语境;
列出项目;
专用数据库数据库;
私有字符串[]名称;
私有字符串[]价格;
私有字符串[]Tarikh;
私人字符串[]酒店;
私有字符串[]Hstars;
私有字符串[]图像;
公共CustomAdapter(上下文,列表行项){
this.context=上下文;
this.rowItem=rowItem;
}
@凌驾
public int getCount(){
返回rowItem.size();
}
@凌驾
公共对象getItem(int位置){
返回rowItem.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回rowItem.indexOf(getItem(position));
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
LayoutInflater mInflater=(LayoutInflater)上下文
.getSystemService(活动.布局\充气机\服务);
convertView=mInflater.充气(R.layout.list_项,空);
}
db=新数据库(上下文);
db.open();
int save=db.sh_categ(“charterdb”、“charterdb”);
名称=新字符串[保存];
价格=新字符串[保存];
Tarikh=新字符串[保存];
Hotel=新字符串[保存];
Hstars=新字符串[保存];
Image=新字符串[保存];

对于(int i=0;我将
Activity context
传递给自定义适配器的构造函数。这里的Activity context指的是您拥有自定义适配器的活动。我更新了,现在您可以看到我的完整代码了,您可以编辑它吗?因为您已经有了context,请使用类似于,
int-ImageResource=context.getResources().getIdentifier(Image[position],“drawable”,context.getPackageName());
请不要在
getView()
中使用数据库实例初始化,它将创建大量数据库对象实例。因此,请将这些行
db=new Database(context);db.open();
放在适配器的构造函数中。并将
db.close()放在
适配器完成“获取全部”视图后,请立即输入。已完成,谢谢。好的