How to add a TextView to StackView From code Xamarin?

Viewed 211

Am using Xamarin StackView, but when I try to add a child to it it gives me an error the code: xaml:

<StackView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/exchangesStack"
            android:layout_column="0"
            android:layout_row="4"/>

c#:

TextView txt = new TextView(BaseContext);
txt.Text = "TEST";
StackView stck = (StackView)FindViewById(Resource.Id.exchangesStack);
stck.AddView(txt);

error:

Java.Lang.UnsupportedOperationException: 'addView(View) is not supported in AdapterView'

I tried to google the problem and the error but I found nothing...

1 Answers

StackView is a type of view that can contain images and you can show your images in a stack form and also scroll your images up and down. You could create a new view in your Adapter instead of adding the Textview to StackView directly.

Model:

 public class StackItem
{
    public string itemText { get; set; }
    public int itemPhoto { get; set; }
    public StackItem(string text, int photo)
    {
        this.itemPhoto = photo;
        this.itemText = text;
    }
}

Adapter:

public class StackItemAdapter : BaseAdapter<StackItem>
{
    private List<StackItem> sList;
    private Context sContext;
    public StackItemAdapter(Context context, List<StackItem> list)
    {

        this.sList = list;
        this.sContext = context;
    }

    public override StackItem this[int position]
    {
        get
        {
            return sList[position];
        }
    }
    public override int Count
    {
        get
        {
            return sList.Count;
        }

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

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        if (row == null)
        {
            row = LayoutInflater.From(sContext).Inflate(Resource.Layout.layout1, null, false);
        }
        var m = sList[position];
        if (m != null)
        {
            TextView text = row.FindViewById<TextView>(Resource.Id.textView1);
            ImageView img = row.FindViewById<ImageView>(Resource.Id.imageView1);
            text.Text = sList[position].itemText;
            img.SetImageResource(sList[position].itemPhoto);
        }
        return row;
    }

}

The Main layout with the StackView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<StackView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/exchangesStack"
        android:layout_column="0"
        android:layout_row="4"/>
</RelativeLayout>

The new view you create with include the TextView for the Adapter:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"/>
</FrameLayout >

MainActivity:

public class MainActivity : AppCompatActivity
{
    private StackView exchangesStack;
    private List<StackItem> stackItems;
    StackItemAdapter adapter;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        stackItems = new List<StackItem>();
        stackItems.Add(new StackItem("dog", Resource.Drawable.dog));
        stackItems.Add(new StackItem("pink", Resource.Drawable.pink));
        stackItems.Add(new StackItem("walrus", Resource.Drawable.walrus));
        stackItems.Add(new StackItem("lighthouse", Resource.Drawable.lighthouse));
        stackItems.Add(new StackItem("coffee", Resource.Drawable.coffee));


        exchangesStack = FindViewById<StackView>(Resource.Id.exchangesStack);
        adapter = new StackItemAdapter(this, stackItems);
        exchangesStack.Adapter = adapter;
        exchangesStack.ItemClick += ExchangesStack_ItemClick;

     
    }

    private void ExchangesStack_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        var select = stackItems[e.Position].itemText;
        Android.Widget.Toast.MakeText(this, select, Android.Widget.ToastLength.Long).Show();
    }

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

Screenshot:

enter image description here

Related