用网络中的图像形成gridview的问题

发布于 2024-11-07 21:33:14 字数 2622 浏览 0 评论 0原文

在我的应用程序中,我试图从存储在 web.xml 中的 xml 文件形成网格视图。

以下是我的代码

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);

            Document 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; 
        }
}

要么我只获取存储在该网址中的第一个图像,要么我没有获取任何其他图像

以下是我试图从中获取图像的链接

http://94.23.207.167/websiteIphone/Admin/XML/Santa.xml

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);

            Document 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮年慕年 2024-11-14 21:33:14

您的代码不完整,因此给出明确的答案有点困难,但在最后的 while 循环中,如果您要分配 imageXMLfn id = it.next()。图像适配器仅使用设置为迭代器中最后一个值的id。假设 arl 是您的 ArrayList 类型的类中的一个字段,它可能可以通过以下方式解决:

public class ImageAdapter extends BaseAdapter 
{
    private Context myContext;

    private String[] myRemoteImages = arl.toArray(new String[arl.size()]);

    public ImageAdapter(Context c)
    { 
        this.myContext = c; 
    }
}

但是,您的代码在这方面确实不完整,所以我'我不确定这是否会成功。

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.

め可乐爱微笑 2024-11-14 21:33:14

我对同一问题的主要发现是这样的。在后台延迟加载图像。在 onpostexecute 中时,根本不显示任何图像,因为当滑动屏幕时,当前显示会变得像多媒体幻灯片一样疯狂,图像会闪烁进出,并且可能会错误更新。然后我发现如果对gridview变量做Fling检测如:

                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(); 
            }

        } 
    }); 

invalidateViews会导致新下载的图片被刷新。另外,在调用惰性后台加载程序时,在进程启动后,我立即加载占位符位图。我现在只需要找到一种方法来获取一些剩余的占位符图像,这些图像以某种方式在某些卷轴上找到方法,以便在图像可用时进行更新。惰性加载器遍布互联网,并且认为它是比上面的代码更好的加载远程图像的方法。这是 Android 开发者试图完成的最棘手的事情之一。我花了和我遇到的所有其他“陷阱”一样长的时间。希望这能为您解决问题。快乐编码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文