【问题标题】:To obtain data from Json从 Json 获取数据
【发布时间】:2015-05-18 10:41:23
【问题描述】:

我想从下面的一个Json中获取countryId、countryName的详细信息(请参考截图:http://i.imgur.com/DDs9rGT.png)。

这是我的代码:http://prntscr.com/76jbm4 这里: ArrayList companyName = new ArrayList(); AutoCompleteTextView country_editText; 此 AutoCompleteTextView 向用户显示国家名称列表,我想获取用户选择的国家/地区的 countryId。

问题是我无法传递包含 countryId 和 countryName 的 AutoCompleteTextView 列表。

【问题讨论】:

  • prntscr.com/76jbm4 这个链接有什么用?它不适合我
  • 你有什么错误吗?
  • 只传递CountyList并使用indexOf查找列表的id,而不是传递多个值。
  • prntscr.com/76jbm4 是我的程序的截图
  • @Muthu,我确实知道 indexOf 是什么...你能帮我解释一下吗

标签: android json arraylist autocompletetextview


【解决方案1】:

创建模型类

class country{  

private String countryName;  
private String countryPhonecode;  
private int countryId;  
private String countryCode;  

//getters and setters    
}  
List<Country> countryNameList=new ArrayList<Country>();  

for(int i=0;i<yourArray;i++){  
JSONObject json=array.getJSONObject(i);  
country c=new country();  
c.setcountryName(json.getString("countryName"));  
//do same with all parameter  

countryNameList.add(c);    
}  

and Now use indexOf to find id of the list like that.

list.get(position).getCountryId().   

希望对你有帮助。

【讨论】:

  • 感谢您的回答。根据我的经验,我没有使用过 indexOf 。你能解释一下我如何使用它吗
  • list.get(position).getCountryId()。在这里你能解释一下我如何获得位置的价值
  • @ParthibanM 当您单击列表中的任何项目时,它总是返回一个告诉您索引的位置。并且借助该索引,您可以获得国家/地区类对象。当您获得对象时,现在您无法借助国家/地区类的 getCountryId() 方法获取国家/地区ID。
  • 但在我的情况下,因为它是 autocompleteTextView,所以只要可编辑文本中有文本更改,位置就会发生变化。这不是问题吗?
猜你喜欢
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
  • 2018-09-20
  • 2011-12-27
  • 1970-01-01
  • 2018-06-04
  • 2019-01-19
  • 2015-04-04
相关资源
最近更新 更多