In Android, I have an ArrayList of Sections (there is a Section class so it’s not just an ArrayList of Strings). I would like to represent each Section as a Button. Currently, I am accomplishing this by iterating through each Section, inflating section.xml, and then dynamically adding the properties that vary with each particular Section:
SectionsActivity.java:
public class SectionsActivity extends Activity {
private int numSections;
LayoutInflater inflater;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sections);
numSections = App.Sections.getSectionList().size();
inflater = getLayoutInflater();
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
for (int i = 0; i < numSections; i++) {
ll.addView(getSectionButton(App.Sections.getSectionList().get(i)));
}
}
public Button getSectionButton(Section s) {
Button b = (Button) inflater.inflate(R.layout.section, null);
b.setHint("section" + s.getSectionId());
b.setText(s.getName());
b.setTextColor(Color.parseColor("#"+s.getColor()));
return b;
}
}
Sections.java:
public class Sections {
private ArrayList<Section> SectionList;
public ArrayList<Section> getSectionList() {
return SectionList;
}
public void setSectionList(ArrayList<Section> sectionList) {
SectionList = sectionList;
}
}
Section.java:
public class Section {
private String Color;
private String Name;
private int SectionId;
//constructor, standard getters and setters
}
section.xml:
<?xml version="1.0" encoding="utf-8"?>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold" />
This works fine, but I feel like there’s probably a better solution. Here’s an example in .NET for Windows Phone 7: you tell the XAML what you want bound (SectionList, which is an ObservableCollection) and then you give it a template for how each item in the collection should be represented.
<StackPanel Name="StackPanelSection">
<ListBox Name="ListBoxSection" ItemsSource="{Binding SectionList}" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name, Converter={StaticResource StringToLowercaseConverter}}" FontFamily="Segoe WP SemiLight" FontSize="48" Foreground="{Binding HTMLColor}" Tap="TextBlockSection_Tap" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
This is better, both in simplicity and in that if you change the contents of SectionList, the UI automatically updates. I’ve read enough about data-binding in Android to know that there’s probably not a true equivalent, but what would be the best way to go about accomplishing the same task? Is there one? Even if data-binding is not a good solution here, is there a different way I should be structuring my Android code?
You get that binding in XAML because it’s baked into the framework. Your app executes in a runtime environment that supports the binding lookup, so you get a whole binding framework from Microsoft as part of the toolset. Android just doesn’t have this kind of thing.
I don’t know of a way to do your binding in a declarative way like you would in XAML, but being in a similar boat (coming from WPF/.Net/XAML background into Android), I’ve found creative ways of just making it more convenient. Looks like you’re well along in that path. I make use of custom adapters for any lists or grids I use which give a similar convenience…not AS convenient as xaml binding, but still pretty cool.
I haven’t seen your UI so I can only make assumptions from your code, but I can only assume that what you’re doing (Buttons inside a LinearLayout) can be accomplished with a ListView and a custom adapter. Probably the most classic Android developer video is World of ListView from a past Google I/O. It’s a few years old, but still a great watch and still relevant.
https://www.youtube.com/watch?v=wDBM6wVEO70