2011-01-29 2 views
0

J'ai N "livres" chacun avec un certain nombre de chapitres. Donc, je crée un ListView. Chaque élément du ListView comporte deux parties: TextView pour le nom et Spinner pour lister les chapitres.Android: un spinner dans une vue de liste, Où est l'événement OnClick/OnItemClick/OnItemSelected?

J'ai créé un adaptateur personnalisé pour le ListView - BookAdapt, et un adaptateur personnalisé pour le Spinner - ChapAdapt. Je l'ai affichage, et de travail, mais je ne peux pas savoir comment ajouter un clic/événement sélectionné.

Voici la liste des ressources

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Load Chapter" /> 
<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <Button 
     android:text="Load" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <Button 
     android:text="Cancel" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
</LinearLayout> 

<ListView 
    android:id="@+id/chapview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/>  

Voici l'affichage de chaque livre

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<TextView 
    android:id="@+id/viewbook" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"/> 
<Spinner 
    android:id="@+id/chapspinner" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:prompt="@string/chapter_prompt" 
    android:layout_weight="1"/> 

Voici les éléments clés de la BookAdapt

class BookAdapt extends ArrayAdapter { 
     BookAdapt(Context context, int txtres, BookHolder[] bks) { 

    super(context, txtres); 
     mContext = context; 
     books = bks; 
     mInflater = LayoutInflater.from(context); 
     } 

     public View getView(int pos, View convert, ViewGroup parent) { 
     BookHolder book = books[ pos]; 
     String bkname = book.getName(); 
     if (convert == null) { 
     convert = mInflater.inflate(R.layout.bookview, null); 
     book.text = (TextView) convert.findViewById(R.id.viewbook); 
     Spinner sp = (Spinner)convert.findViewById(R.id.chapspinner); 
     ChapAdapt ch = new ChapAdapt(book, mContext); 
       sp.setAdapter(ch); 
     //convert.setClickable(true); 
     convert.setTag(book); 
     } else { 
     book = (BookHolder)convert.getTag(); 
     } 
     book.text.setText(bkname); 
     return convert; 
     } 

Les ChapAdapt attribuent simplement un TexTView.

Répondre

0

Le Spinner lui-même implémente le OnClicklistener, de sorte qu'il peut apparaître et vous permettre de sélectionner l'élément respectif. Après la sélection Spinner.onClick() devrait être appelé par le système, donc je pense que c'est l'endroit que vous recherchez. Qu'est-ce que vous essayez de faire ressemble beaucoup à un cas d'utilisation pour un ExpandableListView pour moi - peut-être que cela peut être intéressant aussi.

Questions connexes