Use Merchant Connector
United States
Canada
Europe
Latin America
To see our APIs in action, visit the Clover Android SDK examples directory in the Clover Android SDK. The examples use the Clover APIs to connect to Services, query Clover ContentProviders
, and fetch data directly from the Clover REST API.
Query inventory
In InventoryListActivity
, the Clover Inventory
ContentProvider
is queried to display a merchant's inventory items. The merchant's Currency
is also retrieved in order to format prices correctly.
- Get the merchant's
CloverAccount
:
private Account mAccount;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAccount = CloverAccount.getAccount(this);
- Get the merchant's
Currency
by usingMerchantConnector
to get theMerchant
:
mMerchantConnector = new MerchantConnector(this, mAccount, null);
mMerchantConnector.getMerchant(new ServiceConnector.Callback<Merchant>() {
@Override
public void onServiceSuccess(Merchant result, ResultStatus status) {
// set the Currency
sCurrencyFormat.setCurrency(result.getCurrency());
// now start the Loader to query Inventory
getLoaderManager().initLoader(ITEM_LOADER_ID, null, InventoryListActivity.this);
}
@Override
public void onServiceFailure(ResultStatus status) {
}
@Override
public void onServiceConnectionFailure() {
}
});
- Use a
CursorLoader
to query theContentProvider
:
getLoaderManager().initLoader(ITEM_LOADER_ID, null, InventoryListActivity.this);
- In
onCreateLoader
, return theCursorLoader
:
return new CursorLoader(this, InventoryContract.Item.contentUriWithAccount(mAccount), null, null, null, InventoryContract.Item.PRICE);
- Once the query is complete, retrieve the inventory item's data from the cursor and display using a
SimpleCursorAdapter
. Set aViewBinder
to format the price into a String using the merchant'sCurrency
.
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == android.R.id.text2) {
TextView priceTextView = (TextView) view;
String price = "";
PriceType priceType = PriceType.values()[cursor.getInt(cursor.getColumnIndex(InventoryContract.Item.PRICE_TYPE))];
String priceString = cursor.getString(cursor.getColumnIndex(InventoryContract.Item.PRICE));
long value = Long.valueOf(priceString);
if (priceType == PriceType.FIXED) {
price = sCurrencyFormat.format(value / 100.0);
} else if (priceType == PriceType.VARIABLE) {
price = "Variable";
} else if (priceType == PriceType.PER_UNIT) {
price = sCurrencyFormat.format(value / 100.0) + "/" + cursor.getString(cursor.getColumnIndex(InventoryContract.Item.UNIT_NAME));
}
priceTextView.setText(price);
return true;
}
return false;
}
});
Updated 3 months ago