【问题标题】:Populate from specific field from the array从数组中的特定字段填充
【发布时间】:2017-06-02 21:41:57
【问题描述】:

我正在尝试将数据从数组检索到文本字段,它位于列表视图中,现在我有这个

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

它正在工作:

问题是,我只想要“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.class:

@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());
}

【问题讨论】:

  • 在你的 POJO 类中添加完整的代码,建议覆盖 toString
  • @Pavneet_Singh 我已经添加了完整的代码,或者大部分代码,看看你能不能帮忙,我想要的是将TextView txtDesignacao 设置为数组中Designacao 字段的内容跨度>
  • Object todasAsCategorias 应该是 HashMap&lt;String, String&gt; todasAsCategorias 然后是 txtDesignacao.setText(position + 1 + todasAsCategorias.get("Designacao")); ,愿原力与你同在

标签: java android arrays populate


【解决方案1】:

你有 todasAsCategorias 作为一个对象。如果它是公共财产,只需 todastAsCate.Designacao,或者 todastAsCate.getDesignacao()。您所要做的就是检查您的 todasAsCategorias 对象

【讨论】:

    猜你喜欢
    • 2019-05-04
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    • 1970-01-01
    • 2020-12-01
    • 2013-11-03
    • 2019-04-26
    相关资源
    最近更新 更多