Android 使用json在列表视图中显示图像和文本
我使用json数组通过url从服务器上获取mysql数据库中的图像和数据。我可以查看所有的图片,但是我不能把文字和图片放在一起。我想我是在使用视图保持器创建自定义布局。如何创建自定义布局Android 使用json在列表视图中显示图像和文本,android,mysql,json,Android,Mysql,Json,我使用json数组通过url从服务器上获取mysql数据库中的图像和数据。我可以查看所有的图片,但是我不能把文字和图片放在一起。我想我是在使用视图保持器创建自定义布局。如何创建自定义布局 enter code here JSONArray json = jArray.getJSONArray("names"); list=(ListView)findViewById(R.id.list); adapter=new Image
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>
在此处输入代码
JSONArray json=jArray.getJSONArray(“名称”);
list=(ListView)findViewById(R.id.list);
adapter=新的ImageAdapter(这是json);
list.setAdapter(适配器);
ImageAdapter.java
公共类ImageAdapter扩展了BaseAdapter{
字符串图像;
位图bmp;
专用静态充气机=空;
私有图像视图[]模拟图像;
字符串[]项图像;
TextView[]电视;
字符串itemname;
HashMap=newHashMap();
公共ImageAdapter(上下文上下文,JSONArray imageArrayJson){
this.mImages=newImageView[imageArrayJson.length()];
试一试{
for(int i=0;i
上面的图像附加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。请参阅。在GetLayoutFlater中是错误,它显示您想要创建这样的方法。我如何解决此错误?先生,我正在等待您的答复。我可以将其放入main.java文件中。先生,OMG无需,在中显示您的主类。在那里粘贴代码并复制链接先生,我可以发送Min.java请查看