【发布时间】:2017-03-18 09:20:02
【问题描述】:
我必须读取 json 文件,有两个字段:name 和 id_name。我创建了一个数组列表。用户只能看到名称。当用户单击名称时,我必须获取 id。我只向您展示 AssyncTask doInBackground 过程的一部分。另外我用两个参数 name 和 id 创建了 class Name。
请帮助我。这是我的代码。
protected ArrayList<Name> doInBackground(String... params) {
ArrayList<Name> nameList = new ArrayList<>();
String result = "";
......
for (int i = 0; i < rootArray.length(); i++) {
JSONObject nameObject = rootArray.getJSONObject(i);
String name = nameObject.getString("name");
String id = nameObject.getString("id_name");
nameList.add(new NAME(name, id));
}
return nameList;
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String my_id = adapter.getItem(position).toString();
}
});
非常感谢。
【问题讨论】:
-
你试过我的答案了吗?
-
好的,谢谢。 Сan 我问你关于这个话题的另一个问题吗?我的应用程序的下一步是在 Json 文件中检索四个变量(在这种情况下它们都是字符),并将它们中的每一个传递给另一个意图。如何将四的值传递给 onPostExecute?正如我所理解的,我可以在 PostExecute 上执行意图吗?谢谢
标签: android json arraylist onclick onitemclicklistener