public abstract class NFJSONArrayAdapter extends BaseAdapter
A non-filterable custom abstract BaseAdapter
that is backed by a JSONArray
of
arbitrary objects. By default this class delegates view generation to subclasses.
Designed to be a flexible and customizable solution for using JSONArray with an adapter but
without the filtering mechanism. As a result, there is no need for synchronized
blocks
which may help those worried about performance. It exposes most of the JSONArray methods and
conveniently passes along a layout inflater for view creation. Keep in mind JSONArray itself has
limited capabilities which restricts what this adapter can do.
If filtering is required, it's strongly recommended to use the JSONAdapter
instead.
IGNORE_ITEM_VIEW_TYPE, NO_SELECTION
Constructor and Description |
---|
NFJSONArrayAdapter(Context activity)
Constructor
|
NFJSONArrayAdapter(Context activity,
Collection items)
Constructor
|
NFJSONArrayAdapter(Context activity,
JSONArray array) |
NFJSONArrayAdapter(Context activity,
JSONTokener readFrom)
Constructor
|
NFJSONArrayAdapter(Context activity,
String json)
* Constructor
|
Modifier and Type | Method and Description |
---|---|
void |
add(boolean item)
Adds the specified items at the end of the adapter.
|
void |
add(double item)
Adds the specified items at the end of the adapter.
|
void |
add(int item)
Adds the specified items at the end of the adapter.
|
void |
add(long item)
Adds the specified items at the end of the adapter
|
void |
add(Object item)
Adds the specified items at the end of the adapter.
|
void |
addAll(JSONArray items)
Adds the specified JSONArray at the end of the adapter.
|
void |
clear()
Remove all elements from the adapter.
|
Context |
getContext() |
int |
getCount() |
View |
getDropDownView(int position,
View convertView,
ViewGroup parent) |
View |
getDropDownView(LayoutInflater inflater,
int position,
View convertView,
ViewGroup parent)
Get a
View that displays in the drop down popup the data at the
specified position in the data set. |
Object |
getItem(int position) |
boolean |
getItemBoolean(int position)
Gets the boolean data item associated with the specified position in the data set.
|
double |
getItemDouble(int position)
Gets the double data item associated with the specified position in the data set.
|
long |
getItemId(int position) |
int |
getItemInt(int position)
Gets the int data item associated with the specified position in the data set.
|
JSONArray |
getItemJSONArray(int position)
Gets the JSONArray data item associated with the specified position in the data set.
|
JSONObject |
getItemJSONObject(int position)
Gets the JSONObject data item associated with the specified position in the data set.
|
long |
getItemLong(int position)
Gets the long data item associated with the specified position in the data set.
|
String |
getItemString(int position)
Gets the String data item associated with the specified position in the data set.
|
JSONArray |
getJSONArray() |
View |
getView(int position,
View convertView,
ViewGroup parent) |
abstract View |
getView(LayoutInflater inflater,
int position,
View convertView,
ViewGroup parent)
Get a View that displays the data at the specified position in the data set.
|
boolean |
isNull(int position) |
void |
notifyDataSetChanged() |
Object |
optItem(int position)
Gets the data item associated with the specified position in the adapter or null if there is
no value at position.
|
boolean |
optItemBoolean(int position)
Gets the boolean data item associated with the specified position in the adapter or null if
there is no value at position.
|
boolean |
optItemBoolean(int position,
boolean fallback)
Gets the boolean data item associated with the specified position in the adapter or null if
there is no value at position.
|
double |
optItemDouble(int position)
Gets the double data item associated with the specified position in the adapter or null if
there is no value at position.
|
double |
optItemDouble(int position,
double fallback)
Gets the double data item associated with the specified position in the adapter or null if
there is no value at position.
|
int |
optItemInt(int position)
Gets the int data item associated with the specified position in the adapter or null if there
is no value at position.
|
int |
optItemInt(int position,
int fallback)
Gets the int data item associated with the specified position in the adapter or null if there
is no value at position.
|
JSONArray |
optItemJSONArray(int position)
Gets the JSONArray data item associated with the specified position in the adapter or null if
there is no value at position.
|
JSONObject |
optItemJSONObject(int position)
Gets the JSONObject data item associated with the specified position in the adapter or null
if there is no value at position.
|
long |
optItemLong(int position)
Gets the long data item associated with the specified position in the adapter or null if
there is no value at position.
|
long |
optItemLong(int position,
long fallback)
Gets the long data item associated with the specified position in the adapter or null if
there is no value at position.
|
String |
optItemString(int position)
Gets the String data item associated with the specified position in the adapter or null if
there is no value at position.
|
String |
optItemString(int position,
String fallback)
Gets the String data item associated with the specified position in the adapter or null if
there is no value at position.
|
void |
setJSONArray(JSONArray items)
Resets the adapter to store a new JSONArray of items.
|
void |
setNotifyOnChange(boolean notifyOnChange)
Control whether methods that change the list (
add(java.lang.Object) , clear() ) automatically
call notifyDataSetChanged() . |
void |
update(int position,
Object item)
Updates the item at the specified position in the adapter with the specified item.
|
areAllItemsEnabled, getItemViewType, getViewTypeCount, hasStableIds, isEmpty, isEnabled, notifyDataSetInvalidated, registerDataSetObserver, unregisterDataSetObserver
public NFJSONArrayAdapter(Context activity)
activity
- Context used for inflating viewspublic NFJSONArrayAdapter(Context activity, JSONTokener readFrom) throws JSONException
activity
- Context used for inflating viewsreadFrom
- a tokener whose nextValue() method will yield a JSONArray
to be
stored in the adapter.JSONException
- if the parse fails or doesn't yield a JSONArray
.public NFJSONArrayAdapter(Context activity, String json) throws JSONException
activity
- Context used for inflating viewsjson
- a JSON-encoded string containing an array to be stored in the adapterJSONException
- if the parse fails or doesn't yield a JSONArray
.public NFJSONArrayAdapter(Context activity, Collection items)
activity
- Context used for inflating viewsitems
- The items to represent within the adapter.public void add(Object item)
item
- The item to add at the end of the adapter.public void add(double item) throws JSONException
item
- The item to add at the end of the adapter.JSONException
- If item is NaN or infinitypublic void add(long item)
item
- The item to add at the end of the adapter.public void add(boolean item)
item
- The item to add at the end of the adapter.public void add(int item)
item
- The item to add at the end of the adapter.public void addAll(JSONArray items)
items
- The JSONArray to add at the end of the adapter.public void clear()
public Context getContext()
public int getCount()
public View getDropDownView(LayoutInflater inflater, int position, View convertView, ViewGroup parent)
Get a View
that displays in the drop down popup the data at the
specified position in the data set.
inflater
- the LayoutInflater object that can be used to inflate each view.position
- index of the item whose view we want.convertView
- the old view to reuse, if possible. Note: You should check that this view
is non-null and of an appropriate type before using. If it is not possible
to convert this view to display the correct data, this method can create a
new view.parent
- the parent that this view will eventually be attached toView
corresponding to the data at the specified position.public final View getDropDownView(int position, View convertView, ViewGroup parent)
getDropDownView
in interface SpinnerAdapter
getDropDownView
in class BaseAdapter
public Object getItem(int position)
public boolean getItemBoolean(int position) throws JSONException
position
- Position of the item whose data we want within the adapter's data set.JSONException
- If the value at position doesn't exit or cannot be coerced to
a boolean.public double getItemDouble(int position) throws JSONException
position
- Position of the item whose data we want within the adapter's data set.JSONException
- If the value at position doesn't exit or cannot be coerced to
a double.public long getItemId(int position)
public int getItemInt(int position) throws JSONException
position
- Position of the item whose data we want within the adapter's data set.JSONException
- If the value at position doesn't exit or cannot be coerced to
a int.public JSONArray getItemJSONArray(int position) throws JSONException
position
- Position of the item whose data we want within the adapter's data set.JSONException
- If the value at position doesn't exit or is not a JSONArray.public JSONObject getItemJSONObject(int position) throws JSONException
position
- Position of the item whose data we want within the adapter's data set.JSONException
- If the value at position doesn't exit or is not a JSONObject.public long getItemLong(int position) throws JSONException
position
- Position of the item whose data we want within the adapter's data set.JSONException
- If the value at position doesn't exit or cannot be coerced to
a long.public String getItemString(int position) throws JSONException
position
- Position of the item whose data we want within the adapter's data set.JSONException
- If no such value exists.public JSONArray getJSONArray()
public void setJSONArray(JSONArray items)
clear()
, then addAll(org.json.JSONArray)
without having to worry about an extra
notifyDataSetChanged()
invoked in between. Will repeat the last filtering request if
invoked while filtered results are being displayed.items
- New JSONArray of items to store within the adapter.public abstract View getView(LayoutInflater inflater, int position, View convertView, ViewGroup parent)
LayoutInflater.inflate(int, android.view.ViewGroup, boolean)
to specify
a root view and to prevent attachment to the root.inflater
- the LayoutInflater object that can be used to inflate each view.position
- The position of the item within the adapter's data set of the item whose
view we want.convertView
- The old view to reuse, if possible. Note: You should check that this view
is non-null and of an appropriate type before using. If it is not possible
to convert this view to display the correct data, this method can create a
new view. Heterogeneous lists can specify their number of view types, so
that this View is always of the right type (see BaseAdapter.getViewTypeCount()
and BaseAdapter.getItemViewType(int)
).parent
- The parent that this view will eventually be attached topublic boolean isNull(int position)
null
reference or NULL
.public void notifyDataSetChanged()
notifyDataSetChanged
in class BaseAdapter
public Object optItem(int position)
position
- Position of the item whose data we want within the adapter's data set.public boolean optItemBoolean(int position)
position
- Position of the item whose data we want within the adapter's data set.public boolean optItemBoolean(int position, boolean fallback)
position
- Position of the item whose data we want within the adapter's data set.fallback
- Value to return if no data is found.public double optItemDouble(int position)
position
- Position of the item whose data we want within the adapter's data set.public double optItemDouble(int position, double fallback)
position
- Position of the item whose data we want within the adapter's data set.fallback
- Value to return if no data is found.public int optItemInt(int position)
position
- Position of the item whose data we want within the adapter's data set.public int optItemInt(int position, int fallback)
position
- Position of the item whose data we want within the adapter's data set.fallback
- Value to return if no data is found.public JSONArray optItemJSONArray(int position)
position
- Position of the item whose data we want within the adapter's data set.public JSONObject optItemJSONObject(int position)
position
- Position of the item whose data we want within the adapter's data set.public long optItemLong(int position)
position
- Position of the item whose data we want within the adapter's data set.public long optItemLong(int position, long fallback)
position
- Position of the item whose data we want within the adapter's data set.fallback
- Value to return if no data is found.public String optItemString(int position)
position
- Position of the item whose data we want within the adapter's data set.public String optItemString(int position, String fallback)
position
- Position of the item whose data we want within the adapter's data set.fallback
- Value to return if no data is found.public void setNotifyOnChange(boolean notifyOnChange)
Control whether methods that change the list (add(java.lang.Object)
, clear()
) automatically
call notifyDataSetChanged()
. If set to false, caller must manually call
notifyDataSetChanged() to have the changes reflected in the attached view.
The default is true, and calling notifyDataSetChanged() resets the flag to true.
notifyOnChange
- if true, modifications to the list will automatically call notifyDataSetChanged()
public void update(int position, Object item) throws JSONException
position
- The location at which to put the specified itemitem
- The new item to replace with the oldJSONException
- If item is NaN or infinity