使用JSON列表视图中显示图像和文字 [英] displaying images and text in list view using json

查看:192
本文介绍了使用JSON列表视图中显示图像和文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器中使用JSON数组通过URL获取从MySQL数据库中的图像和数据。我可以能够做的ListView所有图像,但我可以不能够处理图像相处的文字。我想我是用视图holder.how我可以创建自定义布局创建自定义布局。

I am fetching the images and data from mysql database from server using json array through url. i can able to do listview all images but i can't able to get text along with images. i think i was create custom layout using view holder.how can i create my custom layout.

            enter code here
       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;
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;
            Bitmap 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) {
      return mImages[position];
}
  }
       i can create one layout
  <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">
   <ImageView
  android:id="@+id/image"
  android:layout_width="50dip"
  android:layout_height="50dip" android:src="@drawable/stub"          
      android:scaleType="centerCrop"/> 
    <TextView
  android:id="@+id/text"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_weight="1"  android:textSize="20dip"      
      android:layout_marginLeft="10dip"/> 
    </LinearLayout>

上面的图片追加listview.xml和文字也与listview.xml追加
我想在列表视图显示图像和名称......请任何一个能帮助我。

the above image append listview.xml and text also append with listview.xml i want display images and names in a listview...please any one can help me.

推荐答案

该ImageAdapter类应该扩展BaseAdapter。请参阅 http://pradeep-sharma.com/blog/android-custom -listview-baseadapter教程/

The ImageAdapter class should extend BaseAdapter. Refer http://pradeep-sharma.com/blog/android-custom-listview-baseadapter-tutorial/ .

这篇关于使用JSON列表视图中显示图像和文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆