Pergunta

I am trying to create List Dialog using following code

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

            }
        })
    .create();

But it shows data in a following form

enter image description here

But i want to create something like this

enter image description here

so to achieve this do i need to create custom dialog and load a listview inside it ?

Foi útil?

Solução

Instead of using setItems(), use setAdapter(). Example:

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){

You can use your layout in the place of R.layout.listrowlay.

Outras dicas

In my case I am creating a custom adapter for ListView..

This is the sample code :

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

Yes you have to create a custom dialog (see http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog) and use setView to add the ListView. I don't think there's any way to change the standard list item they are using in the dialog otherwise.

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

THis is list row xml file

<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" />

I hope this is usefor you and give me 1+

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top