来自 json 个原始文件的微调器

Spinner from json raw file

我有一个像这样的 json 原始文件

{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}}
{"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}
{"_id":1283378,"name":"Gorkhā","country":"NP","coord":{"lon":84.633331,"lat":28}}
{"_id":1270260,"name":"State of Haryāna","country":"IN","coord":{"lon":76,"lat":29}}
{"_id":708546,"name":"Holubynka","country":"UA","coord":{"lon":33.900002,"lat":44.599998}}
{"_id":1283710,"name":"Bāgmatī Zone","country":"NP","coord":{"lon":85.416664,"lat":28}}
{"_id":529334,"name":"Mar’ina Roshcha","country":"RU","coord":{"lon":37.611111,"lat":55.796391}}
{"_id":1269750,"name":"Republic of India","country":"IN","coord":{"lon":77,"lat":20}}
{"_id":1283240,"name":"Kathmandu","country":"NP","coord":{"lon":85.316666,"lat":27.716667}}
{"_id":703363,"name":"Laspi","country":"UA","coord":{"lon":33.733334,"lat":44.416668}}

它不是一个 JSON 数组 - 它是一个巨大的 JSON 对象列表。这里 Populate the spinner from JSON In Android, here here android how to convert json array to string array or even here https://developer.android.com/guide/topics/ui/controls/spinner.html 假定我有一个 JSON 数组或静态数据。我想要两个微调器,一个用于乡村,第二个用于城市。假设我有国家列表,所以它将是一个静态数据。我希望第二个微调器从原始 json 文件动态加载与第一个微调器中选定城市相对应的所有城市。我认为我可以处理微调器的事情,但是如何将原始 json 文件中的数据加载到微调器中?我是否需要修改这个原始文件,使其成为一个 json 数组,然后执行如下操作:

JSONArray jsonArray = new JSONArray(arraytitle);
List<String> list = new ArrayList<String());
for(int i = 0 ; i < jsonArray.length();i++){
list.add(jsonArray.getJSONObject(i));
}

编辑 我找到了这个答案 - 如果我能成功,我会 post 一个答案

所以你有两个问题:

如何在更新第一个 Spinner 时动态更新第二个 Spinner

您需要做三件事:

  1. 实施AdapterView.OnItemSelectedListener
  2. 更新第二个 Spinner 的适配器内的备份数据集。在您的情况下,它将是一个城市列表。 (为此你需要创建你自己的从 ArrayAdapter 扩展的适配器)
  3. 致电adapter.notifyDataSetChanged()

如何使用JSON

那里有几种解决方案。但是其他人很难在不了解要求的任何细节的情况下提供最适合您的案例。

当你遇到设计问题时,请记住一件事:

Do the simplest thing first.