0

jsonを使用して、URLを介してmysqlデータベースから画像と名前を取得しています。リストビューで画像を表示できますが、名前は表示できません。getview メソッド内にカスタム ビューを作成したいのですが、カスタム ビューを作成すると、logcat にエラーが表示されます。コードを参照して、コードを編集して送信してください

public class ImageAdapter extends BaseAdapter {
private static final Context Context = null;
String qrimage;
Bitmap bmp, resizedbitmap;
Activity activity = null;
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);

            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 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 class ViewHolder {
        TextView text;
    }


public View getView(int position, View convertView, ViewGroup parent) {



//  return mImages[position];
      ViewHolder holder = new ViewHolder();
        if (convertView == null){
            convertView = inflater.inflate(R.layout.listview, null);
            holder.text = (TextView)convertView.findViewById(R.id.text);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        holder.text.setText(map.get("itemname"));
        return convertView;
    }


}

レイアウト xml

  <?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>

imageview の mimage と itemname を textview に追加します。最後に、リストビューとしてmimageとitemnameが必要です

4

3 に答える 3

1

で試してみてください

convertView = activity.getLayoutInflater().inflate(R.layout.listview, null);
于 2012-03-28T05:03:26.347 に答える
1
  1. ステートメントを使用しないでください、

    private static LayoutInflater inflater = null;

そのままにしておきます

private static LayoutInflater inflater;
  1. インフレータをfor...loopとtry{}catchブロックの中に入れないでください。例外がスローされた場合、インフレータは初期化されません。

  2. コンストラクター自体の最初の行でインフレーターを初期化します。

  3. 代わりに、

    inflater = LayoutInflater.from(context);

コンストラクターでは、

  1. ハッシュマップの作成方法を確認してください。誤りです。
  2. 所有者には、getView()でTextViewと一緒に更新する必要があるImageviewが必要です。
于 2012-03-28T04:59:13.013 に答える
0

JSONArrayの代わりにStringArrayをImageAdapterクラスに渡すことをお勧めします。setAdapterを呼び出す前にすべての値を取得し、それらをString配列に格納して、渡します。それに応じてImageAdapterクラスを変更する必要があります。

于 2012-03-28T04:52:50.217 に答える