Fernando
Fernando

Reputation: 646

Xamarin Adapter FindViewById returns null

Not sure what I'm doing wrong here but FindViewById always returns null for my custom row view:

public override View GetView(int position, View convertView, ViewGroup parent) {
    var view = convertView;
    var item = items[position];

    if(view == null)
        view = context.LayoutInflater.Inflate(Resource.Layout.listview_row, null);

    var nameTextView = view.FindViewById<TextView>(Resource.Id.nameTextView);
    var priceTextView = view.FindViewById<TextView>(Resource.Id.priceTextView);

    nameTextView.Text = item.Name;
    priceTextView.Text = (item.Price ?? 0M).ToString("#.00");

    return view;
}

This is the view code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView id="@+id/nameTextView"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="0.8"
        android:layout_margin="5dp" />

    <TextView id="@+id/priceTextView"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="0.2"
        android:layout_margin="5dp" />
</LinearLayout>

The view inflates correctly but for some reason both TextViews are always null after the call to FindViewById.

I have tried cleaning and rebuilding the project but that did not work. Not sure why this is not working.

Thank you for any pointers.

Upvotes: 0

Views: 178

Answers (1)

FreakyAli
FreakyAli

Reputation: 16499

You are providing an id with the wrong way hence it is not getting registered in Resource designer

Try this :

 android:id="@+id/nameTextView"
 android:id="@+id/priceTextView"

Upvotes: 1

Related Questions