Reputation: 55
I have two buttons +
and -
in my listview
to increase and decrease the quantity of an item
. I've written the +
button onclicklistener
inside my adapter class
. When I click the button
, it increases the quantity only once. The initial quantity is set to 0
. On first click it becomes 1
. After that, no matter how many times I click the button
the quantity does not change.
List view layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/foodpic"
android:layout_width="80dp"
android:layout_height="80dp"
android:padding="10dp"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/foodname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/foodpic"
android:padding="10dp"
android:text="name" />
<TextView
android:id="@+id/foodprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="16dp"
android:padding="10dp"
android:text="price" />
<Button
android:id="@+id/minus"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_below="@+id/foodname"
android:layout_toEndOf="@+id/foodpic"
android:text="-"
android:textSize="12sp" />
<EditText
android:id="@+id/quantity"
android:layout_width="25dp"
android:layout_height="30dp"
android:layout_below="@+id/foodname"
android:layout_toEndOf="@+id/minus"
android:ems="10"
android:inputType="number"
android:textAlignment="center"
android:background="@drawable/edittextbackground"
android:text="0" />
<Button
android:id="@+id/plus"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignTop="@+id/quantity"
android:layout_toEndOf="@+id/quantity"
android:text="+"
android:textSize="12sp" />
<Button
android:id="@+id/addtocart"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignBaseline="@+id/quantity"
android:layout_alignBottom="@+id/quantity"
android:layout_alignEnd="@+id/foodprice"
android:background="@drawable/ic_shopping_cart_black_24dp" />
</RelativeLayout>
Adapter class:
package com.res.easyorder;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.zip.Inflater;
public class MyAdapter extends ArrayAdapter<item> {
int q_ty = 0;
ArrayList<item> foodlist = new ArrayList<>();
public MyAdapter(Context context, int textViewResourceId, ArrayList<item> objects){
super(context, textViewResourceId, objects);
foodlist = objects;
}
@Override
public int getCount(){
return super.getCount();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_view_layout, null);
ImageView foodpic = (ImageView) v.findViewById(R.id.foodpic);
TextView foodname = (TextView) v.findViewById(R.id.foodname);
TextView foodprice = (TextView) v.findViewById(R.id.foodprice);
Button plus = (Button) v.findViewById(R.id.plus);
final EditText quantity = (EditText) v.findViewById(R.id.quantity);
Button minus = (Button) v.findViewById(R.id.minus);
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
item item = getItem(position);
q_ty = Integer.parseInt(item.getQuantity());
q_ty = q_ty + 1;
quantity.setText("" +q_ty);
}
});
foodname.setText(foodlist.get(position).getFoodName());
foodpic.setImageResource(foodlist.get(position).getFoodImage());
foodprice.setText("BDT: " +foodlist.get(position).getFoodPrice());
quantity.setText(foodlist.get(position).getQuantity());
return v;
}
}
Item java class:
package com.res.easyorder;
/**
* Created by User on 02-Apr-18.
*/
public class item {
String foodName, quantity;
int foodImage, foodPrice;
public item(String foodName, int foodImage, int foodPrice, String quantity)
{
this.foodName = foodName;
this.foodImage = foodImage;
this.foodPrice = foodPrice;
this.quantity = quantity;
}
public String getFoodName()
{
return foodName;
}
public int getFoodImage()
{
return foodImage;
}
public int getFoodPrice()
{
return foodPrice;
}
public String getQuantity()
{
return quantity;
}
}
Activity layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.res.easyorder.foodActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:divider="#ffff"
android:dividerHeight="5dp"/>
</RelativeLayout>
Activity java class:
package com.res.easyorder;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import java.util.ArrayList;
public class foodActivity extends AppCompatActivity {
private String type = null;
ListView simplelist;
ArrayList<item> foodlist = new ArrayList<>();
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food);
firebaseAuth = FirebaseAuth.getInstance();
simplelist = (ListView) findViewById(R.id.listview);
type = getIntent().getExtras().getString("type");
if(type.equals("breakfast"))
{
foodlist.add(new item("Rooti",R.drawable.ruti,5,"0"));
foodlist.add(new item("Parata",R.drawable.porata,8,"0"));
foodlist.add(new item("Tandoor",R.drawable.tandoor,15,"0"));
foodlist.add(new item("Vegetable",R.drawable.sodji,10,"0"));
foodlist.add(new item("Daal",R.drawable.dal,10,"0"));
foodlist.add(new item("Omelet",R.drawable.dimvaji,15,"0"));
foodlist.add(new item("Singara",R.drawable.singara,8,"0"));
foodlist.add(new item("Samosa",R.drawable.samosa,10,"0"));
foodlist.add(new item("Puri",R.drawable.puri,5,"0"));
}
MyAdapter myAdapter = new MyAdapter(this,R.layout.list_view_layout,foodlist);
simplelist.setAdapter(myAdapter);
}
}
Upvotes: 0
Views: 1304
Reputation: 12005
It's not chaging because you're only changing the item quantity count in the UI, not in the actual item:
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
item item = getItem(position);
q_ty = Integer.parseInt(item.getQuantity());
q_ty = q_ty + 1;
quantity.setText("" +q_ty);
}
});
You need to set the new quantity in item so when you click the button again it will retrieve the new quantity:
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
item item = getItem(position);
q_ty = Integer.parseInt(item.getQuantity());
q_ty = q_ty + 1;
item.setQuantity(q_ty);
quantity.setText("" +q_ty);
}
});
EDIT:
Add to your item class:
public void setQuantity(String quantity)
{
this.quantity = quantity;
}
And in your click method instead of using:
item.setQuantity(q_ty);
use:
item.setQuantity(String.valueOf(q_ty));
Upvotes: 3
Reputation: 59
instead if this
item item = getItem(position);
q_ty = Integer.parseInt(item.getQuantity());
q_ty = q_ty + 1;
quantity.setText("" +q_ty);
try this
item item = getItem(position);
q_ty = q_ty + 1;
quantity.setText(q_ty);
Upvotes: 0
Reputation: 672
The problem is that you increment only local valiable q_ty
and don't set new value to element of your class. You need to add this string to your onClickListener
method:
item.setQuantity(q_ty);
Upvotes: 0