[英]Best approach to save unknown quantity of data?
Ok, here I have a quite challenging problem (just to me, of course). 好的,这是一个非常具有挑战性的问题(当然,对我而言)。
I'm retrieving some data from a MySql db via web services in order to fill an spinner in my app. 我正在通过Web服务从MySql数据库中检索一些数据,以填充应用程序中的微调框。 This spinner shows cities, and this cities should change depending on another spinner's state (which contains countries)
此微调器显示城市,并且该城市应根据另一个微调器的州(包含国家/地区)进行更改
No problem so far. 到目前为止没有问题。 I can retrieve the data, and the spinner changes as it should.
我可以检索数据,微调器会按需更改。 My problem is that I don't want my app to connect again and again to retrieve cities information any time the user selects another item from Countries.
我的问题是,我不希望用户每次从“国家/地区”选择其他项目时,我的应用程序一次又一次地连接以检索城市信息。
And here is my real problem. 这是我真正的问题。 Ideally I would like to save cities data inside one String array for each country, but considering I don't know how many countries will be loaded from my database my question is: how could I achieve that?
理想情况下,我想将每个国家/地区的城市数据保存在一个String数组中,但是考虑到我不知道将从数据库中加载多少个国家,我的问题是:如何实现?
I was thinking that it would be possible by creating a multidimensional array, but I don't know how to create an "array of arrays". 我当时以为可以通过创建多维数组来实现,但是我不知道如何创建“数组数组”。
Any ideas? 有任何想法吗?
Thank you very much in advance. 提前非常感谢您。
create a proper data structure. 创建适当的数据结构。 And check if it is null then reload data again else perform regular operation.
并检查是否为空,然后再次重新加载数据,否则执行常规操作。 eg refer following getter setter class
例如参考下面的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;
}
} }
Calling the web service you can store into the DB each country as a JSON string, containing the whole list of cities, for example like this : 调用Web服务,您可以将每个国家/地区作为JSON字符串存储到数据库中,其中包含城市的整个列表,例如:
{"id": "1", "name": "coutry1", "cities": [{"id": "1", "name": "city1"},{"id": "2", "name": "city2"},{"id": "3", "name": "cityx"}]}
Later making your request on SQLite you can user this JSONObject to create instances of Country 稍后在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;
}
}
Then you can use the instances of Country in any kind of adapter for spinners, list, or expandable list.... 然后,您可以在微调器,列表或可扩展列表的任何适配器中使用Country的实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.