java - 从数组中的特定字段填充

标签 java android arrays populate

我正在尝试将数据从数组检索到文本字段,该文本字段位于 ListView 中,现在我有这个

txtDesignacao.setText(position + 1 + todasAsCategorias.toString());

它正在工作:

enter image description here

问题是,我只想要“Designacao”字段,在本例中为“Azeites & Outros”,我怎样才能实现这一点?

##########################编辑

TodasAsCategorias.class:

@Override
    protected Void doInBackground(Void... params) {
        HttpHandler sh = new HttpHandler();
        String url1 = "something";
        String url2 = "something2";

        for (int idCat = 0; idCat <100; idCat++){
            final String jsonStr = sh.makeServiceCall(url1 + idCat + url2);
            if (jsonStr != null) {
                try {
                    JSONObject jsonObject = new JSONObject(jsonStr);
                    String K_PRODUTO = jsonObject.getString("K_PRODUTO");
                    String Designacao = jsonObject.getString("Designacao");
                    HashMap<String, String> cat = new HashMap<>();
                    cat.put("K_PRODUTO", K_PRODUTO);
                    cat.put("Designacao", Designacao);
                    listaCategorias.add(cat);
                } catch (final JSONException e) {
                    //DO SOMETHING
                }
            } else {
                //DO SOMETHING
            }
        }
        return null;
    }

@Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        mAdapter = new ListViewAdapter(mContext);
        lv.setAdapter(mAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //Do Something
            }
        });
    }
}

public static ArrayList<HashMap<String, String>> getListaCategorias() {
    return listaCategorias;
}

ListViewAdapter.类:

@Override
public void fillValues(int position, View convertView) {
    ArrayList array = TodasAsCategorias.getListaCategorias();
    Object todasAsCategorias = array.get(i);
    TextView txtDesignacao = (TextView) convertView.findViewById(R.id.Designacao);
    txtDesignacao.setText(position + 1 + todasAsCategorias.toString());
}

最佳答案

您已将 todasAsCategorias 作为对象。如果它是公共(public)属性(property),则只需 todastAsCate.Designacao,或 todastAsCate.getDesignacao()。您所要做的就是检查您的 todasAsCategorias 对象

关于java - 从数组中的特定字段填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44327643/

相关文章:

Android通话记录日期转换

php将json数组合并为一个数组

javascript - JSON数组迭代: How to apply a function on each element of a json array in javascript?

java - 从随机生成的中提取特定数字

java - Android - Arraylist 在 for 循环外返回 null

Android程序连接Facebook : LoginActivity has leaked window

php - 检查 $var 是整数还是数组

java - 如何处理 spring jpa @transactional 和 catch block 中的新插入

java - Eclipse .classpath 位置

java - 下载具有特定内容类型的文件