RecyclerView正在复制警报对话框结果

时间:2018-08-16 17:18:38

标签: android android-fragments android-recyclerview alertdialog recycler-adapter

我有一个片段,该片段想在我的RecyclerView中显示警报对话框的结果。每次我在对话框中单击“添加”时,它会将重复的结果添加到我的RecyclerView中。我已经搜索了很多,但似乎找不到我做错了什么。我试过添加.clear();但是,如果我添加了它,则RecyclerView中什么也不会显示。我在适配器中添加了getItemId和getItemViewType以返回位置;但项目仍会重复。我添加了adapter.setData(model),然后添加了adapter.notifyDataSetChange();而且我的RecyclerView仍然显示重复的项目。该应用程序运行,因此我没有要发布的日志。谢谢。

Screenshot: duplicate items

模型

public class SubjectsModel
{

    //private long id;
    private String mTitle;
    private String mTeacher;

    public String getmTitle()
    {
        return mTitle;
    }

    public void setmTitle(String title)
    {
        this.mTitle = title;
    }

    public String getmTeacher()
    {
        return mTeacher;
    }

    public void setmTeacher(String teacher)
    {
        this.mTeacher = teacher;
    }
}

片段

public class SubjectsFrag extends DialogFragment implements
        SubjectsEditor.OnAddSubjectListener
{
    private static final String TAG = SubjectsFrag.class.getSimpleName();

    @NonNull
    Context context;
    private EditText titleView, teacherView;
    private String sTitle, sTeacher;
    public EmptyRecyclerView recyclerView;
    public RecyclerView.LayoutManager layoutManager;
    public RecyclerSubAdapter recyclerSubAdapter;
    public ArrayList<SubjectsModel> subMod = new ArrayList<>();
    DbHelper helper;
    @BindView(R.id.main_root)
    ViewGroup root;

    public SubjectsFrag() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_subjects, container, false);

        FloatingActionButton fab = view.findViewById(R.id.fab_sub);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();
            }
        });

        helper = new DbHelper(getActivity());
        helper.getSubject();

        titleView = view.findViewById(R.id.edit_subject);
        teacherView = view.findViewById(R.id.edit_subject_teacher);

        View emptyView = view.findViewById(R.id.empty_subject_view);

        recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);


        recyclerSubAdapter = new RecyclerSubAdapter(getContext(), subMod);

        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(recyclerSubAdapter);

        return view;
    }

    @Override
    public void OnAddSubjectSubmit(String title, String teacher)
    {
        SubjectsModel model = new SubjectsModel();

        model.setmTitle(title);
        model.setmTeacher(teacher);

        //subMod.clear();
        subMod.add(model);
        recyclerSubAdapter.setData(subMod);

        recyclerSubAdapter.notifyDataSetChanged();

    }

    private void showDialog()
    {
        SubjectsEditor addSubjectDialog = new SubjectsEditor();
        addSubjectDialog.setTargetFragment(this, 0);
        addSubjectDialog.show(getFragmentManager(), null);

    }
}

适配器

public class RecyclerSubAdapter extends RecyclerView.Adapter<RecyclerSubAdapter.ViewHolder>
{
    private static final String TAG = RecyclerSubAdapter.class.getSimpleName();

    public List<SubjectsModel> subMod = new ArrayList<>();
    private OnItemClicked onClick;
    static ClickListener clickListener;
    Context context;
    DbHelper helper;

    public RecyclerSubAdapter(Context context, ArrayList<SubjectsModel> subMod)
    {
        this.context = context;
        this.subMod = subMod;
        this.helper = new DbHelper(context);
    }

    @NonNull
    @Override
    public RecyclerSubAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
    {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.subjects_item_list, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerSubAdapter.ViewHolder holder, final int position)
    {
       SubjectsModel currentSubject = subMod.get(position);

        holder.titleView.setText(currentSubject.getmTitle());
        holder.teacher.setText(currentSubject.getmTeacher());

        //helper.addClass(subMod.get(position));
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements
            View.OnClickListener
    {
        TextView titleView;
        TextView teacher;
        CardView cardView;

        public ViewHolder(View itemView)
        {
            super(itemView);
            titleView = itemView.findViewById(R.id.subject_subject);
            teacher = itemView.findViewById(R.id.subject_teacher_text);
            cardView = itemView.findViewById(R.id.card_view);

        }

        @Override
        public void onClick(View view)
        {
            if (clickListener != null)
            {
            }
        }
    }

    @Override
    public int getItemCount()
    {
        if (subMod == null)
        {
            Log.d(TAG, "sub is null");
        }
        return subMod.size();
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public int getItemViewType(int position)
    {
        return position;
    }

    public interface OnItemClicked
    {
        void onItemClick(int position);
    }

    public void setOnClick(OnItemClicked onClick)
    {
        this.onClick = onClick;
    }

    public void setClickListener(ClickListener clicked)
    {
        RecyclerSubAdapter.clickListener = clicked;
    }

    public interface ClickListener
    {
        void itemClicked(SubjectsModel model, int position);
    }

    public void setData(ArrayList<SubjectsModel> data)
    {
            this.subMod = data;
            //this.subMod.clear();
            this.subMod.addAll(data);
            notifyDataSetChanged();
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_root">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.example.ashleighwilson.schoolscheduler.adapter.EmptyRecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="60dp"/>

    </LinearLayout>

   <RelativeLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true">

    <TextView
        android:id="@+id/empty_subject_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:visibility="gone"
        android:text="@string/no_subjects"/>

   </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_sub"
        style="@style/FAB" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

OnAddSubjectSubmit()中,您致电subMod.add(model);
然后您呼叫recyclerSubAdapter.setData(subMod);,后者依次呼叫this.subMod.addAll(data);
自己检查一下。我相信您可以在其中两次添加新项目。
建议:将recyclerSubAdapter.setData(subMod);中的OnAddSubjectSubmit()注释掉。

答案 1 :(得分:0)

mailMessage.To.Add("amir_khan@enter.com, walter_white@yahoo.com");

您通过向 this.subMod = data; this.subMod.addAll(data); 分配subMod来初始化它,然后再次添加数据:   data