문제

I am running into an issue and don't know the best way to use a set of drawables inside an AlertDialog. The AlertDialog is presented when a user presses a button and is prompted to choose from a list of drawables.

Currently it shows Buttons I have placed in a dialog_view layout when the dialog is created. Ideally I would like to be able to use some type of listview, but if that is not possible I would like to be able to handle the selection/pressing of an item when inside the AlertDialog.

What should I do to handle any actions from within the AlertDialog?

DIALOGFRAGMENT

public class PicturePickerFragment extends DialogFragment {

ArrayList<Integer> imageList = new ArrayList<Integer>();

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // fill an array with selected images

    String title = "Picture";

    imageList.add(R.drawable.barbershop);
    imageList.add(R.drawable.wedding);
    imageList.add(R.drawable.meeting);
    imageList.add(R.drawable.barbershop);

    // return alertdialog
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog_view, null))
            .setTitle(R.string.event_type)
            .setPositiveButton(R.string.select_picture,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // call the method on the parent activity when
                            // user click the positive button
                        }
                    });

    return builder.create();
}

}

DIALOG VIEW

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_baby"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/baby"
        android:text="Baby Shower" />

    <Button
        android:id="@+id/btn_baking"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/baking"
        android:text="Baking" />

    <Button
        android:id="@+id/btn_barber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/barbershop"
        android:text="Barbershop" />
</LinearLayout>

도움이 되었습니까?

해결책

You can use some ListAdapter such as an ArrayAdapter where you present a drawable for every item in the list and then use the adapter when you build the AlertDialog:

builder.setAdapter(myAdapter, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //which is the position of the item clicked
        }
    })

Edit: This is an example that would work with your imageList:

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setAdapter(new ArrayAdapter<Integer>(getActivity(), R.layout.dialog_image, imageList) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view;
                if (convertView == null) {
                    LayoutInflater inflater = getActivity().getLayoutInflater();
                    view = inflater.inflate(R.layout.dialog_image, parent, false);
                } else {
                    view = convertView;
                }
                ImageView imageView = (ImageView) view.findViewById(R.id.image);
                int resId = getItem(position);
                imageView.setImageResource(resId);
                return view;
            }
        }, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.i("Dialog", "selected position " + which);
            }
        });
        return builder.create();

where R.layout.dialog_image contains an ImageView with id android:id="@+id/image" as root element. For example:

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/image"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:scaleType="center"
 android:paddingLeft="16dp"
 android:paddingRight="16dp"
 android:minHeight="?attr/listPreferredItemHeight" >
</ImageView>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top