1.) My main.xml contains :
<ListView
android:id="@+id/lsym"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
2.) After populating the list, I’m assigning an adapter to it as follows :
lsym.Adapter=new SymbolAdapter(this,result);
where result is a DataTable.
3.) Within SymbolAdapter, I have used :
convertView = inflater.Inflate(Resource.Layout.list_symbol, null);
4.) list_symbol.xml contains :
<RelativeLayout android:id="@+id/symbolLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:id="@+id/tvsym"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
5.) And finally, for list item click, I’m using following code snippet:
lsym.ItemClick+= SearchItem_Click;
&
private void SearchItem_Click(object sender, ItemEventArgs e)
{
string company=((TextView)e.View).Text;
Toast.MakeText(this, "Selected="+company,ToastLength.Short).Show();
}
as specified here.
But when I click a list item, it is giving me following error :
System.InvalidCastException: Cannot cast from source type to destination type.
SearchItem_Click (object,Android.Widget.ItemEventArgs)
at ((TextView)e.View).Text.
ANY IDEA WHY THIS IS HAPPENING?
I know I have put up lot of code but as I’m absolute beginner to mono for android, so any help appreciated.
e.ViewisRelativeLayoutso casting it to TextView will not work …instead of this you should call
FindViewById(Resource.Id.tvsym)on that view ( not just plain since it will be called on Activity)code should looks like:
TextView rowText = (TextView)e.View.FindViewById(Resource.Id.tvsym);EDIT: or in Mono you can use:
TextView rowText = e.View.FindViewById<TextView>(Resource.Id.tvsym);