我怎样才能从MySQL数据库中的图像和名称到列表视图 [英] how can i get images and names from mysql database into a listview
问题描述
我是得到MySQL数据库的图片到我的Android手机。我想随着名称的显示图像 输入code这里Main.java
i am get the images from mysql database to my android mobile . i want to display image along with names enter code here Main.java
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>
以上codei又能够从服务器查看从mysql数据库中的图片到我的Android手机。我想随着列表视图中的项目名称在底座适配器,轻松查看,我可以显示我要显示所有项目名称都images.like显示itemimages。我打印与itemNames从数据库打印所有名称。我想显示图像和名称列表视图
the above code i can able to view the images from the mysql database from server to my android mobile. i want display itemimages along with item names in list view in base adapter get view i can display all images.like that i want display all item names. i print itemnames it print all names from database . i want display images and names in list view
推荐答案
您可以创建一个显示元素的列表活动,也创造这将对您的单一列表的元素设计一个单独的XML布局文件。其所谓创建自定义列表视图。 看看这个教程 HTTP://www.softwarepassion。 COM / Android的系列定制列表视图项 - 和 - 适配器/ 以获取更多信息。
you can create a list activity for display elements and also create a separate xml layout file which will have the design of your single element of the list. its called creating a custom list view. have a look at this tutorial http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ for further information.
这篇关于我怎样才能从MySQL数据库中的图像和名称到列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!