I have a ListView adapter that inflates a view to show in a ListView. When I try to get some of the items from the View by using FindViewById( Resource.Id.ItemId ) the debugger throws and error saying:
"System.InvalidCastException: Unable to convert instance of type 'Android.Widget.TableRow' to type 'Android.Widget.TextView' " .
I know that the resource Id that I'm using should map to a TextView.
There also is another problem in the some of the items never get the values place in them and so always display their default values.
Here is the 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>
Here is the code:
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;
}
}
}
And the auto generated resource:
#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
Does anybody know a reason why the auto generated resource would be out of sync with the Android inflated view?
Not certain, but it may be that you are looking in the wrong view?
var Row = convertView ?? Context.LayoutInflater.Inflate(Resource.Layout.TripListViewRow, null );
is actually giving you the TableLayout
, not either of the TableRow
s. To get the actual TableRow
s you might need to do
TableRow actualRow1 = Row.FindViewById<TableRow>(Resource.Id.tripViewTableRow1 );
TableRow actualRow2 = Row.FindViewById<TableRow>(Resource.Id.tripViewTableRow2 );
Then look for your TextView
s, etc., in actualRow1
and actualRow2
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.