使用android中的json从服务器数据库显示图像和文本

时间:2012-03-27 09:52:40

标签: android

我可以使用json将图像从mysql db获取到android到列表视图。我希望将图像和文本显示为列表视图

enter code here
 Main.java
  JSONArray json = jArray.getJSONArray("names");
        list=(ListView)findViewById(R.id.list);
         adapter=new ImageAdapter(this, json);
         list.setAdapter(adapter);
  ImageAdapter.java
    public class ImageAdapter extends BaseAdapter {
String qrimage;
Bitmap bmp, resizedbitmap;
private static LayoutInflater inflater = null;

private ImageView[] mImages;
String[] itemimage;
TextView[] tv;
String itemname;
HashMap<String, String> map = new HashMap<String, String>();

public ImageAdapter(Context context, JSONArray imageArrayJson) {
    this.mImages = new ImageView[imageArrayJson.length()];

    try {

        for (int i = 0; i < imageArrayJson.length(); i++) {
            JSONObject image = imageArrayJson.getJSONObject(i);
            qrimage = image.getString("itemimage");
            itemname = image.getString("itemname");
            map.put("itemname", image.getString("itemname"));
            System.out.println(itemname);

            byte[] qrimageBytes = Base64.decode(qrimage.getBytes());

            bmp = BitmapFactory.decodeByteArray(qrimageBytes, 0,
                    qrimageBytes.length);
            int width = 100;
            int height = 100;
            resizedbitmap = Bitmap.createScaledBitmap(bmp, width, height,
                    true);

            mImages[i] = new ImageView(context);
            mImages[i].setImageBitmap(resizedbitmap);

            mImages[i].setScaleType(ImageView.ScaleType.FIT_START);
            // tv[i].setText(itemname);
        }

    } catch (Exception e) {
        // TODO: handle exception
    }
}

public int getCount() {
    return mImages.length;
}

public Object getItem(int position) {
    return position;
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    /* View row=inflater(R.layout.listview,null);
        TextView text=(TextView)row.findViewById(R.id.text);
                ImageView image=(ImageView)row.findViewById(R.id.image);

      text.setText(itemname[position]);
               image.setImageBitmap(resizedbitmap[pos]);
        return row;*/
    return mImages[position];
}

   }

上面的代码我可以打印itemname。表示它在logcat中打印所有项目名称。我只能在listview中查看图像。如何将文本视图附加到itemname如何将itemimage和itemname显示为listview ..请不要给出其他示例请告诉我,我可以做什么其他明智的修改代码,我可以搞错..请帮帮我

1 个答案:

答案 0 :(得分:1)

您可以使用此代码显示图像。 ClueImgURL是图像的网址。

       InputStream is = null;
            try {
                is = (InputStream) new URL(ClueImgURL).getContent();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace(); 
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        Drawable d = Drawable.createFromStream(is, "src name");
        mImages[i].setBackgroundDrawable(d);