Adding custom ListView to Alert dialog

流过昼夜 提交于 2020-01-06 04:04:23

问题


I want to add custom list view too a alert dialog it works fine when i select a item from list and click the (setPositiveButton) the dialog close and the text is changed successful but when i click again on the passenger button to view or for change selection than application stops unfortunately

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

回答1:


Please try these for setting list view inside alert dialog

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

i think it help full for you.




回答2:


 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;
    }
}



回答3:


You can try this

Create costom adapter by extending the class as ArrayAdapter(its in your case)

Override getView() function(also inflate the view).

After that you can directly set adapter by using

 builder.setAdapter(costomAdapter);


来源:https://stackoverflow.com/questions/37460109/adding-custom-listview-to-alert-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!