ArrayAdapter - 拆分 2 个字符串

ArrayAdapter - Split 2 strings

[已解决] 我在这里需要一点帮助。 我有一个包含 2 个字符串的 ArrayAdapter,我想向用户显示 ArrayAdapter 的第一部分,当用户单击特定项目时,我想获取该特定项目的 ID 和 post 它。 在下图中,您可以看到我想向用户显示 JAVA 并希望在用户单击按钮时发送 JAVAs ID。

代码:

 private class getSkillsList extends AsyncTask<String, Void, Void> {

    private String dataFetchedFromURL = "";

    @Override
    protected Void doInBackground(String... skillListURL) {

        try {
            String newUrl = skillListURL[0];
            URL url = new URL(newUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while (line != null) {
                line = bufferedReader.readLine();
                dataFetchedFromURL = dataFetchedFromURL + line;
            }
            System.out.print("DATA: " + dataFetchedFromURL);
            JSONArray root = new JSONArray(dataFetchedFromURL);
            skillList = new ArrayList<String>();
            for (int i = 0; i < root.length(); i++) {
                JSONObject jsonObjectHoldingCountries = root.getJSONObject(i);
HashMap<String, Integer> skillsMap = new HashMap<>();                   
skillsMap.put(jsonObjectHoldingCountries.optString("lookupValue"), jsonObjectHoldingCountries.getInt("lookupValueId"));
            }
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
ArrayAdapter<String> adapter;
        for(int i=0; i<skillsMap.size();i++){
            skillList.add(String.valueOf(skillsMap.get(i)));
        }
            adapter = new ArrayAdapter<String>(Skills.this, android.R.layout.simple_list_item_1, skillList);
        acSkillTextView.setAdapter(adapter);
    }
}

1 条更新:

现在我已经更新了我的代码并将 JSON 响应存储在 HashMap 中,第一个键是字符串,值是整数。 当我尝试在 post 执行方法中的适配器中添加那些 Hashmap 时,它在 autocompletetext 下拉列表中显示 NULL。

2 更新:

我得到了我的解决方案,我首先将所有接收到的数据插入到一个 HashMap 中,然后我从 hashmap 中将键值插入到一个数组中,并使用适配器在下拉列表中显示该数组。干杯!谢谢你们:)

尝试使用 name 和 id 的 hashmap 以及仅使用 name 的数组。根据位置,你可以从 hashmap 中获取你的 id。

我给你解释一下。

 hashmap.put("java","350");
 hashmap.put("c++","351");

现在制作一个数据数组

array.add("java");
array.add("c++");

现在选择1号位置,可以得到相应的id