Как передать данные из адаптера в основное состояние при нажатии кнопки - PullRequest
0 голосов
/ 19 марта 2020

Я загрузил некоторые данные в sqlite, и я хочу удалить все данные из окна реселлера, нажав кнопку. так как я могу этого достичь? Я хочу, чтобы получить идентификатор из переработчика просмотра в основной деятельности, а затем удалить данные из sqlite и обновить reyclerview. Как мне этого добиться?

MainActivity

public class SelectedProblems extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_selecte_problem_drawal);

    mRecyclerView = (RecyclerView) findViewById(R.id.selected_problem_recycle_view); // Instantiate Recyclerview
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(linearLayoutManager);
    mRecyclerView.setHasFixedSize(true);
    selectedProblemsAdapter = new SelectedProblemsAdapter(this, getAllItems(),  listener);
    mRecyclerView.setAdapter(selectedProblemsAdapter);

   Button remove = findviewbyid(R.id.remove);
   remove.setOnClickListener(new View.OnClickListener() {

   }
  }

Выбранные проблемыAdapter

  public SelectedProblemsAdapter(Context context, Cursor cursor, OnItemClick listener) {
        this.context = context;
        this.cursor = cursor;
        listener = listener;

    }

    @Override
    public SelectedProblemsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.activity_selected_problems_content, null, false);
        return new SelectedProblemsViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull SelectedProblemsViewHolder holder, final int position) {
        if (this.cursor != null){
        }
    }


    @Override
    public int getItemCount() {
        return (null != cursor ? cursor.getCount(): 0);
    }

    public void update(Cursor cursor) {
        this.cursor = cursor;
        notifyDataSetChanged();
    }

    class SelectedProblemsViewHolder extends RecyclerView.ViewHolder{

        TextView selectedProblems, selectedProblemPrice, selectedProblemTotal;
        Button remove_problem_from_cart;
        public SelectedProblemsViewHolder(View itemView) {
            super(itemView);
            selectedProblems = itemView.findViewById(R.id.selected_problems);
            selectedProblemPrice = itemView.findViewById(R.id.selected_problem_price);
            selectedProblemTotal = itemView.findViewById(R.id.selectedProblemTotal);
            remove_problem_from_cart = itemView.findViewById(R.id.remove_problem_from_cart);

        }
    }
}

1 Ответ

0 голосов
/ 19 марта 2020

MainActivity

public class SelectedProblems extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_selecte_problem_drawal);

    mRecyclerView = (RecyclerView) findViewById(R.id.selected_problem_recycle_view); // Instantiate Recyclerview
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(linearLayoutManager);
    mRecyclerView.setHasFixedSize(true);
    selectedProblemsAdapter = new SelectedProblemsAdapter(this, getAllItems(),  listener);
    mRecyclerView.setAdapter(selectedProblemsAdapter);

   Button remove = findviewbyid(R.id.remove);
   remove.setOnClickListener(new View.OnClickListener() {

   }
  }

Выбранные проблемы Adapter

 public SelectedProblemsAdapter(Context context, Cursor cursor, OnItemClick listener) {
        this.context = context;
        this.cursor = cursor;
        listener = listener;

    }

    @Override
    public SelectedProblemsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.activity_selected_problems_content, null, false);
        return new SelectedProblemsViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull SelectedProblemsViewHolder holder, final int position) {
        if (this.cursor != null){
           // it will call your interface method which is implemented in Activity
           holder.yourView_name.setOnClickListener(new View.OnClickListener() {
           listener.yourmethod();
   }
        }
    }


    @Override
    public int getItemCount() {
        return (null != cursor ? cursor.getCount(): 0);
    }

    public void update(Cursor cursor) {
        this.cursor = cursor;
        notifyDataSetChanged();
    }

    class SelectedProblemsViewHolder extends RecyclerView.ViewHolder{

        TextView selectedProblems, selectedProblemPrice, selectedProblemTotal;
        Button remove_problem_from_cart;
        public SelectedProblemsViewHolder(View itemView) {
            super(itemView);
            selectedProblems = itemView.findViewById(R.id.selected_problems);
            selectedProblemPrice = itemView.findViewById(R.id.selected_problem_price);
            selectedProblemTotal = itemView.findViewById(R.id.selectedProblemTotal);
            remove_problem_from_cart = itemView.findViewById(R.id.remove_problem_from_cart);

        }
    }
}
...