안드로이드에서 Sections의 ArrayList가 있습니다 (Section 클래스가 있으므로 String의 ArrayList가 아닙니다). 각 섹션을 버튼으로 표현하고 싶습니다.Windows Phone 7/.NET과 같은 데이터 바인딩?
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 : 현재, 나는 각각의 특정 섹션에 따라 달라질 속성을 각 섹션을 반복 section.xml을 팽창하고 동적으로 추가하여 달성하고
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" />
이 방법은 정상적으로 작동하지만 더 나은 해결책이 될 것 같습니다. 다음은 Windows Phone 7 용 .NET의 예입니다. XAML에 바운드 (SectionList, ObservableCollection)를 지정하고 컬렉션의 각 항목을 표현하는 방법에 대한 템플릿을 제공합니다.
<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>
단순함과 SectionList의 내용을 변경하면 UI가 자동으로 업데이트된다는 점에서 더 좋습니다. 필자는 안드로이드에서 데이터 바인딩에 대해 충분히 읽었으므로 동일한 것은 아닐 수 있지만 동일한 작업을 수행하는 가장 좋은 방법은 무엇입니까? 하나 있습니까? 데이터 바인딩이 여기 좋은 해결책이 아니더라도 안드로이드 코드를 구조화해야하는 다른 방법이 있습니까?