Adding Create Item Functionality

Add Imports:

import android.widget.Button;
import android.widget.EditText;

Add Variables:

    private Button mCreateItemButton;
    private EditText mCreateItemEditText;
    private String mCreateItemText;

We want to initialize our create item text, and add our Button and EditTextView to our activity's onResume:

    @Override
    protected void onResume() {
        super.onResume();

        ...

        mCreateItemButton = (Button) findViewById(R.id.createItemButton);
        mCreateItemEditText = (EditText) findViewById(R.id.createItemEditText);
        mCreateItemText = "";
    }

We need to create a new AsyncTask to handle creating new items:

    private class CreateItemAsyncTask extends AsyncTask<Void, Void, Item> {

        @Override
        protected Item doInBackground(Void... voids) {
            try {
                Item newItem = new Item().setName(mCreateItemText).setPrice((long)0);
                mInventoryConnector.createItem(newItem);
                return newItem;
            } catch (RemoteException | ClientException | ServiceException| BindingException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected final void onPostExecute(Item item) {
            super.onPostExecute(item);

            new InventoryAsyncTask().execute();
        }
    }

Set an onClickListener to our create item button that calls our create item task, then clears our EditTextView:

        mCreateItemButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCreateItemText = mCreateItemEditText.getText().toString();
                new CreateItemAsyncTask().execute();
                mCreateItemEditText.setText("");
                mCreateItemText = "";
            }
        });

This is what your Main Activity should look like:

import android.accounts.Account;
import android.os.AsyncTask;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.clover.sdk.util.CloverAccount;
import com.clover.sdk.v1.BindingException;
import com.clover.sdk.v1.ClientException;
import com.clover.sdk.v1.ServiceException;
import com.clover.sdk.v1.merchant.Merchant;
import com.clover.sdk.v1.merchant.MerchantConnector;
import com.clover.sdk.v3.inventory.InventoryConnector;
import com.clover.sdk.v3.inventory.Item;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Account mAccount;
    private InventoryConnector mInventoryConnector;
    private MerchantConnector mMerchantConnector;
    private TextView mMerchantTextView;
    private TextView mInventoryTextView;
    private Button mCreateItemButton;
    private EditText mCreateItemEditText;
    private String mCreateItemText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();

        mMerchantTextView = (TextView) findViewById(R.id.merchantName);
        mInventoryTextView = (TextView) findViewById(R.id.inventoryItems);
        mCreateItemButton = (Button) findViewById(R.id.createItemButton);
        mCreateItemEditText = (EditText) findViewById(R.id.createItemEditText);
        mCreateItemText = "";

        //Retrieve Clover account
        if (mAccount == null) {
            mAccount = CloverAccount.getAccount(this);

            if (mAccount == null) {
                return;
            }
        }

        connectInventory();
        connectMerchant();

        new MerchantAsyncTask().execute();
        new InventoryAsyncTask().execute();

        mCreateItemButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCreateItemText = mCreateItemEditText.getText().toString();
                new CreateItemAsyncTask().execute();
                mCreateItemEditText.setText("");
                mCreateItemText = "";
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        disconnectInventory();
        disconnectMerchant();
    }

    private void connectMerchant() {
        disconnectMerchant();

        if (mAccount != null) {
            mMerchantConnector = new MerchantConnector(this, mAccount, null);
            mMerchantConnector.connect();
        }
    }

    private void disconnectMerchant() {
        if (mMerchantConnector != null) {
            mMerchantConnector.disconnect();
            mMerchantConnector = null;
        }
    }

    private void connectInventory() {
        disconnectInventory();

        if (mAccount != null ) {
            mInventoryConnector = new InventoryConnector(this, mAccount, null);
            mInventoryConnector.connect();
        }
    }

    private void disconnectInventory() {
        if (mInventoryConnector != null) {
            mInventoryConnector.disconnect();
            mInventoryConnector = null;
        }
    }

    private class InventoryAsyncTask extends AsyncTask<Void, Void, List<Item>> {

        @Override
        protected List<Item> doInBackground(Void... voids) {
            try {
                return mInventoryConnector.getItems();
            } catch (RemoteException | ClientException | ServiceException| BindingException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected final void onPostExecute(List<Item> items) {
            super.onPostExecute(items);

            StringBuilder inventoryText = new StringBuilder("Items: ");
            inventoryText.append("\n");

            for (int i = 0; i < items.size(); i++) {
                inventoryText.append(items.get(i).getName() + "\n");
            }

            mInventoryTextView.setText(inventoryText.toString());
        }
    }

    private class MerchantAsyncTask extends AsyncTask<Void, Void, Merchant> {

        @Override
        protected Merchant doInBackground(Void... voids) {
            try {
                return mMerchantConnector.getMerchant();
            } catch (ServiceException | BindingException | ClientException | RemoteException e) {

            }

            return null;
        }

        @Override
        protected void onPostExecute(Merchant merchant) {
            super.onPostExecute(merchant);
            mMerchantTextView.setText("Merchant Name: " + merchant.getName());
        }
    }

    private class CreateItemAsyncTask extends AsyncTask<Void, Void, Item> {

        @Override
        protected Item doInBackground(Void... voids) {
            try {
                Item newItem = new Item().setName(mCreateItemText).setPrice((long)0);
                mInventoryConnector.createItem(newItem);
                return newItem;
            } catch (RemoteException | ClientException | ServiceException| BindingException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected final void onPostExecute(Item item) {
            super.onPostExecute(item);

            new InventoryAsyncTask().execute();
        }
    }
}

Now we can run and add items:

results matching ""

    No results matching ""