Skip to content

The Generic List Picker Form Cell

GenericListPickerFormCell is an enhanced version of the older ListPickerFormCell class.

Applications often present a long list of items and allow users to express their choice in either single or multiple selections.

GenericListPickerFormCell is designed to handle such complex lists with ease and allow developers to:

  • inflate custom views to present the target items.
  • bind items by their position or custom id.
  • track users' interactions and notify the application of the changes (user's selection choices) through callbacks.

The cell can easily be configured to allow: single selection only – Presents the list of items with radio buttons and allows only one selection. multi selection – Presents the list of items with check boxes and allows users to make multiple selections.

This Cell also allows you to perform paging for better performance. For example, if you have 100,000 elements to present, then GenericListPickerFormCell allows you to only fetch the initial 100 elements and then load new elements as the user scrolls the list.

Using the GenericListPickerFormCell

This form cell can be used within your activity like any traditional Android view:

1
2
3
4
5
6
7
8
        <com.sap.cloud.mobile.fiori.formcell.GenericListPickerFormCell
                    android:id="@+id/genericTextPicker"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="?android:attr/selectableItemBackground"
                    app:activityTitle="Choose Text"
                    app:bindViewById="true"
                    app:key="Pick Text Options" />

GenericListPickerFormCellActivity

The GenericListPickerFormCell depends on GenericListPickerFormCellActivity to present the list items. GenericListPickerFormCellActivity is an abstract class responsible for creating the list items, presenting them to a user, tracking the user actions, and informing the form cell about the changes made.

As shown in the following example, GenericListPickerFormCellActivity has two generic parameters: #### V extends View – Represents the type of views to be presented to user; for example, TextView or ObjectCell. #### T extends Serializable – Represents the type of key used to identify two different elements in the list being presented; for example, position of the item in actual list or item id from your database.

1
class GenericListPickerFormCellActivity<V extends View, T extends Serializable> extends ...

In order to create and bind the data, GenericListPickerFormCellActivity follows the RecyclerView.Adapter pattern. For the ease of usage, APIs of the Activity are named along the lines of RecyclerView.Adapter APIs.

The following table lists the methods, with descriptions, that you have to override to set up the GenericListPickerFormCellActivity.

Abstract Methods Description
int getItemCount() Informs the GenericListPickerFormCellActivity about the number of items in the List.
int getItemViewType(int position) Inflates different types of list item views. This method informs the GenericListPickerFormCellActivity about type of the view at a given position.
V onCreateView(int viewType, @NonNull Context context) Given a view type, creates the View.
void onBindView(@NonNull V view, int position) This is where you bind your view with data. Given position and view bind it to data.
T getId(int pos) Given a position in adapter, return the id used for the item.
  • You can use "app:value" XML attribute or setValue(List) to set the already selected value on the cell.
  • You can set GenericListPickerFormCellActivity using setPickerActivity(@Nullable GenericListPickerFormCellActivity pickerActivity) method on the form cell.

Selection Section

The list picker form cell comes with a selection section which, when it is enabled, shows all the selected items on top of the list. You can enable or disable the selection section with setShowSelected(boolean) method.

Show selected Section Do not show selected section

Single select mode of the GenericListPickerFormCell does not support the selected section.

Left To Right

GenericListPickerFormCell allows you to position the selector (checkbox or radio) button on either left of right end of the item view. Using setLeftToRight API on GenericListPickerFormCell, you can position the selector on either end.

By default, selectors are positioned on the start or left end of the item.

Selector on Left Selector on Right

Paging using GenericListPickerFormCell

If it is desired to present a long list of items to the user, then it is good practice to load the items when required. Loading only partial lists saves memory and computation resources.

You can override the void onBindPosition(int pos) method in GenericListPickerFormCellActivity class to be notified when the adapter position of the item for which onBindView(@NonNull V view, T id) is called. This gives you an indication as to whether your current list is going to be exhausted and you can trigger network calls to download more items.

1
2
3
4
5
6
@Override
protected void onBindPosition(int pos) {
        if(pos < getItemCount() - 10) {
            // trigger the network call to load more items as we have only 10 more spare items to present
        }
    }

Editable and Non-Editable Modes

This form cell can be set to editable or non-editable modes. For the list picker form cell, editable and enabled attributes are the same: a non-editable form cell is not enabled.

By default, the list picker form cell is editable and enabled. You can control the editability of the cell by using XML attribute "app:editable="false". You can also fetch the view in your activity and set it to be editable using Java APIs.

1
2
       GenericListPickerFormCell mPickerFormCell = findViewById(R.id.pickerCell);
       mPickerFormCell.setIsEditable(false);
For more information about the editable attribute, see FormCell.

Error and Helper

Follow design guidelines and see FormCell

Listening for Cell Value Changes

Like all FormCells, GenericListPickerFormCell notifies the application about changes through CellValueChangeListener. To listen for updates to a cell value, attach a CellValueChangeListener using setCellValueChangeListener:

1
2
3
4
5
6
mPickerFormCell.setCellValueChangeListener(new FormCell.CellValueChangeListener<List<T>>() {
            @Override
            protected void cellChangeHandler(@NonNull List<T> value) {
                // Application logic here
            }
        });
See also FormCell.