У меня есть список который устанавливается в recyclerView. Как можно из него удалить элемент? Bот код адаптера:
public class RecyclerViewPizzasAdapter extends RecyclerView.Adapter<RecyclerViewPizzasAdapter.PizzasViewHolder> {
private int numbersItem;
public RecyclerViewPizzasAdapter(int k) {
numbersItem = k;
};
@NonNull
@Override
public PizzasViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
int layoutIdForListIttem = R.layout.adapter_backet;
LayoutInflater inflator = LayoutInflater.from(context);
View view = inflator.inflate(layoutIdForListIttem, parent, false);
PizzasViewHolder viewHolder = new PizzasViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull PizzasViewHolder holder, int position) {
holder.bind(position);
}
@Override
public int getItemCount() {
return numbersItem;
}
class PizzasViewHolder extends RecyclerView.ViewHolder {
TextView nameText;
TextView countText;
Button plus;
Button minus;
public PizzasViewHolder(@NonNull View itemView) {
super(itemView);
nameText = itemView.findViewById(R.id.textok1);
countText = itemView.findViewById(R.id.textok2);
plus = itemView.findViewById(R.id.plus);
minus = itemView.findViewById(R.id.minus);
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
Log.i(TAG, String.valueOf(position));
ArrayList<String> Pizzas = new ArrayList<>();
if (MainActivity.pepperoni.getCount() != 0)
Pizzas.add("pizza1");
if (MainActivity.calzone.getCount() != 0)
Pizzas.add("pizza2");
if (MainActivity.quattrostagioni.getCount() != 0)
Pizzas.add("pizza3");
if (MainActivity.quattroformaggi.getCount() != 0)
Pizzas.add("pizza4");
if (MainActivity.mexican.getCount() != 0)
Pizzas.add("pizza5");
Log.i(TAG, "Button was clicked for list item " + position);
boolean b = true;
int i = 0;
while (b) {
i++;
String j = "pizza" + i;
if (j.equals(Pizzas.get(position))) {
b = false;
if (i == 1) {
int y = MainActivity.pepperoni.getCount();
MainActivity.pepperoni.setCount(y + 1);
Log.i(TAG, "+++++++++++++++++");
BasketPage.onButton();
}
if (i == 2) {
int y = MainActivity.calzone.getCount();
MainActivity.calzone.setCount(y + 1);
Log.i(TAG, "--------------");
BasketPage.onButton();
}
if (i == 3) {
int y = MainActivity.quattrostagioni.getCount();
MainActivity.quattrostagioni.setCount(y + 1);
BasketPage.onButton();
}
if (i == 4) {
int y = MainActivity.quattroformaggi.getCount();
MainActivity.quattroformaggi.setCount(y + 1);
BasketPage.onButton();
}
if (i == 5) {
int y = MainActivity.mexican.getCount();
MainActivity.mexican.setCount(y + 1);
BasketPage.onButton();
}
}
}
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
Log.i(TAG, String.valueOf(position));
ArrayList<String> Pizzas = new ArrayList<>();
if (MainActivity.pepperoni.getCount() != 0)
Pizzas.add("pizza1");
if (MainActivity.calzone.getCount() != 0)
Pizzas.add("pizza2");
if (MainActivity.quattrostagioni.getCount() != 0)
Pizzas.add("pizza3");
if (MainActivity.quattroformaggi.getCount() != 0)
Pizzas.add("pizza4");
if (MainActivity.mexican.getCount() != 0)
Pizzas.add("pizza5");
Log.i(TAG, "Button was clicked for list item " + position);
boolean b = true;
int i = 0;
while (b) {
i++;
String j = "pizza" + i;
if (j.equals(Pizzas.get(position))) {
b = false;
if (i == 1) {
int y = MainActivity.pepperoni.getCount();
MainActivity.pepperoni.setCount(y - 1);
BasketPage.onButton();
}
if (i == 2) {
int y = MainActivity.calzone.getCount();
MainActivity.calzone.setCount(y - 1);
BasketPage.onButton();
}
if (i == 3) {
int y = MainActivity.quattrostagioni.getCount();
MainActivity.quattrostagioni.setCount(y - 1);
BasketPage.onButton();
}
if (i == 4) {
int y = MainActivity.quattroformaggi.getCount();
MainActivity.quattroformaggi.setCount(y - 1);
BasketPage.onButton();
}
if (i == 5) {
int y = MainActivity.mexican.getCount();
MainActivity.mexican.setCount(y - 1);
BasketPage.onButton();
}
}
}
}
});
}
void bind(int name) {
Log.i(TAG, String.valueOf(name) + "эээээээ");
Log.i(TAG, String.valueOf(MainActivity.pepperoni.getCount()));
ArrayList<String> Pizzas = new ArrayList<>();
if (MainActivity.pepperoni.getCount() != 0)
Pizzas.add("Пепперони " + MainActivity.pepperoni.getCount() + " шт.");
if (MainActivity.calzone.getCount() != 0)
Pizzas.add("Кальцоне " + MainActivity.calzone.getCount() + " шт.");
if (MainActivity.quattrostagioni.getCount() != 0)
Pizzas.add("Четыре сезона " + MainActivity.quattrostagioni.getCount() + " шт.");
if (MainActivity.quattroformaggi.getCount() != 0)
Pizzas.add("Четыре сыра " + MainActivity.quattrostagioni.getCount() + " шт.");
if (MainActivity.mexican.getCount() != 0)
Pizzas.add("Мексиканская " + MainActivity.mexican.getCount() + " шт.");
nameText.setText(Pizzas.get(name));
}
}
}
и вот код активности:
public class BasketPage extends Activity implements OnTouchListener {
public ScrollView scroller;
public String name;
public String phoneNumber;
public String street;
public String home;
public String porch;
public String level;
public String apprt;
public String comment;
public boolean b;
public static boolean chng = false;
private RecyclerView recyclerView;
private static RecyclerViewPizzasAdapter adapteR;
WorkBD workBD;
MainActivity mainActivity;
public void culculateFprice() {
TextView FinalBuy = (TextView) findViewById(R.id.buyText);
int Pprice = MainActivity.pepperoni.getFinalPrice();
int Cprice = MainActivity.calzone.getFinalPrice();
int QCprice = MainActivity.quattrostagioni.getFinalPrice();
int QFprice = MainActivity.quattroformaggi.getFinalPrice();
int Mprice = MainActivity.mexican.getFinalPrice();
int FinalPrice = Pprice + Cprice + QCprice + QFprice + Mprice;
FinalBuy.setText("Стоимось вашего заказа: " + FinalPrice + " руб.");
}
private int calculateAdapter() {
int k = -1;
if (MainActivity.pepperoni.getCount() != 0)
k++;
if (MainActivity.calzone.getCount() != 0)
k++;
if (MainActivity.quattrostagioni.getCount() != 0)
k++;
if (MainActivity.quattroformaggi.getCount() != 0)
k++;
if (MainActivity.mexican.getCount() != 0)
k++;
if (k == -1)
return -1;
else
return k + 1;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bascket_page);
mainActivity = new MainActivity();
int CountAdapter = calculateAdapter();
culculateFprice();
workBD = new WorkBD(this);
scroller = findViewById(R.id.scroll);
scroller.setOnTouchListener(this);
if (CountAdapter != -1) {
recyclerView = findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapteR = new RecyclerViewPizzasAdapter(CountAdapter);
recyclerView.setAdapter(adapteR);
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (chng) {
culculateFprice();
chng = false;
}
return true;
}
public static void onButton() {
adapteR.notifyDataSetChanged();
chng = true;
}
public void finish_check(View v) {
b = true;
TextView t = (TextView) findViewById(R.id.errorview);
EditText namik = (EditText) findViewById(R.id.name);
EditText phonik = (EditText) findViewById(R.id.phone);
EditText streetik = (EditText) findViewById(R.id.street);
EditText homeik = (EditText) findViewById(R.id.home);
EditText porchik = (EditText) findViewById(R.id.porch);
EditText levelik = (EditText) findViewById(R.id.level);
EditText apprtik = (EditText) findViewById(R.id.room);
EditText commentic = (EditText) findViewById(R.id.comment);
name = namik.getText().toString();
t.setText("");
if (name.equals("")) {
t.setText(getString(R.string.error_final));
b = false;
}
phoneNumber = phonik.getText().toString();
if (phoneNumber.equals("")) {
t.setText(getString(R.string.error_final));
b = false;
}
street = streetik.getText().toString();
if (street.equals("")) {
t.setText(getString(R.string.error_final));
b = false;
}
home = homeik.getText().toString();
if (home.equals("")) {
t.setText(getString(R.string.error_final));
b = false;
}
porch = porchik.getText().toString();
if (porch.equals("")) {
t.setText(getString(R.string.error_final));
b = false;
}
level = levelik.getText().toString();
if (porch.equals("")) {
t.setText(getString(R.string.error_final));
b = false;
}
apprt = apprtik.getText().toString();
if (apprt.equals("")) {
t.setText(getString(R.string.error_final));
b = false;
}
comment = commentic.getText().toString();
String address = "Улица: " + street + ", Дом: " + home + ". Номер подъезда: " + porch + ". Этаж: " + level + ". Номер квартиры: " + apprt;
if (b) {
SQLiteDatabase database = workBD.getReadableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(WorkBD.KEY_NAME, name);
contentValues.put(WorkBD.KEY_NUMBER, phoneNumber);
contentValues.put(WorkBD.KEY_ADDRESS, address);
contentValues.put(WorkBD.KEY_PIZZA1, MainActivity.pepperoni.getCount());
contentValues.put(WorkBD.KEY_PIZZA2, MainActivity.calzone.getCount());
contentValues.put(WorkBD.KEY_PIZZA3, MainActivity.quattrostagioni.getCount());
contentValues.put(WorkBD.KEY_PIZZA4, MainActivity.quattroformaggi.getCount());
contentValues.put(WorkBD.KEY_PIZZA5, MainActivity.mexican.getCount());
contentValues.put(WorkBD.KEY_COMMENT, comment);
database.insert(WorkBD.TABLE_CONTACTS, null, contentValues);
}
}
}
Нужно что бы когда количество элементов в элементе становилось равно 0 элемент из писка удалялся из RecyclerView.