'How do I remove childview from parent layout before creating an alertdialog in android?
I am trying to create an AlertDialog after cancelling the previous alert using ad.cancel() in my code.
private void make_changes() {
if(new_spin != null){
new_spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(i != 0) {
ad.cancel();
builder.setMessage("Make your changes below:");
TextView book = new TextView(getContext());
TextView page = new TextView(getContext());
TextView type = new TextView(getContext());
TextView no = new TextView(getContext());
book.setPadding(70, 10, 10, 20);
book.setTypeface(null, Typeface.BOLD);
page.setPadding(70,10,10,20);
page.setTypeface(null,Typeface.BOLD);
type.setPadding(70,10,10,20);
type.setTypeface(null,Typeface.BOLD);
no.setPadding(70,10,10,50);
no.setTypeface(null,Typeface.BOLD);
EditText book_no = new EditText(getContext());
EditText page_no = new EditText(getContext());
EditText qtr_type = new EditText(getContext());
EditText qtrno = new EditText(getContext());
book_no.setPadding(70, 10, 10, 20);
page_no.setPadding(70,10,10,20);
qtr_type.setPadding(70,10,10,20);
qtrno.setPadding(70,10,10,50);
book.setText("Book");
page.setText("Page");
type.setText("Qtr type");
no.setText("Qtr No");
LinearLayout diagLayout = new LinearLayout(getContext());
diagLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout diagLayout1 = new LinearLayout(getContext());
diagLayout1.setOrientation(LinearLayout.HORIZONTAL);
for(int j=0; j<arr.size(); j++){
try {
if(arr.get(j).get(1).toString() == new_spin.getSelectedItem()){
book_no.setText(arr.get(j).get(0).toString());
page_no.setText(arr.get(j).get(1).toString());
qtr_type.setText(arr.get(j).get(2).toString());
qtrno.setText(arr.get(j).get(3).toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
diagLayout.addView(border1);
diagLayout1.addView(book);
diagLayout1.addView(book_no);
diagLayout1.addView(page);
diagLayout1.addView(page_no);
diagLayout1.addView(type);
diagLayout1.addView(qtr_type);
diagLayout1.addView(no);
diagLayout1.addView(qtrno);
diagLayout1.addView(border);
diagLayout.addView(diagLayout1);
builder.setView(diagLayout);
builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
book_no.setText("");
page_no.setText("");
qtr_type.setText("");
qtrno.setText("");
dialogInterface.dismiss();
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
ad = builder.create();
ad.show();
}
}
When I execute the function above, I keep getting this error on ad.show():
The specified child already has a parent. You must call removeView() on the child's parent first.
Why is this and how do I fix it?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
