我是 Android 开发新手。
考虑到 JSON Carrier 相对于 XML 的轻便性,我纯粹喜欢在我的简单应用程序中使用 JSON 对象和数组。
我在使用 ArrayAdapter 填充 ListView 时遇到了挑战。
这就是我克服的方法,需要你的建议。
Extend the Adaptor class.
然后将 JSONArray 传递给构造函数。
这里构造函数用 调用 superdummy 设置 JSONArray 长度的字符串数组。
将构造函数参数存储在类中以供进一步使用。
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;
}
The 获取视图()将完成以下工作fetching来自 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
}
现在有任何改进/建议/深思熟虑的问题吗?