title: set image in dynamic grid view
body: <p><strong>GridActivity.java</strong></p>
  
  <pre><code>import java.io.BufferedOutputStream;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.Reader;
  import java.io.StringWriter;
  import java.io.Writer;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URLConnection;
  import java.util.ArrayList;
  import java.util.Collection;
  
  import org.apache.http.HttpResponse;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.entity.StringEntity;
  import org.apache.http.impl.client.DefaultHttpClient;
  
  import android.app.Activity;
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.BaseAdapter;
  import android.widget.GridView;
  import android.widget.ImageView;
  import android.widget.Toast;
  import android.widget.AdapterView.OnItemClickListener;
  
  public class GridActivity extends Activity
  {
  ArrayList&lt;String&gt; imageurl=new ArrayList&lt;String&gt;();
  String albumId= ConstantData.album_id;
  String userId = ConstantData.user_id;
  int pageNo =1;
  int limit = 20;
  ArrayList&lt;Object&gt; result;
  XmlParser parser;
  
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.photos_activity);
  //Add required urls
  
  try {
  DefaultHttpClient client = new DefaultHttpClient();
  HttpPost post = new HttpPost("http://192.168.5.10/ijoomer_development/index.php?option=com_ijoomer&amp;plg_name=jomsocial&amp;pview=album&amp;ptask=photo_paging&amp;userid="+ ConstantData.user_id +"&amp;sessionid="+ ConstantData.session_id +"&amp;tmpl=component&amp;albumid="+ ConstantData.album_id +"&amp;pageno=1&amp;limit=20");
  StringBuffer strBuffer = new StringBuffer("&lt;data&gt;&lt;userid&gt;" + userId + "&lt;/userid&gt;&lt;albumid&gt;" + albumId + "&lt;/albumid&gt;&lt;pageno&gt;" + pageNo +"&lt;/pageno&gt;&lt;limit&gt;"+ limit +"&lt;/limit&gt;&lt;/data&gt;");
  StringEntity strEntity = new StringEntity(strBuffer.toString());
  post.setEntity(strEntity);
  HttpResponse response = client.execute(post);
  
  InputStream in = response.getEntity().getContent();
  
  String strResponse = convertStreamToString(in);
  parser = new XmlParser(in, new AddAlbumDetailBean());
  result = parser.parse("data", "photo");
  
  //Log.i("aBean Value", ""+aBean);
  
  //System.out.println(strResponse);
  
  String startCode = "&lt;code&gt;";
  String endCode = "&lt;/code&gt;";
  String starPhotoCount = "&lt;photocount&gt;";
  String endPhotoCount ="&lt;/photocount&gt;";
  String starPhotos = "&lt;photos&gt;";
  String endPhotos ="&lt;/photos&gt;";
  String startPhoto = "&lt;photo&gt;";
  String endPhoto = "&lt;/photo&gt;";
  
  String startId = "&lt;Id&gt;";
  String endId = "&lt;/Id&gt;";
  String starTitle = "&lt;title&gt;";
  String endTitle ="&lt;/title&gt;";
  String startThumb ="&lt;thumb&gt;";
  String endThumb = "&lt;/thumb&gt;";
  String startUrl = "&lt;url&gt;";
  String endUrl = "&lt;/url&gt;";
  
  if (startCode.equalsIgnoreCase("&lt;code&gt;") &amp;&amp; endCode.equalsIgnoreCase("&lt;/code&gt;"))
  {
  int startC = strResponse.indexOf(startCode);
  int endC = strResponse.indexOf(endCode);
  
  Log.i("startCode", ""+startC);
  Log.i("endCode", ""+endC);
  
  String OldCode = strResponse.substring(startC, endC);
  int startCodeindex = OldCode.indexOf("&gt;");
  String code = OldCode.substring(startCodeindex + 1).trim();
  
  Log.i("Code", ""+code);
  }
  if (starPhotoCount.equalsIgnoreCase("&lt;photocount&gt;") &amp;&amp; endPhotoCount.equalsIgnoreCase("&lt;/photocount&gt;"))
  {
  int startPC = strResponse.indexOf(starPhotoCount);
  int endPC = strResponse.indexOf(endPhotoCount);
  
  Log.i("starPhotoCount", ""+startPC);
  Log.i("endPhotoCount", ""+endPC);
  
  String OldPC = strResponse.substring(startPC, endPC);
  int startPCindex = OldPC.indexOf("&gt;");
  String photocount = OldPC.substring(startPCindex + 1).trim();
  
  Log.i("PhotoCount", ""+photocount);
  }
  if (starPhotos.equalsIgnoreCase("&lt;photos&gt;") &amp;&amp; endPhotos.equalsIgnoreCase("&lt;/photos&gt;"))
  {
  int startPs = strResponse.indexOf(starPhotos);
  int endPs = strResponse.indexOf(endPhotos);
  
  Log.i("starPhotos", ""+startPs);
  Log.i("endPhotos", ""+endPs);
  
  String OldPhotos = strResponse.substring(startPs, endPs);
  int startPhotosindex = OldPhotos.indexOf("&gt;");
  String photos = OldPhotos.substring(startPhotosindex + 1).trim();
  Log.i("Photos", ""+photos);
  }
  if (startPhoto.equalsIgnoreCase("&lt;photo&gt;") &amp;&amp; endPhoto.equalsIgnoreCase("&lt;/photo&gt;"))
  {
  int startP = strResponse.indexOf(startPhoto);
  int endP = strResponse.indexOf(endPhoto);
  
  Log.i("startPhoto", ""+startP);
  Log.i("endPhoto", ""+endP);
  
  String OldThumb = strResponse.substring(startP, endP);
  int startUrlindex = OldThumb.indexOf("&gt;");
  String photo = OldThumb.substring(startUrlindex + 1).trim();
  
  Log.i("Photo", ""+photo);
  }
  /*if (startId.equalsIgnoreCase("&lt;id&gt;") &amp;&amp; endId.equalsIgnoreCase("&lt;/id&gt;"))
  {
  int startI = strResponse.indexOf(startId);
  int endI = strResponse.indexOf(endId);
  
  Log.i("startId", ""+startI);
  Log.i("endId", ""+endI);
  
  String OldId = strResponse.substring(startI, endI);
  int startIdindex = OldId.indexOf("&gt;");
  String id = OldId.substring(startIdindex + 1).trim();
  
  Log.i("ID", ""+id);
  }*/
  
  if (starTitle.equalsIgnoreCase("&lt;title&gt;") &amp;&amp; endTitle.equalsIgnoreCase("&lt;/title&gt;"))
  {
  int startT = strResponse.indexOf(starTitle);
  int endT = strResponse.indexOf(endTitle);
  
  Log.i("startTitle", ""+startT);
  Log.i("endTitle", ""+endT);
  
  String OldTitle = strResponse.substring(startT, endT);
  int startTitleindex = OldTitle.indexOf("&gt;");
  String title = OldTitle.substring(startTitleindex + 1).trim();
  
  Log.i("Title", ""+title);
  }
  
  if (startThumb.equalsIgnoreCase("&lt;thumb&gt;") &amp;&amp; endThumb.equalsIgnoreCase("&lt;/thumb&gt;"))
  {
  int startTh = strResponse.indexOf(startThumb);
  int endTh = strResponse.indexOf(endThumb);
  
  Log.i("startThumb", ""+startTh);
  Log.i("endThumb", ""+endTh);
  
  String OldThumb = strResponse.substring(startTh, endTh);
  int startthumbindex = OldThumb.indexOf("&gt;");
  String thumb = OldThumb.substring(startthumbindex + 1).trim();
  Log.d("Thumb Url", thumb);
  imageurl.add(thumb);
  Log.i("Thu0mb", ""+thumb);
  }
  
  if (startUrl.equalsIgnoreCase("&lt;url&gt;") &amp;&amp; endId.equalsIgnoreCase("&lt;/url&gt;"))
  {
  int startU = strResponse.indexOf(startUrl);
  int endU = strResponse.indexOf(endUrl);
  
  Log.i("startUrl", ""+startU);
  Log.i("endUrl", ""+endU);
  
  String OldUrl = strResponse.substring(startU, endU);
  int startUrlindex = OldUrl.indexOf("&gt;");
  String strUrl = OldUrl.substring(startUrlindex + 1).trim();
  
  Log.i("Url", ""+strUrl);
  }
  
  }
  catch (Exception e) {
  e.printStackTrace();
  }
  
  /*imageurl.add("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
  imageurl.add("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
  imageurl.add("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");*/
  GridView gridView = (GridView) findViewById(R.id.gridview);
  gridView.setAdapter(new ImageAdapter(this));
  
  gridView.setOnItemClickListener(new OnItemClickListener()
  {
  public void onItemClick(AdapterView parent,
  View v, int position, long id)
  {
  Toast.makeText(getBaseContext(),
  "pic" + (position + 1) + " selected",
  Toast.LENGTH_SHORT).show();
  }
  });
  }
  
  public class ImageAdapter extends BaseAdapter
  {
  private Context context;
  
  public ImageAdapter(Context c)
  {
  context = c;
  }
  public int getCount() {
  Log.i("SiZE of ImageUrl", ""+imageurl.size());
  return imageurl.size();
  
  }
  public Object getItem(int position) {
  return position;
  }
  
  public long getItemId(int position) {
  return position;
  }
  public View getView(int position, View convertView, ViewGroup parent)
  {
  ImageView imageView;
  if (convertView == null) {
  imageView = new ImageView(context);
  imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
  imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
  imageView.setPadding(5, 5, 5, 5);
  } else {
  imageView = (ImageView) convertView;
  }
  
  /*AddAlbumDetailBean aBean = (AddAlbumDetailBean)result.get(position);
  imageurl.add(aBean.thumb);*/
  String strURL=imageurl.get(position);
  Bitmap bitmap = null;
  InputStream in = null;
  BufferedOutputStream out = null;
  URL url;
  try {
  url = new URL(strURL);
  URLConnection conn = url.openConnection();
  in=conn.getInputStream();
  Bitmap bitmap1= BitmapFactory.decodeStream(in);
  imageView.setImageBitmap(bitmap1);
  
  } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  
  
  
  //imageView.setImageResource(imagen]);
  return imageView;
  }
  }
  public String convertStreamToString(InputStream in)
  throws IOException {
  
  
  if (in != null) {
  Writer writer = new StringWriter();
  
  char[] buffer = new char[1024];
  try {
  Reader reader = new BufferedReader(
  new InputStreamReader(in, "UTF-8"));
  int n;
  while ((n = reader.read(buffer)) != -1) {
  writer.write(buffer, 0, n);
  }
  } finally {
  // in.close();
  }
  return writer.toString();
  } else { 
  return "";
  }
  }
  }
  </code></pre>
  
  <p><strong>XMLParser.java</strong></p>
  
  <pre><code>import java.io.InputStream;
  import java.net.URL;
  import java.util.ArrayList;
  import java.util.Vector;
  
  import javax.xml.parsers.SAXParser;
  import javax.xml.parsers.SAXParserFactory;
  
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.xml.sax.Attributes;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  import org.xml.sax.XMLReader;
  import org.xml.sax.helpers.DefaultHandler;
  
  import android.util.Log;
  
  public class XmlParser extends DefaultHandler{
  
  public String RootElement;
  public String RecordElement;
  public InputStream in;
  public Object mainObj;  
  public Object newObj;
  public boolean inProcess;
  public String xmlURL;
  
  public ArrayList&lt;Object&gt; Records = null;
  
  private final String TAG = "XmlParser";
  
  StringBuffer buffer = new StringBuffer(); 
  String elementName;
  String elementValue;
  
  public XmlParser(InputStream is,Object tempObj)
  {
  in = is;
  mainObj = tempObj;  
  Log.i("Object value", ""+mainObj);
  inProcess = false;
  }
  
  
  
  public XmlParser(String strURL,Object tempObj)
  {
  xmlURL = strURL;
  mainObj = tempObj;  
  inProcess = false;
  }
  
  public ArrayList&lt;Object&gt; ParseUrl(String rootElement ,String recordElement)
  {
  RootElement = rootElement;
  Log.i("RootElement",RootElement);
  RecordElement = recordElement;
  Log.i("RecordElement", RecordElement);
  
  try
  {
  URL sourceUrl = new URL(xmlURL);
  Log.d("URl", xmlURL);
  SAXParserFactory spf = SAXParserFactory.newInstance();
  SAXParser sp = spf.newSAXParser();
  XMLReader reader = sp.getXMLReader();
  reader.setContentHandler(this);
  
  reader.parse(new InputSource(sourceUrl.openStream()));
  }
  catch(Exception e)
  {
  e.printStackTrace();
  return null;
  }
  
  return this.Records;
  }
  
  
  
  public ArrayList&lt;Object&gt; parse(String rootElement, String recordElement)
  {
  RootElement = rootElement;
  RecordElement = recordElement;
  Log.i("Root Element", ""+RootElement);
  Log.i("Record Element", ""+RecordElement);
  try{
  SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
  parser.parse(in, this);
  }catch(Exception e){
  e.printStackTrace();
  return null;
  }
  return this.Records;
  }
  
  @Override
  public void startElement(String Uri, String localName,String qName,Attributes attributes) throws SAXException
  {
  Log.d("URl", xmlURL);
  elementValue = "";
  Log.i("IN STARTELEMENT", ""+elementValue);
  if(localName.length() &gt; 0)
  {
  Log.i("Local Name Length", ""+localName.length());
  Log.i("LocalName", ""+localName);
  if(localName.equalsIgnoreCase(RootElement))
  {
  Records = new ArrayList&lt;Object&gt;();
  Log.i("Root element", ""+RootElement);
  Log.i("Records", ""+Records);
  }
  else if(localName.equalsIgnoreCase(RecordElement))
  {
  newObj = ClassUtils.newObject(mainObj);
  Log.i("Main Object", ""+mainObj);
  Log.i("Record element", ""+RecordElement);
  ClassUtils.objectMapping(newObj, localName, elementValue);
  Log.i("Element Value", ""+elementValue);
  inProcess = true;
  }
  }
  }
  
  @Override
  public void characters(char[] ch,int start,int length) throws SAXException{
  elementValue+= new String(ch,start,length).trim();  
  Log.i("CHARACTERS VALUE", ""+elementValue);
  }
  
  @Override
  public void endElement(String Uri,String localName,String qName) throws SAXException
  {
  if(localName.equalsIgnoreCase(RecordElement)){
  Records.add(newObj);
  inProcess = false;
  }
  else if(inProcess){ 
  ClassUtils.objectMapping(newObj, localName, elementValue);  
  }
  }   
  }
  </code></pre>
  
  <p><strong>AddAlbumDetailActivity.java</strong></p>
  
  <pre><code>import android.util.Log;
  
  public class AddAlbumDetailBean
  {
  public String data = null;
  public String code = null;
  public String photocount = null;
  public String id = null;
  public String title = null;
  public String thumb = null;
  public String url = null;
  
  public AddAlbumDetailBean()
  {
  this("","","","","","","");
  }
  
  public AddAlbumDetailBean(String data,String code,String photocount,String id,String title,String thumb,String url)
  {
  this.data = data;
  this.code = code;
  this.photocount = photocount;
  this.id = id;
  this.title = title;
  this.thumb = thumb;
  this.url = url;
  }
  
  }
  </code></pre>