2012-12-18 3 views
0

나는 안드로이드에서 간단한 응용 프로그램을 개발 중입니다. 나는 목록으로 표시하고 싶은 카테고리의리스트를 가지고있다. (그리고 이것은 멀리 떨어져있다.) 클릭 할 때, 어떤 함수로 다른 활동을 시작한다. ... 음, 상황 :사용자 정의 어댑터를 사용하여 listView에서 어느 값으로 이동해야합니까?

List<categorie> values = datasource.getAllCategorie(); 

     // Use the SimpleCursorAdapter to show the 
     // elements in a ListView 
     ArrayAdapter<categorie> adapter = new ArrayAdapter<categorie>(this, 
      R.layout.single_list_item, values); 
     setListAdapter(adapter); 

여기에 데이터 소스에서 I 선언 어디 getAllCategorie된다 :

내 single_list_item.xml : 나는 어댑터와 레이아웃을 호출 할 경우

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <!-- Name Label --> 
    <TextView android:id="@+id/category_label" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"   
      android:textStyle="bold" 
      android:paddingTop="10dip" 
      android:paddingBottom="10dip" 
      android:textColor="#43bd00"/> 
    <!-- Description Label --> 

</LinearLayout> 

여기에 주요 활동으로는

마지막으로 categorie.class: 나는 응용 프로그램을 실행하면 목록보기를 시작하는 동안

public class categorie { 
     private long id; 
     private String nome; 
     private long preferita; 

     public long getId() { 
     return id; 
     } 

     public void setId(long id) { 
     this.id = id; 
     } 

     public String getNome() { 
     return nome; 
     } 

     public void setNome(String nome) { 
     this.nome = nome; 
     } 



     public long getPreferita() { 
     return preferita; 
    } 

    public void setPreferita(long preferita) { 
     this.preferita = preferita; 
    } 

    // Will be used by the ArrayAdapter in the ListView 
     @Override 
     public String toString() { 
     return nome; 
     } 
    } 

는 현재, 그것은 정지

, 그것은 완전히 비어 있습니다. 이름, ID 또는 "좋아요"(categorie.class에서 Preferita를 가져오고 설정)와 같은 각 범주 요소를 어디에 둘 것인지 지정하고 싶습니다.

ArrayAdapter<categorie> adapter = new ArrayAdapter<categorie>(this, 
      android.R.layout.simple_list_item_1, values); 
     setListAdapter(adapter); 

모든 이유 OK 기본 레이아웃을 사용하고 있습니다 ... 마술 확인했다 : 내가 시작했을 때

는 어댑터 부분입니까? 다시, 어디로 가는지 어디에서 지정해야합니까?

미리 감사드립니다.

답변

1

간단한 목록을 구현하려는 경우 ArrayAdapter을 사용하는 데 문제가 없습니다. Android는 어댑터에 제공 한 객체를보고 해당 객체의 toString()을 호출하고 TextView[email protected]:id/text1으로 채우고 String으로 채 웁니다. 그 레이아웃이 포함되어 있기 때문에 [email protected]:id/text1

당신이 당신의 ListView의 각 행의 레이아웃을보다 효율적으로 관리를 원하는 경우에, 당신은 당신이 특정 Views에 값을 매핑 할 수있는 SimpleAdapter를 사용할 수있는 TextView 당신이 android.R.layout.simple_list_item_1을 사용하는 경우 그것은 "마술"일 귀하의 행 레이아웃. 또는 ArrayAdapter을 확장하여 고유 한 어댑터 클래스를 작성할 수 있습니다. 자신의 어댑터를 작성하는 것이 좋으며, 목록 작성 방법에 대한 통찰력이 있습니다. Here's 시작하는 방법에 대한 안내서입니다.

관련 문제