Reputation: 149
I can able to fetch the images from mysql db to android using json to the list view . i want display Image and text as listview
enter code here
Main.java
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, resizedbitmap;
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;
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) {
/* View row=inflater(R.layout.listview,null);
TextView text=(TextView)row.findViewById(R.id.text);
ImageView image=(ImageView)row.findViewById(R.id.image);
text.setText(itemname[position]);
image.setImageBitmap(resizedbitmap[pos]);
return row;*/
return mImages[position];
}
}
The above code i can print itemname. means it print all item names in logcat. i can only view images only in listview. how can append text view into itemname how to display itemimage and itemname as listview.. please don't give other examples please tell me what can i do other wise modify code where i can mistake.. please help me
Upvotes: 0
Views: 1826
Reputation:
You can use this code to display the image. ClueImgURL is the url of the image.
InputStream is = null;
try {
is = (InputStream) new URL(ClueImgURL).getContent();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Drawable d = Drawable.createFromStream(is, "src name");
mImages[i].setBackgroundDrawable(d);
Upvotes: 1