在android中存储长数据列表的最佳选择

时间:2011-12-20 03:00:45

标签: java android xml

我正在为我的大学的同学们编写一个应用程序,以便更轻松地使用公共交通工具。

它是一个xml解析器,它抓住下一班巴士将停在当前巴士站的时间。它在网上的布局如何     选择巴士> 06里士满     选择方向>南     选择停止> 141

然后我将使用字符串构建器来创建扩展ada.aspx?r=06&d=3&s=141来解析信息。

我的问题是,大约有2000个条目需要存储,为了提高效率我读到我应该使用XML,但是我如何关联06-Richmond以便当用户点击它时它存储06 in一个变量。 到目前为止,我已将它们存储在XML字符串数组中。

`     

<string-array name="bus_routes">

    <item>01 Kipps Lane / Thompson Road</item>
    <item>02 Dundas st</item>
    <item>03 Hailton Rd</item>
    <item>04 Oxford East</item>
    <item>05 Springbank</item>
    <item>06 Richmond</item>
    <item>07 Wavell</item>
    <item>08 Riverside</item>
    <item>09 Whitehills </item>
    <item>10 Wonderland</item>`    

如果我使用不同的存储方法,我的希望是当你点击里士满时,ID标签是06或类似的东西。我买了一本Apress书来学习,而且它还没有完成这项工作,所以如果有人有任何书,他们认为也是好书,那也很好。

由于

1 个答案:

答案 0 :(得分:1)

要么改变存储方法(比如数据库),要么保留XML方法然后自己创建一个方法,它将返回字符串/或数字:

public int getBusIndex(String myButtonText)
{
String[] bus = context.getResources().getStringArray(R.array.bus_routes);
    for(String s: bas)
    {
     if(s.contains(myButtonText))   //simple linear search
         return Integer.parseInt(s);
    }

return -1;// if the search is not successfull (also try to implement more faster searches, see Collections class)
}

你打电话:

int busIndex = getBusIndex(mButton.getText()); // supposing we are dealing with button

但是如果你还打算写(在你的字符串数组(xml)中动态新的东西你肯定需要改变方法。

My personal opinion and choice: Writing simple query against database will cut on memory resources for loading all the string entries. Since you will have ID column (of integer type) which will do the work of 06 stuff.