java - 如何在适配器类中删除和设置对话框的 View ?

标签 java android android-recyclerview android-alertdialog adaptor

收到错误: 指定的子项已经有父项。您必须首先在子级的父级上调用removeView()。

问题:因为我重复调用警报生成器,所以首先我必须删除 View ,然后需要再次设置 View 。

问题:我不知道如何删除和设置适配器中的 View 。

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

}

最佳答案

您正在调用 alert.setView 2 次。这就是问题所在。

关于java - 如何在适配器类中删除和设置对话框的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41428230/

相关文章:

java - 我的重绘函数没有调用我的paintComponent

java - 对象数组与索引列表+数组

android - 在 Prism 导航页面的工具栏中添加搜索栏

android - 如何在 Jetpack 可组合测试中获取字符串资源

android - Fresco:滚动 RecyclerView 后图像消失

android - 加载数据时 RecyclerView 滚动到底部

java - 如何在android中获取recyclerView项目的X和Y坐标?

java - 使用 thymeleaf 显示文本,就像在文本文件.txt 中显示的一样

java - 如何在菜单中查找菜单项的位置(以坐标表示)

Android:对话框中单选组的空指针异常