problem in forming gridview with images in the web
in my app i am trying to form a grid view from the xml file that been stored in web.
Following is my code
grid = (GridView)findViewById(R.id.gridView1);
imageXMLfn();
grid.setAdapter(new ImageAdapter(this));
private void imageXMLfn()
{
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
factory.setCoalescing(true);
factory.setNamespaceAware(false);
factory.setValidating(false);
DocumentBuilder parser = factory.newDocumentBuilder();
URL url = new URL(UserManual.IMAGE_URL);
Log.e("ViewImage3",""+UserManual.IMAGE_URL);
Docu开发者_运维知识库ment document= parser.parse(new InputSource(url.openStream()));
NodeList sections = document.getElementsByTagName("application");
numSections = sections.getLength();
for (int i = 0; i < numSections; i++)
{
Element section = (Element) sections.item(i);
if(section.hasChildNodes()==true)
{
NodeList section1=section.getChildNodes();
for(int j=0;j<section1.getLength();j++)
{
if(section1.item(j).hasChildNodes()==true)
{
for(int k=0;k<section1.item(j).getChildNodes().getLength();k++)
{
xmlvalue=String.valueOf(section1.item(j).getChildNodes().item(k).getNodeValue()).trim();
arl.add(xmlvalue);
}
}
}
}
}
}
catch(Exception e)
{
System.out.println(e);
Log.e("ViewImage Error1",e.getMessage());
}
Iterator<String> itr = arl.listIterator();
int z=0,x=0,increment=0;
while (itr.hasNext())
{
id = itr.next();
img = img+id;
z++;
}
}
public class ImageAdapter extends BaseAdapter
{
private Context myContext;
private String[] myRemoteImages = {id};
public ImageAdapter(Context c)
{
this.myContext = c;
}
}
Either i am getting only the first image stored in that url or else i am not getting any other images
Following is the link from which i am trying to get the images
http://94.23.207.167/websiteIphone/Admin/XML/Santa.xml
Your code is not complete, so it's a bit difficult to give a definite answer but in the while loop at the end if imageXMLfn
you're assigning id = it.next()
. The image adapter than uses only id
which is set to the last value in the iterator. Supposing that arl
is a field in your class of type ArrayList<String>
it probably can be solved by:
public class ImageAdapter extends BaseAdapter
{
private Context myContext;
private String[] myRemoteImages = arl.toArray(new String[arl.size()]);
public ImageAdapter(Context c)
{
this.myContext = c;
}
}
However, your code is really incomplete in this regard, so I'm not sure whether this will work out.
What I found out mostly to this same problem is this. Lazy Load your images in the background. When in onpostexecute, don't display any images at all, due to the fact that when flinging the screen the current display then goes nuts like a multimedia slideshpw where your images blink in and out and may get updated incorrectly. Then I found that if you do a Fling detection on the gridview variable such as:
mPhotoView.setOnScrollListener(new OnScrollListener() {
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if(scrollState != SCROLL_STATE_IDLE) {
mAdapter.setFlinging(true);
} else {
mAdapter.setFlinging(false);
}
int first = view.getFirstVisiblePosition();
int count = view.getChildCount();
if (scrollState == SCROLL_STATE_IDLE || (first + count >
mAdapter.getCount()) ) {
mPhotoView.invalidateViews();
}
}
});
the invalidateViews will cause the newly downloaded images to be refreshed. Also, on the call to the Lazy background loader, right after the process is set in motion I load a placeholder bitmap. I only now need to find a way to get a few of the leftover placeholder images that somehow find its way through on some of the scrolls to be updated when the images are available. Lazy Loaders are all over the Internet and think its a far better way to load remote images than you code above. This is one of the trickiest things Androider's try to accomplish. Took me as long as all the rest of the "gotchas" I've faced. Hope this clears things up for you. Happy coding.
精彩评论