Android 是否可以在对话框内创建列表视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2874191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:40:45  来源:igfitidea点击:

is it possible to create listview inside dialog?

androidlistviewdialog

提问by UMAR

i have created a custom dialog class

我创建了一个自定义对话框类

public class NewPost extends Dialog
{
// functionality

}

now my requirement is to create listview inside it. i know we can create textboxes,buttons,dropdown list inside it.

现在我的要求是在其中创建列表视图。我知道我们可以在其中创建文本框、按钮、下拉列表。

but in order to create list view we should inherit our class from listActivity class

但是为了创建列表视图,我们应该从 listActivity 类继承我们的类

what you suggest is it possible or not if yes then how to achieve this using any interface or what?

你的建议是可能还是不可能,如果是,那么如何使用任何接口或什么来实现这一目标?

采纳答案by dxh

You don't reallyhave to extend listActivityin order to use listviews.

实际上不必扩展listActivity即可使用列表视图。

Extending listActivitywill give you some functionality for free, such as getListView()(if I recall the method name correctly), but that can just as well be done manually with findViewById()just as any other view.

扩展listActivity将免费为您提供一些功能,例如getListView()(如果我没有记错方法名称),但这也可以findViewById()像任何其他视图一样手动完成。

回答by moonlightcheese

this implementation doesn't require you to make any xml layouts. it was written as a case statement in "onCreateDialog" override, but you can adapt if for your purposes very easily:

此实现不需要您进行任何 xml 布局。它是在“onCreateDialog”覆盖中作为 case 语句编写的,但是您可以很容易地根据您的目的进行调整:

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();

because you are making a dialog with only a ListView, you set the onItemClickListener of the ListView, as there isn't one for the basic dialog class.

因为您正在创建一个只有 ListView 的对话框,所以您设置了 ListView 的 onItemClickListener,因为基本对话框类没有一个。

modeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            switch(i) {
                case 0:
                    //do something for first selection
                    break;
                case 1:
                    //do something for second selection
                    break;
            }
            dialog.dismiss();
        }
    });

回答by Gaurav Vaish

Yes.

是的。

You can always use a ListView inside a Dialog. And you definitely don't necessarily need ListActivity to create ListView.

您始终可以在 Dialog 中使用 ListView。而且您绝对不需要 ListActivity 来创建 ListView。

Code may be something like this:

代码可能是这样的:

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:

my_layout.xml:

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

  <ListView blah blah blah attributes
  />

 </ScrollView>

回答by konmik

The simplest possible way:

最简单的方法:

    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();

回答by SegFault

You can create a custom dialog with this layout:

您可以使用此布局创建自定义对话框:

<?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>

create a custom layout also for each item if you want:

如果需要,还可以为每个项目创建自定义布局:

<?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>

and then use an arraylist to populate the list and set the view:

然后使用一个数组列表来填充列表并设置视图:

        //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();     
            }
        });


    }

References: http://pillsfromtheweb.blogspot.it/2014/10/android-listview-inside-alertdialog.html#links

参考资料:http: //pillsfromtheweb.blogspot.it/2014/10/android-listview-inside-alertdialog.html#links

Result: enter image description here

结果: 在此处输入图片说明

回答by George Theodorakis

You can use any layout for alert dialogs. If you want a listview I would do it like here

您可以为警报对话框使用任何布局。如果你想要一个列表视图,我会像这里一样