Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Make Group of XML For Every n Items in List

vK_HDvK_HD Member ✭✭
edited July 2019 in Xamarin.Android

Okay, I've had a fair chunk of experience with classic C#, but I literally picked xamarin android up 26 hours ago so I'm still very green to this and XML in general. I've got a TableLayout currently, the idea was to have headings which would have records underneath containing different entries e.g. NAME AGE LOCATION -- first entry: David 24 London, under that another entry: Susan 29 Kentucky.

The data is saved into a personal folder for the app on the device (this app is only going to really be used by me and close associates so there's no need for it to be perfectly data-secure, it's not even containing any real info about any real people). I'm getting this from a txt file and reading in the data into string arrays, which I then use the elements of arrays to display the data into the table (It works for 1 entry), but like this, I'm simply doing a 'SetText' to change the text of TextView, this means I can only have 1 record, I would obviously like more than 1 record.

So, the main question - how can I get something like

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
        android:text=""
        android:id="@+id/txtDisplayName"/>
    <TextView
        android:text=""
        android:id="@+id/txtDisplayRace"/>
    <TextView
        android:text=""
        android:id="@+id/txtS" />
    <TextView
        android:text=""
        android:id="@+id/txtP" />
    <TextView
        android:text=""
        android:id="@+id/txtE" />
    <TextView
        android:text=""
        android:id="@+id/txtC" />
    <TextView
        android:text=""
        android:id="@+id/txtI" />
    <TextView
        android:text=""
        android:id="@+id/txtA" />
    <TextView
        android:text=""
        android:id="@+id/txtL" />
</TableRow>

(Sorry I don't know how to format it here, on stack overflow it's 4 spaces) to be generated for every record?

Thanks for any help :) I appreciate it.

Edit: Just to add, even if it's just a name of a technique or article I can read I'll be grateful :)

Tagged:

Best Answer

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    Accepted Answer

    Do you want to achieve following screenshot.

    If you, you could create layout.xml like following code.

       <TableLayout 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"
      android:id="@+id/TableLayout2"
       android:stretchColumns="0,1,2">
    
    
       <TableRow >
       <TextView
        android:text="NAME "
        android:id="@+id/txtDisplayName"/>
       <TextView
        android:text="AGE "
        android:id="@+id/txtDisplayAge"/>
    
    <TextView
        android:text="Location "
        android:id="@+id/txtDisplayLocation"/>
      </TableRow>
       </TableLayout>
    

    In the backend cs file. you can add the table row like following code.(I just put the txt to the assets folder)

         public class MainActivity : AppCompatActivity
       {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
            TableLayout tableLayout=FindViewById<TableLayout>(Resource.Id.TableLayout2);
            string content;
            AssetManager assets = this.Assets;
            using (StreamReader sr = new StreamReader(assets.Open("myfile.txt")))
            {
                content = sr.ReadToEnd();
            }
            string[] st=content.Split(",");
    
    
            foreach (var item in st)
            {
    
                string[] st2=item.Split(" ");
                TableRow tr = new TableRow(this);
                TextView name = new TextView(this);
                name.Text = st2[0];
                TextView age = new TextView(this);
                age.Text = st2[1];
                TextView location = new TextView(this);
                location.Text = st2[2];
    
                tr.AddView(name);
                tr.AddView(age);
                tr.AddView(location);
    
                tableLayout.AddView(tr);
            }
    
           }
      }
    

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    Accepted Answer

    Do you want to achieve following screenshot.

    If you, you could create layout.xml like following code.

       <TableLayout 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"
      android:id="@+id/TableLayout2"
       android:stretchColumns="0,1,2">
    
    
       <TableRow >
       <TextView
        android:text="NAME "
        android:id="@+id/txtDisplayName"/>
       <TextView
        android:text="AGE "
        android:id="@+id/txtDisplayAge"/>
    
    <TextView
        android:text="Location "
        android:id="@+id/txtDisplayLocation"/>
      </TableRow>
       </TableLayout>
    

    In the backend cs file. you can add the table row like following code.(I just put the txt to the assets folder)

         public class MainActivity : AppCompatActivity
       {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
            TableLayout tableLayout=FindViewById<TableLayout>(Resource.Id.TableLayout2);
            string content;
            AssetManager assets = this.Assets;
            using (StreamReader sr = new StreamReader(assets.Open("myfile.txt")))
            {
                content = sr.ReadToEnd();
            }
            string[] st=content.Split(",");
    
    
            foreach (var item in st)
            {
    
                string[] st2=item.Split(" ");
                TableRow tr = new TableRow(this);
                TextView name = new TextView(this);
                name.Text = st2[0];
                TextView age = new TextView(this);
                age.Text = st2[1];
                TextView location = new TextView(this);
                location.Text = st2[2];
    
                tr.AddView(name);
                tr.AddView(age);
                tr.AddView(location);
    
                tableLayout.AddView(tr);
            }
    
           }
      }
    
Sign In or Register to comment.