FieldListViewArrayAdapter

This class is another possible choice when we have a Field<List<T>> that we want to display in a ListView. It mimics (and extends) the Android's ArrayAdapter class.

We have two ways of constructing it: via the constructor or via the FieldListViewArrayAdapter.of function. Both these version require the following parameters:

  • items: the Field<List<T>> containing the items to display.
  • context: a Context.
  • resource: the layout resource to inflate for each view.
  • textViewResourceId: a TextView ID contained in the passed layout that is the primary text. If 0 is passed, it takes the whole resource as a TextView.

The constructor than accepts a lambda that, given the inflated View, the TextView, the item T and the isDropdownView boolean binds the item to the view.

The of() method simply accepts a lambda that, for a given item T returns a Field<CharSequence?> containing the text to put in the main TextView.

There is also a special of() version that directly accepts a Field<List<CharSequence?>> as items.

Examples:

//Assuming we have a class Movie val movies : Field<List<Movie>> = getMovies(); //Example 1 listView.setAdapter(FieldListViewArrayAdapter.of( items = movies, context = context, resource = android.R.layout.list_view_items textViewResourceId = android.R.id.textView, textGetter = { it.name } )) //Example 2 listView.setAdapter(FieldListViewArrayAdapter.of( items = movies.transform { it.name }, //We are passing directly a Field<List<CharSequence?>> context = context, resource = android.R.layout.list_view_items textViewResourceId = android.R.id.textView )) //Example 3, using constructor listView.setAdapter(FieldListViewArrayAdapter( items = movies, context = context, resource = android.R.layout.list_view_items textViewResourceId = android.R.id.textView, viewBinder = { view, textView, item, isDropdownView -> textView.setText(movie.name) } ))
//Assuming we have a class Movie Field<List<Movie> movies = getMovies(); //Example 1 listView.setAdapter(FieldListViewArrayAdapter.of( movies, //items context, //context android.R.layout.list_view_items //resource android.R.id.textView, //textViewResourceId movie -> movie.getName() //textGetter )); //Example 2 listView.setAdapter(FieldListViewArrayAdapter.of( movies.transform(movie -> movie.getName()), //items: We are passing directly a Field<List<CharSequence?>> context, //context android.R.layout.list_view_items //resource android.R.id.textView //textViewResourceId )); //Example 3, using constructor listView.setAdapter(new FieldListViewArrayAdapter( movies, //items context, //context android.R.layout.list_view_items //resource android.R.id.textView, //textViewResourceId (view, textView, item, isDropdownView) -> { //viewBinder textView.setText(movie.name) } ));