In for loop it displaying only last data. In database how many rows are there so many rows are getting displayed by taking the last item
all the rows are taking last image and text
Now i want display all the images and text from the database in listview.please help me
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;
}
}
this line is going to store everything in one spot in the map:
but perhaps you meant:
also you have one field for
bmpthat gets reset each time through the loop. yourgetViewmethod has apositionparameter for a reason, so it can index into an array and find the appropriate information for that spot in the list. instead yourgetViewis retrieving the onebmpand the oneitemnamefor the whole adapter instance — not indexed bypositionas it ought to be.I probably shouldn’t be just giving you the code, but here it is.