how to create table with columns and fill it?

Shay Wilner 1,746 Reputation points
2023-06-19T19:42:12.87+00:00

Hello

i would like to add for each item in listview a button but each button is placed outside

Developer technologies .NET Xamarin
{count} votes

Accepted answer
  1. Anonymous
    2023-06-20T05:25:00.2333333+00:00

    Hello,

    You can do it by GridView in Xamarin android.

    Firstly, add GirdView in your layout.

    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"       
            />
    

    Then Open your Activity, you can find this GridView, populate the data and set the adapter of GridView. I add the header data in the List at the firstly, then other data.

    public class MainActivity : AppCompatActivity
        {
            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);
                List<classT> list1 = new List<classT>();
              //add the Grid header
                list1.Add(new classT() { elementA = "header1", elementB = "header2", numberA = "header3"            });
               //add the grid data
                for (int i = 0; i < 10; i++)
                {
                    list1.Add(new classT() { elementA = "A", elementB = "B", numberA = i.ToString() });
                }
                GridView gridview = FindViewById<GridView>(Resource.Id.gridview);
                gridview.Adapter = new MyAdapter(this, list1);
            }
    

    Next, we need to create custom adapter.

    public class MyAdapter : BaseAdapter<classT>
        {
            MainActivity activity;
            List<classT> classTs;
            public MyAdapter(MainActivity activity, List<classT> classTs) : base()
            {
                this.activity = activity;
                this.classTs = classTs;
            }
    
           public override int Count
            {
                get { return classTs.Count; }
            }   
            public override classT this[int position]
            {
                get { return classTs[position]; }
            }
            public override long GetItemId(int position)
            {
                return 0;
            }
            public override View GetView(int position, View convertView, ViewGroup parent)
            {
                View view;
                if (convertView == null)
                {
                    // if convertView is null create a new one 
                    view = activity.LayoutInflater.Inflate(Resource.Layout.griditem, null);
                }
                else
                {
                    //If not, reuse it.
                    view = convertView;
                }               
                              
                view.FindViewById<TextView>(Resource.Id.elementA).Text = classTs[position].elementA;
                view.FindViewById<TextView>(Resource.Id.elementB).Text = classTs[position].elementB;
                view.FindViewById<TextView>(Resource.Id.numberA).Text = classTs[position].numberA.ToString();
               return view;
    
           }
    
        }
    

    Here is my custom grid item layout callled griditem.xml in the Layout folder. You can add other items that you want.

    <?xml version="1.0" encoding="utf-8"?>   
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"   
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:id="@+id/elementA"       
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="Cell 0"
            android:textSize="14dip" />
        <TextView
               android:id="@+id/elementB"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_centerHorizontal="true"
               android:text="Cell 1"
               android:textSize="14dip" />
        <TextView
             android:id="@+id/numberA"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignParentEnd="true"
               android:text="Cell 2"
               android:textSize="14dip" />
    </RelativeLayout>
    

    Please note, I change the public int numberA { get; set; } to public string numberA { get; set; } for testing in classT.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.