2010-05-20 2 views
32

j'ai créé une classe de dialogue personnaliséeest-il possible de créer une liste de contrôle dans une boîte de dialogue?

public class NewPost extends Dialog 
{ 
// functionality 

} 

maintenant mon exigence est de créer listview à l'intérieur. Je sais que nous pouvons créer des boîtes de texte, des boutons, une liste déroulante à l'intérieur.

mais dans le but de créer de liste, nous devrions hériter notre classe de classe ListActivity

ce que vous proposez est-il possible ou non si oui, alors comment y parvenir en utilisant une interface ou quoi?

Répondre

11

Vous n'avez pas vraiment devez étendre listActivity afin d'utiliser les listes.

L'extension listActivity vous donnera certaines fonctionnalités gratuitement, comme getListView() (si je me souviens du nom de la méthode correctement), mais qui peut tout aussi bien être fait manuellement avec findViewById() comme tout autre point de vue.

+0

coincé dans la même quelqu'un peut-il aider à http://stackoverflow.com/questions/29446088/how-to-get- spinner-values-in-textview/29487383? noredirect = 1 # comment47175570_29487383 –

52

Oui.

Vous pouvez toujours utiliser ListView dans une boîte de dialogue. Et vous n'avez certainement pas besoin de ListActivity pour créer ListView.

code

peut être quelque chose comme ceci:

Dialog dlg = new Dialog(context); 
LayoutInflater li = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View v = li.inflate(R.layout.my_layout, null, false); 
dlg.setContentView(v); 
dlg.show(); 

my_layout.xml:

<ScrollView xmlns:android="blah" 
    android:id="xid" 
    android:layout_height="h" 
    android:layout_width="w"> 

    <ListView blah blah blah attributes 
    /> 

</ScrollView> 
+2

Comment remplir la liste dans la boîte de dialogue ici? – lathomas64

+5

'ListView v = (ListView) dlg.findViewById (R.id_list_view_in_your_my_layout);' Ensuite, créez un adaptateur et définissez l'adaptateur. – Enigma

+57

Bonne idée dans l'ensemble, mais [mettre un listView à l'intérieur d'un scrollView n'est pas recommandé] (http://stackoverflow.com/questions/6210895/listview-inside-scrollview-is-not-scrolling-on-android). – greg7gkb

63

cette mise en œuvre ne nécessite pas de faire des mises en page XML. il a été écrit comme une déclaration de cas prioritaire « onCreateDialog », mais vous pouvez adapter à vos besoins si facilement:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("Select Color Mode"); 

ListView modeList = new ListView(this); 
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" }; 
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray); 
modeList.setAdapter(modeAdapter); 

builder.setView(modeList); 
final Dialog dialog = builder.create(); 

dialog.show(); 
+0

J'espérais utiliser ceci pour choisir une sélection dans une boîte de dialogue, mais quand j'ai ajouté un onItemClickListener je n'ai pas pu accéder au dialogue.dismiss() depuis onItemClick. Je suis sûr que quelqu'un de plus intelligent que moi peut accrocher ça mais attention, cette réponse a besoin de plus de code pour être utile. – Jeff

+0

Déclarez simplement la variable de dialogue en tant que private en haut de votre classe, puis appelez dialog.dismiss() dans l'événement on click. Travaillé pour moi – Nick

+3

Jeff - c'est parce que ListView.OnItemClickListener ne vous donne pas le paramètre de dialogue de la manière que DialogInterface.OnMultiChoiceClickListener fait. déclarer que le dialogue est définitif devrait prendre soin de cela et vous permettre d'accéder à la boîte de dialogue dans votre override. hth. – moonlightcheese

0

Vous pouvez utiliser une mise en page pour les boîtes de dialogue d'alerte. Si vous voulez un listview je le ferais comme here

3

La façon la plus simple possible:

ListView listView = new ListView(this); 
    listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] {"item 1", "item 2", "item 3"})); 
    Dialog dialog = new Dialog(this); 
    dialog.setContentView(listView); 
    dialog.show(); 
3

Vous pouvez créer un dialogue personnalisé avec cette mise en page:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical|center_horizontal" 
    android:background="@drawable/dialogs"> 
    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="20dip" 
     android:paddingRight="20dip" 
     android:paddingTop="10dip"> 
     <ImageView 
      android:id="@+id/dialog_title_image" 
      android:layout_alignParentLeft="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/info"/> 
     <TextView 
      android:id="@+id/dialog_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dip" 
      android:layout_marginTop="20dp" 
      android:layout_centerInParent="true" 
      android:text="Title" 
      android:layout_toRightOf="@id/dialog_title_image" 
      android:textColor="@android:color/black" 
      android:textSize="20sp"/> 

     <!-- Lista --> 
    </RelativeLayout> 
    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="3dip" 
      android:background="#1e90ff"/> 
    <ListView 
      android:id="@+id/component_list" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    <!-- Fine lista --> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="10dip" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:gravity="bottom|center_horizontal" 
     android:paddingBottom="20dip"> 
     <Button 
      android:id="@+id/positive_button" 
      android:layout_alignParentLeft="true" 
      android:layout_width="120dip" 
      android:layout_height="wrap_content" 
      android:background="#1e90ff" 
      android:textColor="@android:color/white" 
      android:text="@string/close"/> 

    </RelativeLayout> 
</LinearLayout> 

créer une mise en page personnalisée aussi pour chaque article si vous voulez:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingTop="10dp" 
    android:paddingBottom="10dp" 
    android:paddingLeft="10dp" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Large Text" 
     android:textStyle="bold" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 


    <TextView 
     android:id="@+id/subtitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Small Text" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 


</LinearLayout> 

puis utilisez un arraylist pour remplir la liste et définir la vue:

 //creation and population of the list 
     List<Component> my_list = new ArrayList<Component>(); 
     createComponents(); 

     //adapter 
     array_adapter = new ComponentAdapter(context, R.layout.component,my_list); 

     //button to show the dialog 
     Button button = (Button)findViewById(R.id.button1); 

     button.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       list_dialog = new Dialog(context); 
       list_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
       list_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
       list_dialog.setContentView(R.layout.list_dialog); 

       ListView list = (ListView)list_dialog.findViewById(R.id.component_list); 
       list.setAdapter(array_adapter); 

       Button positiveButton = (Button) list_dialog.findViewById(R.id.positive_button); 

       positiveButton.setOnClickListener(new OnClickListener(){ 

        @Override 
        public void onClick(View arg0) { 

         list_dialog.dismiss(); 
        } 
       }); 

       list_dialog.show();  
      } 
     }); 


    } 

Références: http://pillsfromtheweb.blogspot.it/2014/10/android-listview-inside-alertdialog.html#links

Résultat: enter image description here