Skip to main content

Ready to market your stellar app?

Samsung Developer Program is your gateway to app monetization success.

Learn More

Samsung Developer Program

Getting and Displaying the Purchased Items List

This section describes how to get and display the list of the items in a specified range defined by index and date parameters that have been purchased by the current user (purchased items list but also referred to as the inbox list by code). The coding process in this section is similar to the purchase request coding process.

To get and display an uncached list of all items purchased by the user:

In Android Studio 1.1.0, add InboxListActivity to code, and display the purchased items list with ListView.

<!-- InboxList Button -->
   <Button
      android:id="@+id/btn_get_inbox_list"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="InboxList"
      android:onClick="doGetInBoxList" />
public void doGetInBoxList( View _view )
    {
      Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMdd",
                                                   Locale.getDefault() );
        String today = sdf.format( d );
               Intent intent = new Intent( MainActivity.this,
                                               InboxListActivity.class );
        intent.putExtra( "IapMode", MODE );
        intent.putExtra( "StartNum", 1 );
        intent.putExtra( "EndNum", 15 );
        intent.putExtra( "StartDate", "20130101" );
        intent.putExtra( "EndDate", today );

               startActivity( intent );
    }

When a user clicks the InboxList button, it calls the onCreate method in InboxListActivity in the code below, which saves the purchase item values, transfers them to the intent, and requests the purchased items list from IAP based on the transferred values.

public class InboxListActivity extends Activity implements OnGetInboxListener
{
    private int                 mIapMode          = 1;
    private int                 mStartNum         = 0;
    private int                 mEndNum           = 0;
    private String              mStartDate        = "";
    private String              mEndDate          = "";

       private SamsungIapHelper    mIapHelper        = null;

       @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
                       Intent intent = getIntent();

               if( intent != null && intent.getExtras() != null
                && intent.getExtras().containsKey( "IapMode" )
                && intent.getExtras().containsKey( "StartNum" )
                && intent.getExtras().containsKey( "EndNum" )
                && intent.getExtras().containsKey( "StartDate" )
                && intent.getExtras().containsKey( "EndDate" ) )
        {
            Bundle extras = intent.getExtras();

            mIapMode     = extras.getInt( "IapMode" );
            mStartNum    = extras.getInt( "StartNum" );       
            mEndNum      = extras.getInt( "EndNum" );
            mStartDate   = extras.getString( "StartDate" );        
            mEndDate     = extras.getString( "EndDate" );
        }
        else
        {
            Toast.makeText( this,
                            R.string.invalid_parameter,
                              Toast.LENGTH_LONG ).show();
            finish();
        }

        mIapHelper = SamsungIapHelper.getInstance( this, mIapMode );
        mIapHelper.getItemInboxList( mStartNum,
                                        mEndNum,
                                        mStartDate,
                                        mEndDate,
                                        this );
    }

    @Override
    public void onGetItemInbox
    (  
        ErrorVo              _errorVo,
        ArrayList<InboxVo>  _inboxList
    )
    }
}

The code may look complicated; however, it just saves the transferred values. In fact, only the two highlighted lines correspond to the request to get the purchased items list from IAP. Like the purchase request, the final results are obtained using the onGetItemInbox method in the onGetInboxListener interface. When you run the code, you cannot verify the results visibly because there is no code in onGetItemInbox. However, you can verify that your service app is actually linked to IAP and that the result is transferred to onGetItemInbox.

NOTE: getItemInboxList(String, int, int, String, String, OnGetInboxListener) has been deprecated. Instead, use: getItemInboxList(int, int, String, String, OnGetInboxListener).

CAUTIONgetItemInboxList returns a list of ALL purchased items regardless of development mode.

<activity
        android:name=".InboxListActivity"
        android:label="@string/items_inbox_list" >
   </activity>
@Override
    public void onGetItemInbox

    (  
        ErrorVo              _errorVo,
        ArrayList<InboxVo>  _inboxList
    )
    {
        if( _errorVo != null
                && _errorVo.getErrorCode() ==
                                   SamsungIapHelper.IAP_ERROR_NONE )
        {
            if( _inboxList != null && _inboxList.size() > 0 )
            {
                mInboxList.addAll( _inboxList );
                mInboxListAdapter.notifyDataSetChanged();
            }
        }
    }
  1. Add a button to run InboxListActivity beneath the Purchase One Item button in the layout defined by the activity_main.xml layout, and set it to call the doGetInboxList method when clicked. 
  2. Implement the doGetInboxList method as shown in the following MainActivity.java code to set the required information in the intent and run InboxListActivity.
  3. Add the InboxListActivity to AndroidManifest.xml.
  4. Add data to the ListView in the onGetItemInbox method.
    setContentView( R.layout.inbox_list_layout );
    initView();

    The inbox_list_layout.xml file is as shown below. 

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:orientation="vertical">    
        <!-- Item Inbox List ListView -->
        <ListView
            android:id="@+id/itemInboxList"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:visibility="gone"/>
           <!-- Item Inbox List NULL -->
        <TextView
            android:id="@+id/noDataText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text=" @string/items_inbox_list_is_empty"/>
            </LinearLayout>

    The initView method is as shown below.

    public void initView()
        {
            mInboxListView = (ListView)findViewById( R.id.itemInboxList );
            mNoDataTextView = (TextView)findViewById( R.id.noDataText );
            mNoDataTextView.setVisibility( View.GONE );       
    
            mInboxListView.setEmptyView( mNoDataTextView );
                   mInboxListAdapter = new InboxListAdapter( this,
                                                        R.layout.inbox_row, 
                                                        mInboxList );       
    
            mInboxListView.setAdapter( mInboxListAdapter );
        }
    private ListView                              mInboxListView    = null;
    private TextView                             mNoDataTextView   = null;
    private ArrayList<InboxVo>          mInboxList        = new
                                                                                                ArrayList<InboxVo>();
    private InboxListAdapter   mInboxListAdapter = null;

    You can see mInboxListAdapter in the code above. The AdapterView offers a view for each row of the ListView. Further explanation of mInboxListAdapter is beyond the scope of this guide and is not discussed here.

    The InboxListAdapter is as shown below.

    public class InboxListAdapter extends ArrayAdapter<InboxVo>
    
    {
        private int                    mResId    = 0;
        private LayoutInflater         mInflater = null;
        private ArrayList<InboxVo>     mItems    = null;
        public InboxListAdapter
        (  
            Context                _context,
            int                    _resId,
            ArrayList<InboxVo>     _items
        )
        {
            super( _context, _resId, _items );
    
            mResId    = _resId;
            mItems    = _items;
            mInflater = (LayoutInflater)_context.getSystemService(
                                         Context.LAYOUT_INFLATER_SERVICE );
        }   
        public static class ViewHolder
        {
            TextView itemName;
            TextView itemPriceString;
            TextView itemType;
            TextView paymentId;
            TextView purchaseDate;
            TextView expireDate;
        }
    
        @Override
        public View getView
        (  
            final int         _position,
            View              _convertView,
            final ViewGroup   _parent
        )
        {
            final InboxVo vo = mItems.get( _position );
           ViewHolder vh;
            View v = _convertView;
    
            if( v == null )
            {
             vh = new ViewHolder();
             v = mInflater.inflate( mResId, null );
    
                        vh.itemName    =(TextView)v.findViewById( R.id.itemName );
             vh.itemPriceString =
                          (TextView)v.findViewById( R.id.itemPriceString );
             vh.itemType    =(TextView)v.findViewById( R.id.itemType );
             vh.paymentId   =(TextView)v.findViewById( R.id.paymentId );
             vh.purchaseDate=(TextView)v.findViewById( R.id.purchaseDate );
             vh.expireDate  =(TextView)v.findViewById( R.id.expireDate );           
    
              v.setTag( vh );
            }
            else
            {
                vh = (ViewHolder)v.getTag();
            }
    
            vh.itemName.setText( vo.getItemName() );
            vh.itemPriceString.setText( vo.getItemPriceString() );
            String itemType = "Type : ";
    
                   if( true == "00".equals( vo.getType() ) )
            {
                itemType += "Consumable";
                vh.expireDate.setVisibility( View.GONE );
            }
            else if( true == "01".equals( vo.getType() ) )
            {
                itemType += "NonConsumable";
                vh.expireDate.setVisibility( View.GONE );
            }
            else if( true == "02".equals( vo.getType() ) )
            {
                itemType += "Subscription";
                vh.expireDate.setVisibility( View.VISIBLE );
            }
            else if( true == "03".equals( vo.getType() ) )
            {
                itemType += "Auto Recurring Subscription";
                vh.expireDate.setVisibility( View.GONE );
            }
            else
            {
                itemType += "Unsupported type";
                vh.expireDate.setVisibility( View.GONE );
            }
            vh.itemType.setText( itemType );       
    
            vh.paymentId.setText( "Payment ID : " vo.getPaymentId() );
            vh.purchaseDate.setText( "Purchase Date : " +
                                                   vo.getPurchaseDate() );
            vh.expireDate.setText( "Expire Date : " +
                                            vo.getSubscriptionEndDate() );       
            return v;
        }
    }

    InboxAdapter creates a view for each row of the ListView. The code for the column layout of each view is as shown below. The filename is inbox_row.xml. This filename is transferred as an argument when InboxAdapter is created in the initView method.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp"
        android:paddingLeft="12dp"
        android:paddingRight="12dp"
        android:paddingTop="8dp"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <TextView
                android:id="@+id/itemName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:lineSpacingExtra="8dp"
                android:singleLine="true"
                android:ellipsize="end"
                android:textSize="14dp"
                android:textStyle="bold"
                android:gravity="center_vertical|left"
                android:layout_weight="1" />
    
            <TextView
                android:id="@+id/itemPriceString"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:lineSpacingExtra="8dp"
                android:singleLine="true"
                android:ellipsize="end"
                android:textSize="14dp"
                android:textStyle="bold"
                android:gravity="center_vertical|right"
                android:layout_marginLeft="10dp"
                android:layout_weight="0" />
    
           </LinearLayout>
    
        <TextView
            android:id="@+id/itemType"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:textSize="10dp"
            android:gravity="center_vertical|left"/>
    
           <TextView
            android:id="@+id/paymentId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:textSize="10dp"
            android:gravity="center_vertical|left"/>
       
        <TextView
            android:id="@+id/purchaseDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:textSize="10dp"
            android:gravity="center_vertical|left"/>
    
           <TextView
            android:id="@+id/expireDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:textSize="10dp"
            android:textColor="#FF0000"
            android:gravity="center_vertical|left"/>
    
    </LinearLayout>
    1. Add code that offers views in the Activity and initializes the views to the OnCreate method in InboxListActivity.
    2. Add the member parameters below to the InboxListActivity.
    3. The onGetItemInbox method to handle the result is implemented.
      The code below adds _inboxList to the mInboxList. _inboxList is generated when the Inbox request is processed successfully. The code also notifies mInboxListAdapter that data is revised and updates the ListView.

 

To test the purchased items list code:

  1. On a test device in Android Studio 1.1.0, run the purchasing code.
  2. Verify that a purchased items list similar to below is displayed.
  • Was this article helpful?