Using Merchant Connector

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.

Querying 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.

  1. First, get the merchant's CloverAccount:
private Account mAccount;
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  mAccount = CloverAccount.getAccount(this);
  1. Then, get the merchant's Currency by using MerchantConnector to get the Merchant:
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() {
  }
});
  1. Use a CursorLoader to query the ContentProvider:
getLoaderManager().initLoader(ITEM_LOADER_ID, null, InventoryListActivity.this);
  1. In onCreateLoader, return the CursorLoader:
return new CursorLoader(this, InventoryContract.Item.contentUriWithAccount(mAccount), null, null, null, InventoryContract.Item.PRICE);
  1. Once the query is complete, retrieve the inventory item's data from the cursor and display using a SimpleCursorAdapter. Set a ViewBinder to format the price into a String using the merchant's Currency.
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;
      }
    });

Did this page help you?