【问题标题】:Best approach to save unknown quantity of data?保存未知数量数据的最佳方法?
【发布时间】:2014-07-26 05:24:53
【问题描述】:

好的,我有一个非常具有挑战性的问题(当然是对我而言)。

我正在通过 Web 服务从 MySql 数据库中检索一些数据,以便在我的应用程序中填充微调器。此微调器显示城市,并且此城市应根据另一个微调器的状态(包含国家/地区)而改变

目前没有问题。我可以检索数据,并且微调器会发生应有的变化。我的问题是我不希望我的应用程序在用户从国家/地区选择另一个项目时一次又一次地连接以检索城市信息。

这是我真正的问题。理想情况下,我想将每个国家/地区的城市数据保存在一个字符串数组中,但考虑到我不知道从我的数据库中会加载多少个国家/地区,我的问题是:我该如何实现?

我在想可以通过创建一个多维数组来实现,但我不知道如何创建一个“数组数组”。

有什么想法吗?

非常感谢您。

【问题讨论】:

  • 您需要缓存 http 响应。 github.com/stephanenicolas/robospice 可能会帮助你
  • 谢谢,这可能有用,我一定会去看看,但这次我想我会走 chet 的路。我喜欢让事情尽可能简单。

标签: java android mysql arrays multidimensional-array


【解决方案1】:

创建适当的数据结构。并检查它是否为空,然后再次重新加载数据,否则执行常规操作。例如参考下面的 getter setter 类

public class SpinnerInfoData {
String city="",state="",countries="";

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getState() {
    return state;
}

public void setState(String state) {
    this.state = state;
}

public String getCountries() {
    return countries;
}

public void setCountries(String countries) {
    this.countries = countries;
}

}

【讨论】:

  • 是的,我可以想到一些涉及此建议的解决方案。非常感谢。
【解决方案2】:

调用网络服务,您可以将每个国家/地区作为 JSON 字符串存储到数据库中,其中包含整个城市列表,例如:

{"id": "1", "name": "coutry1", "cities": [{"id": "1", "name": "city1"},{"id": "2", "name": "city2"},{"id": "3", "name": "cityx"}]}

稍后在 SQLite 上提出您的请求,您可以使用此 JSONObject 创建 Country 实例

    public class Country extends BaseElement{
        private ArrayList<City> cities;

        // Imagine that for each country we are storing the kind of JSON into the DB
        // {"id": "1", "name": "coutry1", "cities": [{"id": "1", "name": "city1"},{"id": "2", "name": "city2"},{"id": "3", "name": "cityx"}]}
        public Country(String jsonFromDB){
            cities = new ArrayList<City>();
            if(jsonFromDB != null && jsonFromDB.trim().length() > 0){
                try {
                    JSONObject country = new JSONObject(jsonFromDB);
                    setId(country.getInt("id"));
                    setName(country.getString("name"));
                    JSONArray cts = country.getJSONArray("cities");
                    for(int i = 0; i < cts.length() ; i++){
                        cities.add(new City(cts.getJSONObject(i)));
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        public ArrayList<City> getCities(){ 
            return cities;
        }
    }

    public class City extends BaseElement{

        public City(JSONObject jsonFromDB) throws JSONException{
            if(jsonFromDB != null){
                setId(jsonFromDB.getInt("id"));
                setName(jsonFromDB.getString("name"));
            }
        }
    }

    public class BaseElement{
        private int id;
        private String name;

        public int getId() {
            return id;
        }
        public String getName() {
            return name;
        }
        public void setId(int id) {
            this.id = id;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

然后,您可以在任何类型的适配器中使用 Country 的实例,用于微调器、列表或可扩展列表......

【讨论】:

  • 这是一个很好的选择。我从来没有想过将 JSON 直接存储到数据库中。我相信这种方法将来会对我有所帮助,但目前,由于当前接受的答案,我已经想出了一个解决方案。但我真的很感谢你的帮助。不是每天都有人能有这么详细的解释。
猜你喜欢
  • 2013-01-08
  • 1970-01-01
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多