自定义android中的对话框列表

时间:2012-02-17 13:32:56

标签: android android-dialog

我正在尝试使用以下代码创建列表对话框

new AlertDialog.Builder(ContactActivity.this)
    .setTitle(contactStore.getContactName())
    .setItems(contactStore.getContactNumber(), 
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        })
    .create();

但它以下列形式显示数据

enter image description here

但我想创造像这样的东西

enter image description here

所以要实现这一点,我需要创建自定义对话框并在其中加载listview吗?

4 个答案:

答案 0 :(得分:8)

使用setAdapter()而不是使用setItems()。例如:

return new AlertDialog.Builder(getActivity())
     .setTitle(getArguments().getString("titulo"))
     .setCancelable(false)
     .setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.listrowlay, lista), 
                 new DialogInterface.OnClickListener(){
                     @Override
                     public void onClick(DialogInterface dialog, int item){

您可以在R.layout.listrowlay。

的位置使用您的布局

答案 1 :(得分:1)

是的,您必须创建自定义对话框(请参阅http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog)并使用setView添加ListView。我认为没有办法改变他们在对话框中使用的标准列表项。

答案 2 :(得分:1)

就我而言,我正在为ListView创建自定义适配器..

这是示例代码:

        dialog2 = new Dialog(SActivity.this);
        ListView modeList = new ListView(SActivity.this);
        AlertDialog.Builder builder = new AlertDialog.Builder(SActivity.this);

            builder.setTitle(" resul[s] ");
            MySimpleAdapter adapter = new MySimpleAdapter(SActivity.this, data , R.layout.list_main, 
                    new String[] { "name", "distance" ,"phone","web"}, 
                    new int[] { R.id.item_title, R.id.item_subtitle ,R.id.item_subtitle1 ,R.id.item_subtitle2});
            modeList.setAdapter(adapter);
            builder.setView(modeList);
            dialog2 = builder.create();
            dialog2.show();

答案 3 :(得分:1)

        AlertDialog.Builder builder = new AlertDialog.Builder(
            SignUpActivity.this);

    final ArrayList<String> choiceList = new ArrayList(
            Arrays.asList(getResources().getStringArray(
                    R.array.CountryCodes)));

    FontHelper.applyFont(this, findViewById(R.id.llmainview),
            "AVENIRLTSTD-BOOK_0.OTF");

    int selected = -1; // does not select anything

    builder.setAdapter(new ArrayAdapter<String>(SignUpActivity.this,
            R.layout.dialog_layout, R.id.textView1, choiceList),
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    country_code = "" + choiceList.get(which);
                    edt_country.setText("+" + country_code);
                    Log.error("vggg", "" + choiceList.get(which));
                    dialog.dismiss();
                }
            });

    AlertDialog alert = builder.create();
    StateListDrawable selector = new StateListDrawable();
    selector.addState(new int[] { android.R.attr.state_pressed }, getResources().getDrawable(android.R.color.transparent));
    selector.addState(new int[] {}, getResources().getDrawable(android.R.color.transparent));
    alert.getListView().setSelector(selector);

    alert.getListView().setCacheColorHint(
            getResources().getColor(android.R.color.transparent));
    alert.getListView().setBackgroundColor(
            getResources().getColor(android.R.color.transparent));

    alert.show();

这是列表行xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowid"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:gravity="center_vertical"
    android:paddingLeft="10dp"
    android:text="TextView"
    android:textColor="@color/rowtextcolor" />

<View
    android:id="@+id/view1"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="#9faeb6" />

我希望这对你有用,给我1 +