如何在android中为AlertDialog添加自定义列表视图?

时间:2016-08-11 16:41:20

标签: android user-interface android-alertdialog

到目前为止,这是我的AlertDialog的代码:

public class FriendOptionsDialogFragment extends DialogFragment {

    public static FriendOptionsDialogFragment newInstance(String firstName) {
        FriendOptionsDialogFragment options = new FriendOptionsDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("firstName", firstName);
        options.setArguments(bundle);
        return options;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_friend_options, null);
        Bundle extras = getArguments();
        String friendName = extras.getString("firstName");
        FriendOptionsAdapter adapter = new FriendOptionsAdapter(getActivity(), friendName);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setCancelable(true);
        builder.setView(view);
        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        return builder.create();
    }
}

我想将自定义适配器和布局的列表视图添加到AlertDialog中。我在AlertDialog.Builder对象上使用方法setAdapter并在堆栈跟踪中获得以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
    at com.hb.birthpay.adapter.FriendOptionsAdapter.getView(FriendOptionsAdapter.java:67)

这是我的自定义适配器中发生错误的行:

holder.friendOptionImageView.setImageResource(mFriendOptionImageId[position]);

我可能做错了,但我需要知道如何将自定义列表视图添加到AlertDialog。

1 个答案:

答案 0 :(得分:0)

AlertDialog.Builder builder = new AlertDialog.Builder(context);

final View customView = View.inflate(context, R.layout.custom_dialog_view, null);
builder.setView(customView);
///builder.setCancelable(false);           //====> Disable the outside touch from the dialog window.

final AlertDialog backDialog = builder.create();

backDialog.setTitle(....string...);

backDialog.setButton(AlertDialog.BUTTON_NEGATIVE, getResources().getString(R.string.text_cancel),
new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        backDialog.cancel();
    }
});
backDialog.setCanceledOnTouchOutside(false);
backDialog.show();

CustomAdapter adapter = new CustomAdapter(context, ...arrayValues...);

ListView listView = (ListView) customView
        .findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
    int location, long id) {

         // handle list view item click.
        //you can access values using parent.getItemAtPosition(location).toString());                                             

        backDialog.dismiss();
    }
});

适配器类:

public class CustomAdapter extends ArrayAdapter<String> {

public CustomAdapter(Context context, String[] arrayValues) {
    super(context, android.R.layout.simple_list_item_1);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
    }

    String value = getItem(position);

    //do your work.

    return super.getView(position, convertView, parent);
}}

custom_dialog_view:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <ListView
        android:id="@+id/list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="@dimen/dimen_10"
        android:layout_marginRight="@dimen/dimen_10"
        android:cacheColorHint="@android:color/transparent"
       >
    </ListView>

</RelativeLayout>

希望这会有所帮助...... :)谢谢。