根据接收的json响应加载不同的图像

时间:2015-02-25 05:23:21

标签: android json image

我得到一个json响应,并从即时通讯使用项目详细信息加载到菜单网格中,为我使用适配器。目前我能够加载所有项目细节,如ImageURL,描述,价格。但是我在加载isVeg细节时遇到了问题。 isveg返回true或false,然后如果响应为true,那么我必须加载image1,如果响应为false,那么我必须使用image2。应该在哪里检查n怎么做?如果需要,我应该在哪里使用if条件?

任何帮助将不胜感激。

片段类

           if ((object.getString("MainCategoryID")).equals("1")
                        && (object.getString("SubCategoryID")).equals("9")) {
                    Log.i("ImageURL ", object.getString("ImageURL"));
                    imageUrls.add(object.getString("ImageURL"));
                    Log.i("Description ", object.getString("Description"));
                    descriptions.add(object.getString("Description"));
                    Log.i("Price ", object.getString("Price"));
                    price.add(object.getString("Price"));
                    Log.i("IsVeg ", object.getString("IsVeg"));
                    isVeg.add(object.getString("IsVeg"));
                }
            }

            CustomGridPizza adapter = new CustomGridPizza(getActivity(), descriptions,
                    imageUrls, price);
            grid.setAdapter(adapter);

customgridpizzaadapter

    public class CustomGridPasta extends BaseAdapter {
    private Context context;
    private final List<String> descriptions;
    private final List<String> imageUrls;
    private final List<String> price;
    private final List<String> isVeg;

    public CustomGridPasta(Context c, List<String> descriptions,
            List<String> imageUrls, List<String> price, List<String> isVeg) {
        this.context = c;
        this.descriptions = descriptions;
        this.imageUrls = imageUrls;
        this.price = price;
        this.isVeg = isVeg;
    }

    @Override
    public int getCount() {
        return descriptions.size();
    }

    @Override
    public Object getItem(int position) {
        return descriptions.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.pasta_single_item, parent, false);
            holder.ivImage = (ImageView) convertView
                    .findViewById(R.id.grid_image);
            holder.tvImageIcon = (ImageView) convertView
                    .findViewById(R.id.icon);
            holder.tvHeader = (TextView) convertView
                    .findViewById(R.id.grid_text);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvHeader.setText(descriptions.get(position));
        Picasso.with(this.context).load(imageUrls.get(position))
        .into(holder.ivImage);


        if (isVeg.equals(true)) {
        Bitmap mBitmap = BitmapFactory.decodeResource(this.context.getResources(), R.drawable.veg);
    } else {
        Bitmap mBitmap = BitmapFactory.decodeResource(this.context.getResources(), R.drawable.nonveg);
    }
    holder.tvImageIcon.setImageBitmap(mBitmap); //mBitmap cannot be resolved to a variable

        Button customizePasta = (Button) convertView
                .findViewById(R.id.bt_direct_customize);
        customizePasta.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent next = new Intent(context, ActivityPastaCustomize.class);
                next.putExtra("description", descriptions.get(position));
                next.putExtra("imageUrl", imageUrls.get(position));
                next.putExtra("price", price.get(position));
                context.startActivity(next);
                ((Activity) context).overridePendingTransition(
                        R.anim.slide_in_right, R.anim.slide_out_left);
            }
        });

        return convertView;
    }

    private class ViewHolder {
        private TextView tvHeader;
        private ImageView ivImage;
        private ImageView tvImageIcon;
    }
}

1 个答案:

答案 0 :(得分:1)

使用positionisVeg列表获取值,例如:

String strIsVag=isVeg.get(position);
Bitmap mBitmap; 
if (strIsVag.contains("true")) {
        mBitmap = BitmapFactory.decodeResource(
                              this.context.getResources(), R.drawable.veg);
    } else {
        mBitmap = BitmapFactory.decodeResource(
                           this.context.getResources(), R.drawable.nonveg);
}