Xamarin生成的资源ID与Android充气视图不同步

时间:2016-07-17 11:29:06

标签: android listview xamarin resource-id

我有一个ListView适配器,可以扩展视图以在ListView中显示。当我尝试使用FindViewById(Resource.Id.ItemId)从View中获取一些项目时,调试器抛出并错误地说:

" System.InvalidCastException:无法转换类型的实例' Android.Widget.TableRow'输入' Android.Widget.TextView' "

我知道我使用的资源ID应映射到TextView。

还有一些问题,其中一些项目永远不会将值放在其中,因此始终显示其默认值。

这是axml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tripViewTableLayout"
    android:showDividers="beginning">
    <TableRow
        android:id="@+id/tripViewTableRow1"
        android:layout_width="match_parent"
        android:showDividers="none"
        android:gravity="left">
        <TextView
            android:text="Servive Level"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/tripViewServiceLevel"
            android:textSize="10dp"
            android:layout_column="0"
            android:layout_gravity="left"
            android:layout_weight="0.4"
            android:maxEms="1"
            android:lines="1"
            android:editable="false"
            android:singleLine="true" />
        <TextView
            android:text="Package Type"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/tripViewPackageType"
            android:textSize="10dp"
            android:layout_gravity="left"
            android:layout_weight="0.4"
            android:maxEms="1"
            android:lines="1"
            android:editable="false"
            android:singleLine="true"
            android:layout_column="1" />
        <TextView
            android:text="Weight"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/tripViewWeight"
            android:textSize="10dp"
            android:ellipsize="end"
            android:gravity="left"
            android:layout_gravity="left"
            android:layout_weight="0.1"
            android:maxEms="1"
            android:lines="1"
            android:editable="false"
            android:singleLine="true"
            android:layout_column="2" />
        <TextView
            android:text="Due Time"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/tripViewDueTime"
            android:textSize="10dp"
            android:gravity="left"
            android:editable="false"
            android:lines="1"
            android:ellipsize="end"
            android:singleLine="true"
            android:layout_gravity="left"
            android:layout_weight="0.1"
            android:includeFontPadding="false"
            android:layout_width="match_parent"
            android:maxEms="1"
            android:layout_marginTop="3dp"
            android:layout_column="3" />
    </TableRow>
    <TableRow
        android:id="@+id/tripViewTableRow2"
        android:layout_width="match_parent"
        android:minWidth="25px"
        android:minHeight="25px">
        <ImageView
            android:src="@drawable/orangebar"
            android:layout_column="0"
            android:id="@+id/tripViewImageBar"
            android:layout_height="match_parent"
            android:scaleType="fitStart"
            android:cropToPadding="false"
            android:layout_width="5dp" />
        <LinearLayout
            android:orientation="vertical"
            android:minWidth="25px"
            android:minHeight="25px"
            android:id="@+id/tripViewLinearLayout1"
            android:layout_column="1"
            android:layout_span="3"
            android:layout_weight="1"
            android:layout_gravity="left"
            android:layout_width="match_parent">
            <TextView
                android:text="Company"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:id="@+id/tripViewCompanyName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="15dp"
                android:lines="1"
                android:maxEms="1"
                android:editable="false" />
            <TextView
                android:text="Address"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/tripViewCompanyAddress"
                android:editable="false"
                android:maxLines="1" />
        </LinearLayout>
    </TableRow>
</TableLayout>

以下是代码:

using System.Collections.Generic;
using Android.App;
using Android.Views;
using Android.Widget;
using RemoteService.Model;

namespace Driod2.Trips
{
    public class TripListAdapter : BaseAdapter<Trip>
    {
        private readonly List<Trip> Trips;
        private readonly Activity Context;

        public override long GetItemId( int position ) { return position; }

        public override View GetView( int position, View convertView, ViewGroup parent )
        {
            var T = Trips[ position ];

            var Row = convertView ?? Context.LayoutInflater.Inflate( Resource.Layout.TripListViewRow, null ); // re-use an existing view, if one is available

            Row.FindViewById<TextView>( Resource.Id.tripViewServiceLevel ).Text = T.ServiceLevel;
            Row.FindViewById<TextView>( Resource.Id.tripViewPackageType ).Text = T.PackageType;

            // !!!! The Error Happens here !!!!!! 
            Row.FindViewById<TextView>( Resource.Id.tripViewWeight ).Text = T.Weight.ToString( "F" );


            // !!!! If I don't get the error the the next 4 never change !!!!!! 
            Row.FindViewById<TextView>( Resource.Id.tripViewDueTime ).Text = T.DueTime.ToShortTimeString();
            Row.FindViewById<ImageView>( Resource.Id.tripViewImageBar ).SetImageResource( Resource.Drawable.BlueBar );
            Row.FindViewById<TextView>( Resource.Id.tripViewCompanyName ).Text = T.DeliveryCompany;
            Row.FindViewById<TextView>( Resource.Id.tripViewCompanyAddress ).Text = T.DeliveryCompanyAddressId.ToString();

            return Row;
        }

        public override int Count => Trips.Count;

        public override Trip this[ int position ] => Trips[ position ];

        public TripListAdapter( Activity context, List<Trip> trips )
        {
            Context = context;
            Trips = trips;
        }
    }
}

自动生成的资源:

#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

[assembly: global::Android.Runtime.ResourceDesignerAttribute("Driod2.Resource", IsApplication=true)]

namespace Driod2
{


    [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "1.0.0.0")]
    public partial class Resource
    {

        static Resource()
        {
            global::Android.Runtime.ResourceIdManager.UpdateIdValues();
        }

        public static void UpdateIdValues()
        {
            global::IdsRemoteService.Resource.String.ApplicationName = global::Driod2.Resource.String.ApplicationName;
            global::IdsRemoteService.Resource.String.Hello = global::Driod2.Resource.String.Hello;
            global::RemoteService.Resource.String.ApplicationName = global::Driod2.Resource.String.ApplicationName;
            global::RemoteService.Resource.String.Hello = global::Driod2.Resource.String.Hello;
        }

        public partial class Attribute
        {

            static Attribute()
            {
                global::Android.Runtime.ResourceIdManager.UpdateIdValues();
            }

            private Attribute()
            {
            }
        }

        public partial class Color
        {

            // aapt resource value: 0x7f060000
            public const int AliceBlue = 2131099648;

            // aapt resource value: 0x7f060001
            public const int SteelBlue = 2131099649;

            static Color()
            {
                global::Android.Runtime.ResourceIdManager.UpdateIdValues();
            }

            private Color()
            {
            }
        }

        public partial class Drawable
        {

            // aapt resource value: 0x7f020000
            public const int BlueBar = 2130837504;

            // aapt resource value: 0x7f020001
            public const int BottomBorder = 2130837505;

            // aapt resource value: 0x7f020002
            public const int CaratDownWhite = 2130837506;

            // aapt resource value: 0x7f020003
            public const int CloudWhite = 2130837507;

            // aapt resource value: 0x7f020004
            public const int ForwardWhite = 2130837508;

            // aapt resource value: 0x7f020005
            public const int Icon = 2130837509;

            // aapt resource value: 0x7f020006
            public const int offline32x32 = 2130837510;

            // aapt resource value: 0x7f020007
            public const int online32x32 = 2130837511;

            // aapt resource value: 0x7f020008
            public const int OrangeBar = 2130837512;

            static Drawable()
            {
                global::Android.Runtime.ResourceIdManager.UpdateIdValues();
            }

            private Drawable()
            {
            }
        }

        public partial class Id
        {

            // aapt resource value: 0x7f07000c
            public const int accountId = 2131165196;

            // aapt resource value: 0x7f070003
            public const int appTitle = 2131165187;

            // aapt resource value: 0x7f070000
            public const int baseLayout = 2131165184;

            // aapt resource value: 0x7f07000a
            public const int button1 = 2131165194;

            // aapt resource value: 0x7f070014
            public const int filter1Button = 2131165204;

            // aapt resource value: 0x7f070015
            public const int filter2Button = 2131165205;

            // aapt resource value: 0x7f070016
            public const int filter3Button = 2131165206;

            // aapt resource value: 0x7f070001
            public const int frameLayout1 = 2131165185;

            // aapt resource value: 0x7f070002
            public const int gridLayout1 = 2131165186;

            // aapt resource value: 0x7f070005
            public const int linearLayout2 = 2131165189;

            // aapt resource value: 0x7f070013
            public const int linearLayout3 = 2131165203;

            // aapt resource value: 0x7f070008
            public const int loginLayout = 2131165192;

            // aapt resource value: 0x7f070007
            public const int mainViewFlipper = 2131165191;

            // aapt resource value: 0x7f070010
            public const int password = 2131165200;

            // aapt resource value: 0x7f070006
            public const int pingIcon = 2131165190;

            // aapt resource value: 0x7f070011
            public const int signInButton = 2131165201;

            // aapt resource value: 0x7f070009
            public const int textView2 = 2131165193;

            // aapt resource value: 0x7f07000b
            public const int textView3 = 2131165195;

            // aapt resource value: 0x7f07000d
            public const int textView4 = 2131165197;

            // aapt resource value: 0x7f07000f
            public const int textView5 = 2131165199;

            // aapt resource value: 0x7f070004
            public const int tripCount = 2131165188;

            // aapt resource value: 0x7f070012
            public const int tripLayout = 2131165202;

            // aapt resource value: 0x7f070017
            public const int tripListView = 2131165207;

            // aapt resource value: 0x7f070022
            public const int tripViewCompanyAddress = 2131165218;

            // aapt resource value: 0x7f070021
            public const int tripViewCompanyName = 2131165217;

            // aapt resource value: 0x7f07001d
            public const int tripViewDueTime = 2131165213;

            // aapt resource value: 0x7f07001f
            public const int tripViewImageBar = 2131165215;

            // aapt resource value: 0x7f070020
            public const int tripViewLinearLayout1 = 2131165216;

            // aapt resource value: 0x7f07001b
            public const int tripViewPackageType = 2131165211;

            // aapt resource value: 0x7f07001a
            public const int tripViewServiceLevel = 2131165210;

            // aapt resource value: 0x7f070018
            public const int tripViewTableLayout = 2131165208;

            // aapt resource value: 0x7f070019
            public const int tripViewTableRow1 = 2131165209;

            // aapt resource value: 0x7f07001e
            public const int tripViewTableRow2 = 2131165214;

            // aapt resource value: 0x7f07001c
            public const int tripViewWeight = 2131165212;

            // aapt resource value: 0x7f07000e
            public const int userName = 2131165198;

            static Id()
            {
                global::Android.Runtime.ResourceIdManager.UpdateIdValues();
            }

            private Id()
            {
            }
        }

        public partial class Layout
        {

            // aapt resource value: 0x7f030000
            public const int Main = 2130903040;

            // aapt resource value: 0x7f030001
            public const int TripListViewRow = 2130903041;

            static Layout()
            {
                global::Android.Runtime.ResourceIdManager.UpdateIdValues();
            }

            private Layout()
            {
            }
        }

        public partial class Raw
        {

            // aapt resource value: 0x7f040000
            public const int BadScan = 2130968576;

            // aapt resource value: 0x7f040001
            public const int DeletedTrip = 2130968577;

            // aapt resource value: 0x7f040002
            public const int NewTrip = 2130968578;

            static Raw()
            {
                global::Android.Runtime.ResourceIdManager.UpdateIdValues();
            }

            private Raw()
            {
            }
        }

        public partial class String
        {

            // aapt resource value: 0x7f050001
            public const int ApplicationName = 2131034113;

            // aapt resource value: 0x7f050000
            public const int Hello = 2131034112;

            static String()
            {
                global::Android.Runtime.ResourceIdManager.UpdateIdValues();
            }

            private String()
            {
            }
        }
    }
}
#pragma warning restore 1591

有人知道自动生成的资源与Android充气视图不同步的原因吗?

1 个答案:

答案 0 :(得分:0)

不确定,但可能是你看错了?

var Row = convertView ?? Context.LayoutInflater.Inflate(Resource.Layout.TripListViewRow, null );

实际上是为您提供TableLayout,而不是TableRow。要获得实际的TableRow,您可能需要这样做

TableRow actualRow1 = Row.FindViewById<TableRow>(Resource.Id.tripViewTableRow1 );
TableRow actualRow2 = Row.FindViewById<TableRow>(Resource.Id.tripViewTableRow2 );

然后在TextViewactualRow1中查找您的actualRow2等。