将自定义ListView添加到“警报"对话框
问题描述:
我也想添加自定义列表视图一个警报对话框,当我从列表中选择一个项目并单击(setPositiveButton)对话框关闭并且文本更改成功时,它仍然可以正常工作,但是当我再次单击乘客按钮以查看时或更改选择导致应用程序不幸停止
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();
}
});
答
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;
}
}