Ad

Binding ItemTemplate In MvxListView

I'm developing simple Android application with Mvvmcross v.6.1.2 and have a trouble with MvxListView ItemTemplate binding. Here is my ListView, which is correctly binded to MvxObservableCollection Weather in my ViewModel:

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:local="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
            <MvxListView
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:divider="#000000"
                android:dividerHeight="3px"
                local:MvxBind="ItemsSource Weather; ItemClick ListItemClickCommand"
                local:MvxItemTemplate="ItemTemplateId weatherListItem"
                SelectedItem="{mvx:MvxBind CurrentWeather}"/>
    </LinearLayout>

and here is my ListView ItemTemplate:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
              android:background="#ffffff">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:layout_gravity="center_horizontal"
        local:MvxBind="Text WeatherName"/>
</LinearLayout>

What's wrong in local:MvxItemTemplate="ItemTemplateId weatherListItem"?

Ad

Answer

You have a couple of problems in your layout:

<MvxListView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:divider="#000000"
    android:dividerHeight="3px"
    local:MvxBind="ItemsSource Weather; ItemClick ListItemClickCommand"
    local:MvxItemTemplate="ItemTemplateId weatherListItem"
    SelectedItem="{mvx:MvxBind CurrentWeather}"/>

First issue here is

local:MvxItemTemplate="ItemTemplateId weatherListItem"

MvxItemTemplate when used in a axml/xml file, it refers to a layout to use as the item template. This will be a layout you have in your android resources. In order to point it at one of your resource layouts you need to write it like as follows:

local:MvxItemTemplate="@layout/nameoflayout"

This will give the ListView a ID that matches your layout when it is inflated.

The other issue here is that you are mixing Xamarin.Forms XAML attributes with Android attributes:

SelectedItem="{mvx:MvxBind CurrentWeather}"

This line won't inflate, because there is no attribute called SelectedItem in MvxListView. If you wish to bind a public property, it needs to be done in MvxBind.

local:MvxBind="ItemsSource Weather; ItemClick ListItemClickCommand; SelectedItem CurrentWeather"
Ad
source: stackoverflow.com
Ad