package com.readystatesoftware.android;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.TextView;
/**
* An easy adapter to map JSON data to views defined in an XML layout. Basically a wrapper around
* {@link android.widget.SimpleAdapter} that maps data from a {@link org.json.JSONArray} instance.
*
* @author jgilfelt
*/
public class JSONArrayAdapter extends BaseAdapter implements Filterable {
private JSONArray data;
private String idField;
private InternalSimpleAdapter internalAdapter;
/**
* Constructor
*
* @param context The context where the View associated with this SimpleAdapter is running
*
* @param data A JSONArray of JSONObjects. Each object in the array corresponds to one row in the list. The
* JSONObjects contain the data for each row, and should include all the entries specified in
* "from"
*
* @param resource Resource identifier of a view layout that defines the views for this list
* item. The layout file should include at least those named views defined in "to"
*
* @param from A list of field names that will be mapped from the JSONObject associated with each
* item.
*
* @param to The views that should display column in the "from" parameter. These should all be
* TextViews. The first N views in this list are given the values of the first N columns
* in the from parameter.
*
* @param idField The field name which contains a numeric identifier for each JSONObject.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public JSONArrayAdapter(Context context,
JSONArray data,
int resource,
String[] from,
int[] to,
String idField) {
this.data = data;
this.idField = idField;
List simpleData = jsonToMapList(data, from);
internalAdapter = new InternalSimpleAdapter(context, simpleData, resource, from, to);
}
/**
* Constructor
*
* @param context The context where the View associated with this SimpleAdapter is running
*
* @param data A JSONArray of JSONObjects. Each object in the array corresponds to one row in the list. The
* JSONObjects contain the data for each row, and should include all the entries specified in
* "from"
*
* @param resource Resource identifier of a view layout that defines the views for this list
* item. The layout file should include at least those named views defined in "to"
*
* @param from A list of field names that will be mapped from the JSONObject associated with each
* item.
*
* @param to The views that should display column in the "from" parameter. These should all be
* TextViews. The first N views in this list are given the values of the first N columns
* in the from parameter.
*/
public JSONArrayAdapter(Context context,
JSONArray data,
int resource,
String[] from,
int[] to) {
this(context, data, resource, from, to, null);
}
@SuppressWarnings("rawtypes")
private List jsonToMapList(JSONArray data, String[] fields) {
List<Map> listData = new ArrayList<Map>();
HashMap<String, String> item;
for (int i = 0; i < data.length(); i++) {
JSONObject o = data.optJSONObject(i);
if (o != null) {
item = new HashMap<String, String>();
for (int j = 0; j < fields.length; j++) {
String fname = fields[j];
item.put(fname, optString(o, fname));
}
listData.add(item);
}
}
return listData;
}
private String optString(JSONObject o, String key) {
//iterate over child json keys if we need to
String[] keys = key.split("\\.");
for (int i = 0; i < keys.length - 1; i++)
o = o.optJSONObject(keys[i]);
String lastKey = keys[keys.length - 1];
String s = o.optString(lastKey);
if (s == null || s.equals("null")) {
return null;
} else {
return s;
}
}
public SimpleAdapter unwrap() {
return internalAdapter;
}
public int getCount() {
return internalAdapter.getCount();
}
public Object getItem(int position) {
return data.optJSONObject(position);
}
public long getItemId(int position) {
if (idField != null) {
JSONObject o = data.optJSONObject(position);
if (o != null) {
return o.optLong(idField, 0);
}
}
return internalAdapter.getItemId(position);
}
public View getView(int position, View convertView, ViewGroup parent) {
return internalAdapter.getView(position, convertView, parent);
}
/**
* Returns the {@link ViewBinder} used to bind data to views.
*
* @return a ViewBinder or null if the binder does not exist
*
* @see #setViewBinder(android.widget.SimpleAdapter.ViewBinder)
*/
public ViewBinder getViewBinder() {
return internalAdapter.getViewBinder();
}
/**
* Sets the binder used to bind data to views.
*
* @param viewBinder
* the binder used to bind data to views, can be null to remove
* the existing binder
*
* @see #getViewBinder()
*/
public void setViewBinder(ViewBinder viewBinder) {
internalAdapter.setViewBinder(viewBinder);
}
/**
* Called by bindView() to set the image for an ImageView but only if there
* is no existing ViewBinder or if the existing ViewBinder cannot handle
* binding to an ImageView.
*
* This method is called instead of {@link #setViewImage(ImageView, String)}
* if the supplied data is an int or Integer.
*
* @param v
* ImageView to receive an image
* @param value
* the value retrieved from the data set
*
* @see #setViewImage(ImageView, String)
*/
public void setViewImage(ImageView v, int value) {
v.setImageResource(value);
}
/**
* Called by bindView() to set the image for an ImageView but only if there
* is no existing ViewBinder or if the existing ViewBinder cannot handle
* binding to an ImageView.
*
* By default, the value will be treated as an image resource. If the value
* cannot be used as an image resource, the value is used as an image Uri.
*
* This method is called instead of {@link #setViewImage(ImageView, int)} if
* the supplied data is not an int or Integer.
*
* @param v
* ImageView to receive an image
* @param value
* the value retrieved from the data set
*
* @see #setViewImage(ImageView, int)
*/
public void setViewImage(ImageView v, String value) {
try {
v.setImageResource(Integer.parseInt(value));
} catch (NumberFormatException nfe) {
v.setImageURI(Uri.parse(value));
}
}
/**
* Called by bindView() to set the text for a TextView but only if there is
* no existing ViewBinder or if the existing ViewBinder cannot handle
* binding to an TextView.
*
* @param v
* TextView to receive text
* @param text
* the text to be set for the TextView
*/
public void setViewText(TextView v, String text) {
v.setText(text);
}
public Filter getFilter() {
return internalAdapter.getFilter();
}
class InternalSimpleAdapter extends SimpleAdapter {
public InternalSimpleAdapter(Context context,
List<? extends Map<String, ?>> data, int resource,
android-jsonarrayadapter最新源代码
需积分: 0 186 浏览量
更新于2023-06-16
收藏 22KB ZIP 举报
android-jsonarrayadapter最新源代码android-jsonarrayadapter最新源代码android-jsonarrayadapter最新源代码android-jsonarrayadapter最新源代码android-jsonarrayadapter最新源代码android-jsonarrayadapter最新源代码android-jsonarrayadapter最新源代码android-jsonarrayadapter最新源代码android-jsonarrayadapter最新源代码android-jsonarrayadapter最新源代码android-jsonarrayadapter最新源代码android-jsonarrayadapter最新源代码android-jsonarrayadapter最新源代码android-jsonarrayadapter最新源代码android-jsonarrayadapter最新源代码android-jsonarrayadapter最新源代码android-jsonarrayadapter最新源代码android

Knowledgebase
- 粉丝: 262
最新资源
- 基于财务共享对促进管理会计信息化建设分析(1).docx
- 大数据时代数字化人事档案管理信息化建设策略探究(1).docx
- 关于建筑施工企业财务管理信息化建设探究(1).docx
- 城市信息化与房地产周期波动关系研究(1).docx
- 移动互联网销售人员培训手册(1).pptx
- 学生信息管理系统--C语言课程设计(1).doc
- 2023年编译原理平时作业答案(1).doc
- 通信线路施工安全规定.docx
- 2023年会计从业考试会计电算化会计软件功能模块考试题库(1).docx
- 人工智能头部公司纷纷融资-行业或迎大洗牌期(1).docx
- 新时期中小企业电子商务新战略探究论文(1).docx
- 计算机心得体会范文(1).doc
- 金融计量经济第五讲虚拟变量模型和ProbitLogit模型(1).pptx
- 老年人信息化管理(1).docx
- 2022-2024年CAD(计算机辅助设计)行业现状与投资分析报告(1).pdf
- 大数据智慧城市互联网金融内容营销案例推广活动(1).pptx