我是Android开发的新手.
考虑到JSON Carrier与XML相比的轻巧性,我纯粹喜欢使用JSON Objects和Arrays作为我的简单应用程序.
我对ArrayAdapter提出了挑战,要求填充ListView.
这就是我如何克服并需要你的建议.
Extend the Adaptor class.
然后将JSONArray传递给构造函数.
这里构造函数使用设置JSONArray长度的虚拟 String数组调用super .
将构造函数参数存储在类中以供进一步使用.
public myAdaptor(Context context, int resource, JSONArray array) { super(context, resource, new String[array.length()]); // Store in the local varialbles to the adapter class. this.context = context; this.resource = resource; this.profiles = objects; }
该getView()会做的工作获取从JSONArray一个JSONObjects建立视图.
public View getView(int position, View convertView, ViewGroup parent) { View view; if (convertView == null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(resource, parent, false); } else { view = convertView; } // Here JSONObject item = (JSONObject) profiles.getJSONObject(position); // READY WITH JSONObject from the JSONArray // YOUR CODE TO BUILD VIEW OR ACCESS THE }
现在任何改进/建议/尽管问题?