2012-10-12 3 views
3

안드로이드에서 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가 자동으로 업데이트된다는 점에서 더 좋습니다. 필자는 안드로이드에서 데이터 바인딩에 대해 충분히 읽었으므로 동일한 것은 아닐 수 있지만 동일한 작업을 수행하는 가장 좋은 방법은 무엇입니까? 하나 있습니까? 데이터 바인딩이 여기 좋은 해결책이 아니더라도 안드로이드 코드를 구조화해야하는 다른 방법이 있습니까?

답변

1

프레임 워크에 구워 졌기 때문에 XAML에서이 바인딩을 얻을 수 있습니다. 앱은 바인딩 조회를 지원하는 런타임 환경에서 실행되므로 도구 모음의 일부로 Microsoft의 전체 바인딩 프레임 워크를 사용할 수 있습니다. 안드로이드는 이런 종류의 것을 가지고 있지 않습니다.

XAML 에서처럼 선언적 방식으로 바인딩을 수행하는 방법을 모르지만 비슷한 보트 (WPF/.Net/XAML 배경에서 Android로 전환됨)에 있다는 것을 발견했습니다. 보다 편리하게 만드는 창의적인 방법. 네가 그 길을 잘 지내고있는 것 같아. 비슷한 목록을 사용하는 목록이나 그리드에 사용자 정의 어댑터를 사용합니다 ... xaml 바인딩처럼 편리하지는 않지만 여전히 멋집니다.

나는 당신의 코드에서만 가정을 할 수 있도록 UI를 보지 못했지만, ListView와 사용자 지정 어댑터로 수행중인 작업 (LinearLayout 내의 Button)을 수행 할 수 있다고 가정 할 수 있습니다. 아마도 가장 고전적인 Android 개발자 동영상은 과거 Google I/O의 ListView World입니다. 몇 년됐지만 여전히 훌륭한 시계이며 여전히 관련이 있습니다.

https://www.youtube.com/watch?v=wDBM6wVEO70