将自定义ListView添加到Alert对话框

时间:2016-05-26 11:51:42

标签: android listview alertdialog

我想添加自定义列表视图也是一个警告对话框,当我从列表中选择一个项目并单击(setPositiveButton)对话框关闭并且文本更改成功但当我再次单击乘客按钮查看时,它工作正常或者更改选择而不是应用程序停止

public void onClick(View v) {               // TODO Auto-generated method stub
            AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);
            builder.setCancelable(true);
            builder.setView(v);
            builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id){
                    dialog.cancel();
                }
            });
            builder.setView(listView);
            AlertDialog dialog= builder.create();
            dialog.show();
        }
    });

3 个答案:

答案 0 :(得分:1)

请尝试使用这些来设置警告对话框中的列表视图

AlertDialog.Builder   alertdialog = new AlertDialog.Builder(context);
 LayoutInflater inflaterr = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View  viewtemplelayout= inflaterr.inflate(R.layout.product_popup, null);
    YourAdapter adap=new YourAdapter(R.layout.product_add_popup_adapter,context);
    list.setAdapter(adap);
     alertdialog.setView(viewtemplelayout);
     alertdialog.show()

我认为这对你有帮助。

答案 1 :(得分:0)

你可以试试这个

通过将类扩展为ArrayAdapter(在您的情况下为其中)来创建costom适配器

覆盖getView()函数(也会使视图膨胀)。

之后,您可以使用

直接设置适配器
 builder.setAdapter(costomAdapter);

答案 2 :(得分:0)

 AlertDialog.Builder builderSingle = new AlertDialog.Builder(YourActivity.this);

        List<Requests> requester = new ArrayList<>();
        final NameAdapter AlertDialogueAdapter = new NameAdapter(this, R.layout.your_custom_layout, requester);

        AlertDialogueBuilder.add(new Requests("Hello","StackOverF"))
        builderSingle.setIcon(R.drawable.avatar);
        builderSingle.setTitle("Select Message by,");
        builderSingle.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        });
        builderSingle.setAdapter(AlertDialogueAdapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Requests req = AlertDialogueAdapter.getItem(which);
                AlertDialog.Builder builderInner = new AlertDialog.Builder(ViewSpecialMessage.this);
                builderInner.setMessage(req.getKey());
                builderInner.setTitle(req.getName());
                builderInner.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,int which) {
                        dialog.dismiss();
                    }
                });
                builderInner.show();
            }
        });
        builderSingle.show();

public class Requests{
    private String name;
    private String key;
    public Requests(){

    }
    public Requests(String name, String key){
        this.name = name;
        this.key = key;
    }

    public String getName() {
        return name;
    }

    public String getKey() {
        return key;
    }
}
public class NameAdapter extends ArrayAdapter<Requests>{

    public NameAdapter(Context context, int resource, List<Requests> objects) {
        super(context, resource, objects);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.name_adapter_layout, parent, false);
        }
        final Requests message = getItem(position);
        TextView NameOfRequester = (TextView) convertView.findViewById(R.id.RequesterName);
        ImageView ImgOfRequester = (ImageView) convertView.findViewById(R.id.RequesterImage);
        NameOfRequester.setText(message.getName());
        return convertView;
    }
}