我坚持了一点,我有一个简单的 mysql 数据库,其中包含名称和地址表,我能够获取相同的 json 数组,但无法填充列表视图。请帮我
问问题
459 次
1 回答
0
假设您已经知道如何使用控件和使用 adpater,那么您需要通过扩展、然后覆盖、、和方法ListView
来创建自定义适配器。BaseAdapter
getCount()
getItem()
getItemId()
getView()
例如,您的 BaseAdapter 实现可能需要一个JSONArray
并且可能是这样的......
public class JSONAdapter extends BaseAdapter {
private JSONArray array = null;
public JSONAdapter(JSONArray data) {
this.array=data;
}
public int getCount() {
return array.length();
}
public Object getItem(int position) {
return array.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View currentView, ViewGroup parent) {
// get the current item from the json array
JSONObject o = getItem(position);
// create a new view
View v;
if (convertView == null || newItemView) {
v = inflater.inflate(YOUR_ITEM_LAYOUT_VIEW_ID, parent, false);
} else {
v = convertView;
}
// populate the view
// use v.findViewById() to locate your layout items and then
// set a value from the JSONObject that you obtained above
return view;
}
}
上面的代码可能无法直接编译(并且不包含任何错误检查),因为我只是在这里内联输入它......但希望它能让您了解如何创建一个ListAdapter
可以接受JSONArray
.
于 2012-05-26T12:58:36.610 回答