2

次のコードを使用してリストダイアログを作成しようとしています

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

            }
        })
    .create();

しかし、次の形式でデータを表示します

ここに画像の説明を入力

でもこういうの作りたい

ここに画像の説明を入力

これを達成するには、カスタムダイアログを作成して、その中にリストビューをロードする必要がありますか?

4

4 に答える 4

9

setItems() を使用する代わりに、setAdapter() を使用してください。例:

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 の代わりに独自のレイアウトを使用できます。

于 2013-03-03T22:41:46.283 に答える
2
        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" />

これがお役に立てば幸いです。

于 2014-05-12T08:11:49.397 に答える
1

はい、カスタムダイアログを作成し(http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialogを参照)、setViewを使用してListViewを追加する必要があります。それ以外の場合、ダイアログで使用している標準のリスト項目を変更する方法はないと思います。

于 2012-02-17T13:38:13.463 に答える
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();
于 2012-02-17T13:54:45.467 に答える