在android中使用基本适配器仅显示listview中的最后一个图像和文本
在for循环中,它只显示最后的数据。在数据库中,有多少行通过获取最后一项显示了多少行 所有行都是最后一个图像和文本 现在我想在listview中显示数据库中的所有图像和文本。请帮助我在android中使用基本适配器仅显示listview中的最后一个图像和文本,android,android-layout,Android,Android Layout,在for循环中,它只显示最后的数据。在数据库中,有多少行通过获取最后一项显示了多少行 所有行都是最后一个图像和文本 现在我想在listview中显示数据库中的所有图像和文本。请帮助我 enter code here public class ImageAdapter extends BaseAdapter { private static final Context Context = null; String qrimage; Bitmap bmp, resizedbitmap; Acti
enter code here
public class ImageAdapter extends BaseAdapter {
private static final Context Context = null;
String qrimage;
Bitmap bmp, resizedbitmap;
Activity activity = null;
private static LayoutInflater inflater;
private ImageView[] mImages;
String[] itemimage;
TextView[] tv;
String itemname;
HashMap<String, String> map = new HashMap<String, String>();
public ImageAdapter(Context context, JSONArray imageArrayJson) {
//inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// imageLoader=new ImageLoader(activity);
inflater=LayoutInflater.from(context);
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"));
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);
// tv[i].setText(itemname);
}
System.out.println(map);
} 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) {
View vi=convertView;
vi = inflater.inflate(R.layout.listview, null);
TextView text=(TextView)vi.findViewById(R.id.text);
ImageView image=(ImageView)vi.findViewById(R.id.image);
image.setImageBitmap(bmp);
text.setText(map.get("itemname"));
return vi;
}
}
在此处输入代码
公共类ImageAdapter扩展了BaseAdapter{
私有静态最终上下文上下文=null;
字符串图像;
位图bmp,调整大小的位图;
活动=空;
私人静电充气机;
私有图像视图[]模拟图像;
字符串[]项图像;
TextView[]电视;
字符串itemname;
HashMap=newHashMap();
公共ImageAdapter(上下文上下文,JSONArray imageArrayJson){
//充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
//imageLoader=新的imageLoader(活动);
充气器=充气器。从(上下文);
this.mImages=newImageView[imageArrayJson.length()];
试一试{
for(int i=0;i
此行将把所有内容存储在地图上的一个位置:
map.put("itemname", image.getString("itemname"));
但也许你的意思是:
map.put(itemname, image.getString("itemname"));
此外,您还有一个用于bmp
的字段,每次通过循环时都会重置该字段。您的getView
方法有一个position
参数是有原因的,因此它可以索引到数组中,并在列表中找到该点的适当信息。相反,您的getView
正在为整个适配器实例检索一个bmp
和一个itemname
,而不是按position
进行索引
我可能不应该只是给你代码,但它在这里
public class ImageAdapter extends BaseAdapter {
String qrimage;
Bitmap bmp, resizedbitmap;
Bitmap[] bmps;
Activity activity = null;
private LayoutInflater inflater;
private ImageView[] mImages;
String[] itemimage;
TextView[] tv;
String itemname;
String[] itemnames;
HashMap<String, String> map = new HashMap<String, String>();
public ImageAdapter(Context context, JSONArray imageArrayJson) {
//inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// imageLoader=new ImageLoader(activity);
inflater=LayoutInflater.from(context);
this.mImages = new ImageView[imageArrayJson.length()];
this.bmps = new Bitmap[imageArrayJson.length()];
this.itemnames = new String[imageArrayJson.length()];
try {
for (int i = 0; i < imageArrayJson.length(); i++) {
JSONObject image = imageArrayJson.getJSONObject(i);
qrimage = image.getString("itemimage");
itemname = image.getString("itemname");
itemnames[i] = 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);
bmps[i] = bmp;
mImages[i] = new ImageView(context);
mImages[i].setImageBitmap(resizedbitmap);
mImages[i].setScaleType(ImageView.ScaleType.FIT_START);
// tv[i].setText(itemname);
}
System.out.println(map);
} 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) {
View vi=convertView;
vi = inflater.inflate(R.layout.listview, null);
TextView text=(TextView)vi.findViewById(R.id.text);
ImageView image=(ImageView)vi.findViewById(R.id.image);
image.setImageBitmap(bmps[position]);
text.setText(itemnames[position]);
return vi;
}
}
公共类ImageAdapter扩展了BaseAdapter{
字符串图像;
位图bmp,调整大小的位图;
位图[]bmps;
活动=空;
私人充气机;
私有图像视图[]模拟图像;
字符串[]项图像;
TextView[]电视;
字符串itemname;
字符串[]项名称;
HashMap=newHashMap();
公共ImageAdapter(上下文上下文,JSONArray imageArrayJson){
//充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
//imageLoader=新的imageLoader(活动);
充气器=充气器。从(上下文);
this.mImages=newImageView[imageArrayJson.length()];
this.bmps=新位图[imageArrayJson.length()];
this.itemnames=新字符串[imageArrayJson.length()];
试一试{
for(int i=0;i
检查imageArrayJson.length()的长度并使用ViewHolder
但我可以打印外部地图它只需要最后一个值我如何解决上述问题请编辑我的代码并发送给我。我已将数组添加到原始代码中,更新了答案。