从第一个选项卡更新第二个选项卡中的recyclerView适配器数据

时间:2016-09-18 08:26:13

标签: android android-studio

我有一个Activity,它有两个标签。每个标签都有一个RecyclerView,每个RecyclerView都有一个特定的Adapter。第一个标签RecyclerView的每个数据集中都有一个按钮,用于删除位置中的数据集并将其添加到下一个标签。我的问题是我应该从Activity退出并再次输入以查看第二个标签中添加的数据。如何立即更新第二个标签?

 public class QCRepairAdapter extends RecyclerView.Adapter<QCRepairAdapter.MyViewHolder> {

    private LayoutInflater inflater;
    List<QCInfo> data = Collections.emptyList();

    public QCRepairAdapter(Context context, List<QCInfo> data) {

        inflater = LayoutInflater.from(context);
        this.data = data;

    }

    @Override
    public QCRepairAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.cards_qc_repair, parent, false);

        MyViewHolder holder = new MyViewHolder(view);

        return holder;

    }

    public void Delete(int position) {
        data.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public void onBindViewHolder(final QCRepairAdapter.MyViewHolder holder, final int position) {

        QCInfo current = data.get(position);
        holder.projectName.setText(current.projectName);
        holder.code.setText(current.code);
        holder.operator.setText(current.operatorName);

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        TextView projectName;
        TextView code;
        TextView operator;
        ImageView btn_accept;

        public MyViewHolder(final View itemView) {
            super(itemView);
            projectName = (TextView) itemView.findViewById(R.id.tv_card_qc_repair_name);
            code = (TextView) itemView.findViewById(R.id.tv_card_qc_repair_code);
            operator = (TextView) itemView.findViewById(R.id.tv_card_qc_repair_operatorName);
            btn_accept = (ImageView) itemView.findViewById(R.id.iv_card_qc_repair_accept);
            btn_accept.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {

                showDialog(v.getContext(), "تایید فرآیند ؟", "qcAccept", code.getText().toString(), getPosition());

        }
    }

    private void showDialog(final Context context, String message, final String method, final String code, final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("هشدار")
                .setIcon(R.drawable.alert)
                .setMessage(message)
                .setPositiveButton("بله", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        BackgroundTask backgroundTask = new BackgroundTask(context);
                        backgroundTask.execute(method, code);
                        Delete(position);
                    }
                })
                .setNegativeButton("خیر", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .create().show();
    }
}

0 个答案:

没有答案