使用json在列表视图中显示图像和文本
问题描述:
我从服务器使用json数组通过url从mysql数据库中获取图像和数据。我能够列表查看所有图像,但我无法将图像与文字一起显示。我想我是使用视图holder.how创建自定义布局我可以创建我的自定义布局。使用json在列表视图中显示图像和文本
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 我想显示在列表视图图像和名称追加...请任何人能帮助我。
答
使用适配器这样的,采取一切文本内容和位图到数组调用适配器
class NamesAdapter extends BaseAdapter{
// @Override
public int getCount() {
// TODO Auto-generated method stub
return mImages.length;
}
// @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
// @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
// @Override
public View getView(int pos, View convertView, ViewGroup parent) {
View row=getLayoutInflater().inflate(R.layout.imagetext,null);
TextView text=(TextView)row.findViewById(R.id.text);
ImageView image=(ImnageView)row.findViewById(R.id.image);
name.setText(text[pos]);
image.setImageBitmap(resizedbitmap[pos]);
return row;
}
}
答
的ImageAdapter类应该扩展BaseAdapter之前。请参阅http://pradeep-sharma.com/blog/android-custom-listview-baseadapter-tutorial/。
+0
请更改我的代码让我为阵列适配器发送我 – Vinoth 2012-03-27 07:24:56
在getlayoutinflater是错误就说明你想这样我怎么能解决这个错误 – Vinoth 2012-03-27 07:21:19
请先生我等待UR答复 – Vinoth 2012-03-27 07:33:38
我可以main.java文件先生将 – Vinoth 2012-03-27 07:34:27