1

In WPF we can define an array and bind it to our xml. Elements are then automatically generated based on the contents of this array.:

public partial class MainWindow : Window {
    public MainWindow() { 
        InitializeComponent(); 
        this.DataContext = this;
    } 

    ObservableCollection<int> sampleData = new ObservableCollection<int>();
    public ObservableCollection<int> SampleData {
        get {
            if (sampleData.Count <= 0) {
                sampleData.Add(1);
                sampleData.Add(2);
                sampleData.Add(3);
                sampleData.Add(4);
            }
            return sampleData;
        }
    }
}


<Window x:Class="Sandbox.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox ItemsSource="{Binding Path=SampleData}"/>
    </Grid>
</Window>

Is something like this possible in android?

2
  • @Vanna will look into it. I didnt even know what to search for but that definately is a nice start. Commented Aug 16, 2017 at 13:24
  • @Vanna do you mean this? developer.android.com/reference/android/widget/… Commented Aug 16, 2017 at 13:30

1 Answer 1

1

You can define an XML file, in res/values/strings.xml, to define your array member using:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
    </string-array>
</resources>

More about array resource

Then receive using

getResources().getStringArray(R.array.planets_array);

But there is no

automatically generated based on the contents of this array

similar method in Android. To do it you will need to implement an ArrayAdapter.

More about ArrayAdapter

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer. I also liked the comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.