在android中使用基本适配器仅显示列表视图中的最后一个图像和文本

时间:2012-03-28 09:23:42

标签: android android-layout

在for循环中,它仅显示最后的数据。在数据库中,有多少行是通过获取最后一项来显示的 所有行都采用最后的图像和文本 现在我想在listview中显示数据库中的所有图像和文本。请帮助我

enter code here
  public class ImageAdapter extends BaseAdapter {
private static final Context Context = null;
String qrimage;
Bitmap bmp, resizedbitmap;
Activity activity = null;
private static LayoutInflater inflater;

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

public ImageAdapter(Context context, JSONArray imageArrayJson) {
    //inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  //  imageLoader=new ImageLoader(activity);
    inflater=LayoutInflater.from(context);
    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"));


            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);
        }
        System.out.println(map);

    } 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 vi=convertView;

            vi = inflater.inflate(R.layout.listview, null);


       TextView text=(TextView)vi.findViewById(R.id.text);
        ImageView image=(ImageView)vi.findViewById(R.id.image);
        image.setImageBitmap(bmp);
        text.setText(map.get("itemname"));

        return vi;


 }


}

1 个答案:

答案 0 :(得分:0)

这一行将把所有内容存储在地图中的一个位置:

map.put("itemname", image.getString("itemname"));

但也许你的意思是:

map.put(itemname, image.getString("itemname"));

您还有一个bmp字段,每次循环都会重置。由于某种原因,您的getView方法有一个position参数,因此它可以索引到一个数组中,并在列表中找到该点的相应信息。相反,您的getView正在为整个适配器实例检索一个bmp和一个itemname - 而不是position所应的索引。

我可能不应该只是给你代码,但在这里。

public class ImageAdapter extends BaseAdapter {
  String qrimage;
  Bitmap bmp, resizedbitmap;
  Bitmap[] bmps;
  Activity activity = null;
  private LayoutInflater inflater;

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

  public ImageAdapter(Context context, JSONArray imageArrayJson) {
    //inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //  imageLoader=new ImageLoader(activity);
    inflater=LayoutInflater.from(context);
    this.mImages = new ImageView[imageArrayJson.length()];
    this.bmps = new Bitmap[imageArrayJson.length()];
    this.itemnames = new String[imageArrayJson.length()];

    try {

      for (int i = 0; i < imageArrayJson.length(); i++) {
        JSONObject image = imageArrayJson.getJSONObject(i);
        qrimage = image.getString("itemimage");
        itemname = image.getString("itemname");
        itemnames[i] = 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);
        bmps[i] = bmp;

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

        mImages[i].setScaleType(ImageView.ScaleType.FIT_START);


        // tv[i].setText(itemname);
      }
      System.out.println(map);

    } 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 vi=convertView;

    vi = inflater.inflate(R.layout.listview, null);


    TextView text=(TextView)vi.findViewById(R.id.text);
    ImageView image=(ImageView)vi.findViewById(R.id.image);
    image.setImageBitmap(bmps[position]);
    text.setText(itemnames[position]);

    return vi;


  }


}