我应该使用的东西比的getResource()等。getStringArray()填充一个大阵? [英] Should I be using something other than getResource().getStringArray() to populate a large array?

查看:80
本文介绍了我应该使用的东西比的getResource()等。getStringArray()填充一个大阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继Android的样品填充一个ListView ,我查询从我的的strings.xml 的使用数组<一href="http://developer.android.com/reference/android/content/res/Resources.html#getStringArray%28int%29"相对=nofollow> Activity.getResource()getStringArray():

Following the Android sample for populating a ListView, I query an array from my strings.xml using Activity.getResource().getStringArray():

String [] mDefinitions = getResources().getStringArray(R.array.definition_array);

这方面的文档的方法是pretty的清楚的,我没想到会遇到任何问题与它:

The documentation for this method is pretty clear and I didn't expect to encounter any problems with it:

返回与特定的资源ID相关联的字符串数组。

Return the string array associated with a particular resource ID.

该方法按照预期的小数据集。然而,当我居住的的strings.xml 的使用完整的数据集(接近2000项),我发现,该应用程序崩溃时,它试图加载资源。我注意到,在控制台日志此错误:

This approach worked as expected for small data sets. However, when I populated strings.xml with the full data set (close to 2000 entries), I find that the app crashed when it tried to load the resource. I noticed this error in the console log:

ReferenceTable溢出(最大值= 512)

ReferenceTable overflow (max=512)

与项目的数量打在我的字符串数组的我证实,该错误是可重复的,当项目的数量超过〜700项。

Playing around with the number of items in my string-array I confirmed that the error was reproducible when the number of items exceeded ~700 entries.

谷歌搜索的问题已经变成了其他开发商一些例子<一href="http://www.listware.net/201005/android-developers/4451-android-developers-max-size-of-string-array.html"相对=nofollow>有同样的问题,但我找不到任何Android的文档中解释。

Googling the problem has turned up some examples of other developers having the same problem, but I can't find anything in the Android documentation to explain it.

有人去创造一个问题的麻烦问题的Andr​​oid的谷歌code页面上,但没有它,或者我碰到的帖子,得到了满意的答案。

Someone has gone to the trouble of creating an issue for the problem on the Android Google Code page but neither it, or any of the posts I came across, received a satisfactory answer.

我是不是接近问题的错误的方式?我应该填充自己的数据(文件加载和解析JSON或类似)和回避的问题完全?这感觉就像我失去了一些东西很明显这里。

Am I approaching the problem the wrong way? Should I be populating the data myself (loading a file and parsing JSON or similar) and avoid the issue entirely? It feels like I am missing something obvious here.

推荐答案

我需要能够解析大型XML文件,我的特定应用(我已经有数据连接codeD的方式,我想保持一致的)。

I need to be able to parse large XML files for my particular application (I already had data encoded that way and I wanted to keep it consistant).

所以,我的第一次尝试是使用的 getStringArray 的,其遭受的提到的问题:

So my first attempt was to use getStringArray, which suffers from the problem described in the question:

String [] mDefinitions = getResources().getStringArray(R.array.definition_array);

我的第二次​​尝试患有我encounted用的 getStringArray 的同样的限制。当我试着处理一个更大的XML文件(> 500K),我有一个DalvikVM失事的的getXML 的:

My second attempt suffers from the same limitation that I encounted with getStringArray. As soon as I tried processing a larger XML file (> 500K), I got a DalvikVM crash on getXml:

 XmlResourceParser parser = getResources().getXml(R.xml.index);

 try {
     int eventType = parser.getEventType();

     while (eventType != XmlPullParser.END_DOCUMENT) {
         String name = null;

         switch (eventType){
             case XmlPullParser.START_TAG:
                 // handle open tags
                 break;
             case XmlPullParser.END_TAG:
                 // handle close tags
                 break;
         }

         eventType = parser.next();
     }
 }
 catch (XmlPullParserException e) {
     throw new RuntimeException("Cannot parse XML");
 }
 catch (IOException e) {
     throw new RuntimeException("Cannot parse XML");
 }
 finally {
     parser.close();
 }

我的最终解决方案,它使用SAX解析器上​​的的InputStream 的从原始资源的作品产生。我可以解析大型XML文件,而DalvikVM崩溃:

My final solution, which uses the SAX parser on a InputStream generated from the raw resource works. I can parse large XML files without the DalvikVM crashing:

 InputStream is = getResources().openRawResource(R.raw.index);
 XmlHandler myXMLHandler = new XmlHandler();

 SAXParserFactory spf = SAXParserFactory.newInstance();
 SAXParser sp = spf.newSAXParser();
 XMLReader xr = sp.getXMLReader();

 xr.setContentHandler(myXMLHandler);
 xr.parse(new InputSource (is));

 } catch (Exception e) {
     System.out.println("XML Pasing Excpetion = " + e);
 }

其中XmlHandler是:

Where XmlHandler is:

 public class XmlHandler extends DefaultHandler {

     @Override
     public void startElement(String uri, String localName, String qName,
     Attributes attributes) throws SAXException {
     // handle elements open
     }

     @Override
     public void endElement(String uri, String localName, String qName)
     throws SAXException {  
     // handle element close
     }

     @Override
     public void characters(char[] ch, int start, int length)
     throws SAXException {
     // handle tag characters <blah>stuff</blah>
     }

 }

这篇关于我应该使用的东西比的getResource()等。getStringArray()填充一个大阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆