com.parse
Class ParseQueryAdapter<T extends ParseObject>

Object
  extended by android.widget.BaseAdapter
      extended by com.parse.ParseQueryAdapter<T>
All Implemented Interfaces:
Adapter, ListAdapter, SpinnerAdapter

public class ParseQueryAdapter<T extends ParseObject>
extends BaseAdapter

A ParseQueryAdapter handles the fetching of objects by page, and displaying objects as views in a ListView.

This class is highly configurable, but also intended to be easy to get started with. See below for an example of using a ParseQueryAdapter inside an Activity's onCreate:

 final ParseQueryAdapter adapter = new ParseQueryAdapter(this, "TestObject");
 adapter.setTextKey("name");
 
 ListView listView = (ListView) findViewById(R.id.listview);
 listView.setAdapter(adapter);
 
Below, an example showing off the level of configuration available with this class:
 // Instantiate a QueryFactory to define the ParseQuery to be used for fetching items in this
 // Adapter.
 ParseQueryAdapter.QueryFactory<ParseObject> factory =
     new ParseQueryAdapter.QueryFactory<ParseObject>() {
       public ParseQuery create() {
         ParseQuery query = new ParseQuery("Customer");
         query.whereEqualTo("activated", true);
         query.orderByDescending("moneySpent");
         return query;
       }
     };
 
 // Pass the factory into the ParseQueryAdapter's constructor.
 ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<ParseObject>(this, factory);
 adapter.setTextKey("name");
 
 // Perhaps set a callback to be fired upon successful loading of a new set of ParseObjects.
 adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() {
   public void onLoading() {
     // Trigger any "loading" UI
   }
 
   public void onLoaded(List<ParseObject> objects, ParseException e) {
     // Execute any post-loading logic, hide "loading" UI
   }
 });
 
 // Attach it to your ListView, as in the example above
 ListView listView = (ListView) findViewById(R.id.listview);
 listView.setAdapter(adapter);
 


Nested Class Summary
static interface ParseQueryAdapter.OnQueryLoadListener<T extends ParseObject>
          Implement with logic that is called before and after objects are fetched from Parse by the adapter.
static interface ParseQueryAdapter.QueryFactory<T extends ParseObject>
          Implement to construct your own custom ParseQuery for fetching objects.
 
Field Summary
 
Fields inherited from interface android.widget.Adapter
IGNORE_ITEM_VIEW_TYPE, NO_SELECTION
 
Constructor Summary
ParseQueryAdapter(Context context, Class<? extends ParseObject> clazz)
          Constructs a ParseQueryAdapter.
ParseQueryAdapter(Context context, Class<? extends ParseObject> clazz, int itemViewResource)
          Constructs a ParseQueryAdapter.
ParseQueryAdapter(Context context, ParseQueryAdapter.QueryFactory<T> queryFactory)
          Constructs a ParseQueryAdapter.
ParseQueryAdapter(Context context, ParseQueryAdapter.QueryFactory<T> queryFactory, int itemViewResource)
          Constructs a ParseQueryAdapter.
ParseQueryAdapter(Context context, String className)
          Constructs a ParseQueryAdapter.
ParseQueryAdapter(Context context, String className, int itemViewResource)
          Constructs a ParseQueryAdapter.
 
Method Summary
 void addOnQueryLoadListener(ParseQueryAdapter.OnQueryLoadListener<T> listener)
           
 void clear()
          Remove all elements from the list.
 Context getContext()
          Return the context provided by the Activity utilizing this ParseQueryAdapter.
 int getCount()
          Overrides Adapter's ParseQueryAdapter.getCount() method to return the number of cells to display.
 T getItem(int index)
          
 long getItemId(int position)
          
 View getItemView(T object, View v, ViewGroup parent)
          Override this method to customize each cell given a ParseObject.
 int getItemViewType(int position)
           
 View getNextPageView(View v, ViewGroup parent)
          Override this method to customize the "Load Next Page" cell, visible when pagination is turned on and there may be more results to display.
 int getObjectsPerPage()
           
 View getView(int position, View convertView, ViewGroup parent)
          The base class, Adapter, defines a getView method intended to display data at the specified position in the data set.
 int getViewTypeCount()
           
 void loadNextPage()
          Loads the next page of objects, appends to table, and notifies the UI that the model has changed.
 void loadObjects()
          Clears the table and loads the first page of objects asynchronously.
 void registerDataSetObserver(DataSetObserver observer)
           
 void removeOnQueryLoadListener(ParseQueryAdapter.OnQueryLoadListener<T> listener)
           
 void setAutoload(boolean autoload)
          Enable or disable the automatic loading of results upon attachment to an AdapterView.
 void setImageKey(String imageKey)
           
 void setObjectsPerPage(int objectsPerPage)
           
protected  void setPageOnQuery(int page, ParseQuery<T> query)
          Override this method to manually paginate the provided ParseQuery.
 void setPaginationEnabled(boolean paginationEnabled)
          Enable or disable pagination of results.
 void setPlaceholder(Drawable placeholder)
          Sets a placeholder image to be used when fetching data for each item in the AdapterView .
 void setTextKey(String textKey)
           
 void unregisterDataSetObserver(DataSetObserver observer)
           
 
Methods inherited from class android.widget.BaseAdapter
areAllItemsEnabled, getDropDownView, hasStableIds, isEmpty, isEnabled, notifyDataSetChanged, notifyDataSetInvalidated
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ParseQueryAdapter

public ParseQueryAdapter(Context context,
                         Class<? extends ParseObject> clazz)
Constructs a ParseQueryAdapter. Given a ParseObject subclass, this adapter will fetch and display all ParseObjects of the specified class, ordered by creation time.

Parameters:
context - The activity utilizing this adapter.
clazz - The ParseObject subclass type to fetch and display.

ParseQueryAdapter

public ParseQueryAdapter(Context context,
                         String className)
Constructs a ParseQueryAdapter. Given a ParseObject subclass, this adapter will fetch and display all ParseObjects of the specified class, ordered by creation time.

Parameters:
context - The activity utilizing this adapter.
className - The name of the Parse class of ParseObjects to display.

ParseQueryAdapter

public ParseQueryAdapter(Context context,
                         Class<? extends ParseObject> clazz,
                         int itemViewResource)
Constructs a ParseQueryAdapter. Given a ParseObject subclass, this adapter will fetch and display all ParseObjects of the specified class, ordered by creation time.

Parameters:
context - The activity utilizing this adapter.
clazz - The ParseObject subclass type to fetch and display.
itemViewResource - A resource id that represents the layout for an item in the AdapterView.

ParseQueryAdapter

public ParseQueryAdapter(Context context,
                         String className,
                         int itemViewResource)
Constructs a ParseQueryAdapter. Given a ParseObject subclass, this adapter will fetch and display all ParseObjects of the specified class, ordered by creation time.

Parameters:
context - The activity utilizing this adapter.
className - The name of the Parse class of ParseObjects to display.
itemViewResource - A resource id that represents the layout for an item in the AdapterView.

ParseQueryAdapter

public ParseQueryAdapter(Context context,
                         ParseQueryAdapter.QueryFactory<T> queryFactory)
Constructs a ParseQueryAdapter. Allows the caller to define further constraints on the ParseQuery to be used when fetching items from Parse.

Parameters:
context - The activity utilizing this adapter.
queryFactory - A ParseQueryAdapter.QueryFactory to build a ParseQuery for fetching objects.

ParseQueryAdapter

public ParseQueryAdapter(Context context,
                         ParseQueryAdapter.QueryFactory<T> queryFactory,
                         int itemViewResource)
Constructs a ParseQueryAdapter. Allows the caller to define further constraints on the ParseQuery to be used when fetching items from Parse.

Parameters:
context - The activity utilizing this adapter.
queryFactory - A ParseQueryAdapter.QueryFactory to build a ParseQuery for fetching objects.
itemViewResource - A resource id that represents the layout for an item in the AdapterView.
Method Detail

getContext

public Context getContext()
Return the context provided by the Activity utilizing this ParseQueryAdapter.

Returns:
The activity utilizing this adapter.

getItem

public T getItem(int index)


getItemId

public long getItemId(int position)


getItemViewType

public int getItemViewType(int position)
Specified by:
getItemViewType in interface Adapter
Overrides:
getItemViewType in class BaseAdapter

getViewTypeCount

public int getViewTypeCount()
Specified by:
getViewTypeCount in interface Adapter
Overrides:
getViewTypeCount in class BaseAdapter

registerDataSetObserver

public void registerDataSetObserver(DataSetObserver observer)
Specified by:
registerDataSetObserver in interface Adapter
Overrides:
registerDataSetObserver in class BaseAdapter

unregisterDataSetObserver

public void unregisterDataSetObserver(DataSetObserver observer)
Specified by:
unregisterDataSetObserver in interface Adapter
Overrides:
unregisterDataSetObserver in class BaseAdapter

clear

public void clear()
Remove all elements from the list.


loadObjects

public void loadObjects()
Clears the table and loads the first page of objects asynchronously. This method is called automatically when this Adapter is attached to an AdapterView.

loadObjects() should only need to be called if ParseQueryAdapter.setAutoload(boolean) is set to false.


loadNextPage

public void loadNextPage()
Loads the next page of objects, appends to table, and notifies the UI that the model has changed.


getCount

public int getCount()
Overrides Adapter's ParseQueryAdapter.getCount() method to return the number of cells to display. If pagination is turned on, this count will include an extra +1 count for the pagination cell row.

Returns:
The number of cells to be displayed by the ListView.

getItemView

public View getItemView(T object,
                        View v,
                        ViewGroup parent)
Override this method to customize each cell given a ParseObject.

If a view is not provided, a default view will be created based upon android.R.layout.activity_list_item.

This method expects a TextView with id android.R.id.text1 in your object views. If ParseQueryAdapter.setImageKey(String) was used, this method also expects an ImageView with id android.R.id.icon.

This method displays the text value specified by the text key (set via ParseQueryAdapter.setTextKey(String)) and an image (described by a ParseFile, under the key set via ParseQueryAdapter.setImageKey(String)) if applicable. If the text key is not set, the value for ParseObject.getObjectId() will be displayed instead.

Parameters:
object - The ParseObject associated with this item.
v - The View associated with this row. This view, if non-null, is being recycled and intended to be used for displaying this item.
parent - The parent that this view will eventually be attached to
Returns:
The customized view displaying the ParseObject's information.

getNextPageView

public View getNextPageView(View v,
                            ViewGroup parent)
Override this method to customize the "Load Next Page" cell, visible when pagination is turned on and there may be more results to display.

This method expects a TextView with id android.R.id.text1.

Parameters:
v - The view object associated with this row + type (a "Next Page" view, instead of an "Item" view).
parent - The parent that this view will eventually be attached to
Returns:
The view object that allows the user to paginate.

getView

public final View getView(int position,
                          View convertView,
                          ViewGroup parent)
The base class, Adapter, defines a getView method intended to display data at the specified position in the data set. We override it here in order to toggle between ParseQueryAdapter.getNextPageView(View, ViewGroup) and ParseQueryAdapter.getItemView(ParseObject, View, ViewGroup) depending on the value of ParseQueryAdapter.getItemViewType(int).


setPageOnQuery

protected void setPageOnQuery(int page,
                              ParseQuery<T> query)
Override this method to manually paginate the provided ParseQuery. By default, this method will set the limit value to ParseQueryAdapter.getObjectsPerPage() and the skip value to ParseQueryAdapter.getObjectsPerPage() * page.

Overriding this method will not be necessary, in most cases.

Parameters:
page - the page number of results to fetch from Parse.
query - the ParseQuery used to fetch items from Parse. This query will be mutated and used in its mutated form.

setTextKey

public void setTextKey(String textKey)

setImageKey

public void setImageKey(String imageKey)

setObjectsPerPage

public void setObjectsPerPage(int objectsPerPage)

getObjectsPerPage

public int getObjectsPerPage()

setPaginationEnabled

public void setPaginationEnabled(boolean paginationEnabled)
Enable or disable pagination of results. Defaults to true.

Parameters:
paginationEnabled - Defaults to true.

setPlaceholder

public void setPlaceholder(Drawable placeholder)
Sets a placeholder image to be used when fetching data for each item in the AdapterView . Will not be used if ParseQueryAdapter.setImageKey(String) was not used to define which images to display.

Parameters:
placeholder - A Drawable to be displayed while the remote image data is being fetched. This value can be null, and ImageViews in this AdapterView will simply be blank while data is being fetched.

setAutoload

public void setAutoload(boolean autoload)
Enable or disable the automatic loading of results upon attachment to an AdapterView. Defaults to true.

Parameters:
autoload - Defaults to true.

addOnQueryLoadListener

public void addOnQueryLoadListener(ParseQueryAdapter.OnQueryLoadListener<T> listener)

removeOnQueryLoadListener

public void removeOnQueryLoadListener(ParseQueryAdapter.OnQueryLoadListener<T> listener)