Как получить идентификатор элемента, по которому щелкнули, когда нажата кнопка «Удалить»? - PullRequest
0 голосов
/ 20 марта 2020

Я разрабатываю приложение android, когда пользователь нажимает кнопку на RecyclerView, чтобы удалить элемент из базы данных RecyclerView и SQLite. Однако вместо удаления элемента, по которому щелкнули, он удаляет последний элемент в RecyclerView и последний элемент в SQLite. Как мне это исправить?

Вот мой адаптер

public class SelectedProblemsAdapter extends   RecyclerView.Adapter<SelectedProblemsAdapter.SelectedProblemsViewHolder> {
    private List<SelectedProblemsModel> selectedProblems;
    public static String price;
    public static String problems;
    Context context;
    LikelyProblemAdapter.RecyclerViewClickListener mRecyclerViewClick;
    Cursor cursor;
    private Long id;
    public int problemsIndex, priceIndex, problem_id;
    private String formatted_price;
    SQLiteDatabase sqLiteDatabase;
    OnItemClick listener;
    public SelectedProblemsAdapter(Context context, Cursor cursor) {
        this.context = context;
        this.cursor = cursor;
    }
    @NonNull
    @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){
            this.cursor.moveToPosition(position);
            id = cursor.getLong(cursor.getColumnIndex(CartContract.CartEntry._ID));
            problems = cursor.getString(cursor.getColumnIndex(CartContract.CartEntry.PROBLEMS));
            price = cursor.getString(cursor.getColumnIndex(CartContract.CartEntry.PRICE));
            formatted_price = CurrencyFormatter.currencyFormat(price);
            holder.selectedProblemPrice.setText(formatted_price);
            holder.selectedProblems.setText(problems);
            CartDbHelper cartDbHelper = new CartDbHelper(context);
            sqLiteDatabase = cartDbHelper.getWritableDatabase();
            holder.itemView.setTag(id);
            Log.d(null, "onBindViewHolder: " + "title: " + problems + "position " + position + "id " + id);
            holder.remove_problem_from_cart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.d(null, "onBindViewHolder: removed" + "title: " + problems + "position " + position + "id " + id);
                    sqLiteDatabase.delete(
                            CartContract.CartEntry.TABLE_NAME,
                            CartContract.CartEntry._ID + " = ?",
                            new String[] { String.valueOf(id)  }
                    );
                    Toast.makeText(context, problems + " with id" + id +" Item Removed from Problem Selected" + "with postion " + position, Toast.LENGTH_SHORT).show();
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(position, getItemCount());
                    notifyDataSetChanged();
                }
            });
        }
    }
    @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);
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...