How to remove and set the view of the dialog box in the adaptor class?

145 views Asked by At

Getting the error: The specified child already has a parent. You must call removeView() on the child's parent first.

Issue: Because I am calling the alert builder repeatedly so first I have to remove the view then need to set the view again.

Problem: I don't know how to remove and set the view in the adaptor.

public class ActionAdaptor extends SectionRecyclerViewAdapter<SectionHeader, Action, SectionViewHolder, ActionChildGoal> {


    Context context;
    DbHelper dbHelper;
    HealthVitalsFunction interfaceAdapter;

    public ActionAdaptor(Context context, List<SectionHeader> sectionItemList) {
        super(context, sectionItemList);
        this.context = context;
        dbHelper = new DbHelper(context);
    }


    @Override
    public SectionViewHolder onCreateSectionViewHolder(ViewGroup sectionViewGroup, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.section, sectionViewGroup, false);
        return new SectionViewHolder(view);
    }

    @Override
    public  ActionChildGoal onCreateChildViewHolder(ViewGroup childViewGroup, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.action_list, childViewGroup, false);
        return new ActionChildGoal(view);
    }

    @Override
    public void onBindSectionViewHolder(SectionViewHolder sectionViewHolder, int sectionPosition, SectionHeader section) {
        sectionViewHolder.name.setText(section.getSectionText());
    }

    @Override
    public void onBindChildViewHolder(final ActionChildGoal holder, int sectionPosition, final int childPosition, final Action action) {



        //for more options

        //When nothing is there..
        if (action.getBenefits().length()==0 && action.getSideEffects().length() == 0 && action.getRemarks().length()==0){
            holder.moreOptions.setVisibility(View.GONE);
            holder.actionSubSection.setVisibility(View.GONE);
        }else {

            String msg=" ";

            if (action.getSideEffects().length()!=0)
                msg=msg.concat("<br /> <strong>Side Effects</strong> <br /> "+action.getSideEffects());

            holder.actionSubSection.setText(Html.fromHtml(msg));

            holder.actionSubSection.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    showDialogBox("More Information ",holder.actionSubSection);
                }
            });

        }


    public void showDialogBox(String title, View text)
    {


        final AlertDialog.Builder alert=new AlertDialog.Builder(new ContextThemeWrapper(context,R.style.AlertBoxTheme));
        alert.setView(new TextView(context));

        text.setPadding(3,0,3,0);
        alert.setView(text);
        alert.setTitle(title);


        alert.setPositiveButton("READ", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.cancel();
            }
        });

        alert.show();
    }

}
2

There are 2 answers

8
Manza On BEST ANSWER

You are calling alert.setView 2 times. That's the problem.

3
kenzo On

This line is problem:

     alert.setView(text); 

You are trying to set view for alert dialog with view which is currently added in RecyclerView, you need to create new one or inflate similar from xml.