recycler视图:如何通知子数据集已从父视图更改?

piztneat  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(150)

我实现了一个嵌套的回收器视图。iam正在从父级更改子级中使用的列表,如下所示。如何通知子数据集已从父数据集更改?
以下是父代码段:

public void replace(Integer position,ToDoModel item)
{
    int editPos;
    TodoParent mParent = parentList.get(position);
    editPos = mParent.mChildList.indexOf(item);
    mParent.mChildList.set(editPos,item);
    //sort();
    //notifyDataSetChanged();
}

下面是iam使用的子适配器,我必须通知数据集已更改:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<ToDoModel> values;
    private Context context;
    private OnEditListener onEditListener;
    AlertDialog alertDialog;

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView txtHeader;
        public TextView txtFooter;
        public ImageView mIcon;
        public View layout;
        public ImageView deleteItem;
        public CheckBox doneButton;
        public ImageView editItem;

        public ViewHolder(View v) {
            super(v);
            layout = v;
            txtHeader = (TextView) v.findViewById(R.id.firstLine);
            txtFooter = (TextView) v.findViewById(R.id.secondLine);
            mIcon = (ImageView) v.findViewById(R.id.icon);
            deleteItem = (ImageView) v.findViewById(R.id.deleteButton);
            doneButton = (CheckBox) v.findViewById(R.id.checkBox);
            editItem = (ImageView) v.findViewById(R.id.editButton);
        }
    }

    public void sort() {
        System.out.println("Getting to sort");
        if ((int) values.size() > 1) {
            System.out.println("Getting to sort loop");
            Collections.sort(values);
        }
    }

    public void remove(int position) {
        values.remove(position);
        System.out.println("Remove");
        notifyDataSetChanged();
    }

    public MyAdapter(List<ToDoModel> myDataset) {
        values = myDataset;
        sort();
        notifyDataSetChanged();
        System.out.println("in dataset/sort");
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {

        LayoutInflater inflater = LayoutInflater.from(
                parent.getContext());
        View v =
                inflater.inflate(R.layout.todo_row_view, parent, false);

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        final ToDoModel toDoModel = values.get(position);

        holder.txtHeader.setText(toDoModel.header);
        holder.txtFooter.setText(toDoModel.task + " | " + toDoModel.tod + " | " + toDoModel.footer);

        holder.doneButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isChecked()) {
                    //holder.mIcon.setImageResource(R.drawable.ic_check_circle24px);
                    Toast.makeText(buttonView.getContext(), "Marked As Done", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(buttonView.getContext(), "Marked As UnDone", Toast.LENGTH_SHORT).show();
                }
            }
        });

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

                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                View view = LayoutInflater.from(v.getContext()).inflate(R.layout.delete_promptdialog,null);

    }

我怎样才能做到呢?如何通知数据集从子级更改为父级,反之亦然?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题