Following on from another question I have asked I have created a new class to have an AlertDialog.Builder. This is used in multi classes thoughtout my app. I am creating an object of the class in the class I am using then calling the show() method when needed. This appears correctly.
Trouble I have is as soon as I try and call the show() again I get the following Error :
java.lang.IllegalStateException: The specified child already has a parent.
You must call removeView() on the child's parent first
I am not 100% sure where I need to call this removeView or specifc view. Below is the code of the Dialog Class
public class CommentDialog {
private final Context context;
private final AlertDialog.Builder builder;
private String text;
private EditText editText;
public CommentDialog(Context context) {
this.context = context;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.comment_dialog, null);
editText = (EditText) layout.findViewById(R.id.comment_text);
editText.setText(getCommentText());
builder = new AlertDialog.Builder(context);
builder.setView(LayoutInflater.from(context).inflate(R.layout.round_comment_dialog, null));
builder.setTitle(R.string.round_comment_title);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
setCommentText(editText.getText().toString());
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.create();
}
public void setCommentText(String pCommentText) {
text = pCommentText;
}
public String getCommentText() {
return text;
}
public void show() {
builder.show();
}
}
Thanks for your Time
EDITTED
added round_comment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dip"
android:background="@drawable/border_main"
android:orientation="vertical" >
<EditText
android:id="@+id/comment_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:gravity="top|left"
android:inputType="textMultiLine"
android:lines="5" >
</EditText>
</LinearLayout>
EDITTED MK2
Within the main class I am doing the following :
above contructor
private CommentDialog myDialog;
within the oncreate()
myDialog = new CommentDialog(this);
Then on a button press :
case R.id.round_comment_button:
myDialog.setommentText(comentText);
myDialog.show();
roundComentText = myDialog.getRoundCommentText();
break;
You are better off just having your class extend the Dialog class and building it and then showing it. Of course your views would be created in the constructor of your Dialog class. Because the Dialog does not have a Positive and Negative buttons available, you will have to create your own and set anonymous listeners for your Dialog to listen to them. Everything else should be easy.