Android 如何将mysql数据库中的图像和名称保存到listview中
我正在从mysql数据库获取图像到我的android手机。我想显示带有名称的图像 在这里输入代码Main.javaAndroid 如何将mysql数据库中的图像和名称保存到listview中,android,Android,我正在从mysql数据库获取图像到我的android手机。我想显示带有名称的图像 在这里输入代码Main.java JSONArray json = jArray.getJSONArray("names"); list=(ListView)findViewById(R.id.list); adapter=new ImageAdapter(this, json); list.setAdapter(adapter); Image adapte
JSONArray json = jArray.getJSONArray("names");
list=(ListView)findViewById(R.id.list);
adapter=new ImageAdapter(this, json);
list.setAdapter(adapter);
Image adapter.java
public class ImageAdapter extends BaseAdapter {
Bitmap bmp;
private static LayoutInflater inflater = null;
private ImageView[] mImages;
String[] itemimage;
String itemname;
HashMap<String, String> map = new HashMap<String, String>();
public ImageAdapter(Context context, JSONArray imageArrayJson) {
this.mImages = new ImageView[imageArrayJson.length()];
String qrimage;
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);
Log.e("Image Height", " Height = " + bmp.getHeight()
+ " , Width = " + bmp.getWidth());
mImages[i] = new ImageView(context);
mImages[i].setImageBitmap(resizedbitmap);
mImages[i].setScaleType(ImageView.ScaleType.FIT_START);
}
} 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];
}
}
my 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="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
上面的代码我可以查看从mysql数据库从服务器到我的android手机的图像。我想在基本适配器获取视图的列表视图中显示项目图像和项目名称我可以显示所有图像。这样我想显示所有项目名称。我打印项目名称,它打印数据库中的所有名称。我想在列表视图中显示图像和名称在列表视图中需要自定义行。 因此,在布局下创建一个名为row.xml的文件
然后使用一个简单的游标适配器,您可以为列表视图提供两个值,图像和文本,而不是使用基本适配器。请尝试使用数组适配器并从这里获取帮助
您可以为显示元素创建一个列表活动,还可以创建一个单独的xml布局文件,该文件将包含列表中单个元素的设计。它被称为创建自定义列表视图。
有关更多信息,请参阅本教程。对于您的问题,您只需要一个xml格式的imageview和textview。我想您将能够管理更多。我建议您查看提供的代码。我想这正是您想要的。此示例图像不是从数据库中获取的。它们是从drawable中提取的。我想要使用基本适配器定制布局