0

Preview of app

This is my preview of my app. My app receives values from google sheet. You can see the buttons plus and minus. Listview includes two values which are from the sheet & and three values are increment buttons & quantity edittext.

So my question is how to add on click event on buttons to increment and change the value of quantity edittext

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<ListView
        android:id="@+id/productTakingList"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" >

    </ListView>

</androidx.constraintlayout.widget.ConstraintLayout>

list_item_row.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="#fff"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toTopOf="@+id/tv_brand"
            app:layout_constraintTop_toTopOf="@+id/tv_brand"
            tools:layout_editor_absoluteX="5dp">

            <TextView
                android:id="@+id/takingProduct"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="33dp"
                android:text="Product"
                android:textColor="@color/black"
                android:textSize="18dp"
                app:layout_constraintBottom_toTopOf="@+id/tv_brand"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <EditText
                android:id="@+id/takingRate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="14dp"
                android:layout_marginStart="33dp"
                android:text="Rate"
                android:textSize="15dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/product"
                app:layout_constraintVertical_bias="0.38" />

            <ImageView
                android:id="@+id/takingPlus"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginBottom="14dp"
                android:layout_marginEnd="10dp"
                android:layout_marginTop="14dp"
                android:background="#0B0A0A"
                android:textAlignment="center"
                android:textSize="25sp"
                app:backgroundTint="@color/black"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/takingQty"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ic_baseline_add_box_24"
                tools:ignore="VectorDrawableCompat" />

            <EditText
                android:id="@+id/takingQty"
                android:layout_width="40dp"
                android:layout_height="45dp"
                android:layout_marginBottom="14dp"
                android:layout_marginEnd="10dp"
                android:layout_marginTop="14dp"
                android:text="0"
                android:textAlignment="center"
                android:textColor="#070707"
                android:textSize="25sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/takingMinus"
                app:layout_constraintTop_toTopOf="parent" />

            <ImageView
                android:id="@+id/takingMinus"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginBottom="14dp"
                android:layout_marginEnd="33dp"
                android:layout_marginTop="14dp"
                android:background="#0B0A0A"
                android:onClick="minus"
                android:textAlignment="center"
                android:textSize="25sp"
                app:backgroundTint="@color/black"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ic_baseline_indeterminate_check_box_24"
                tools:ignore="VectorDrawableCompat" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

ListItem.java

public class ListItem extends AppCompatActivity
{
    ListView listView;
    ListAdapter adapter;
    ProgressDialog loading;
    EditText qty;
    ImageView plusImg,minusImg;


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

        listView = (ListView) findViewById(R.id.productTakingList);
        qty=findViewById(R.id.takingQty);
        plusImg=findViewById(R.id.takingPlus);
        minusImg=findViewById(R.id.takingMinus);
        button = findViewById(R.id.button);
        getItems();
    }

    private void getItems() {

        loading =  ProgressDialog.show(this,"Loading","please wait",false,true);

        StringRequest stringRequest = new StringRequest(Request.Method.GET, "My Web App URL is here",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        parseItems(response);
                    }
                },

                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        );

        int socketTimeOut = 50000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);

        stringRequest.setRetryPolicy(policy);

        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(stringRequest);

    }


    private void parseItems(String jsonResposnce) {

        ArrayList<HashMap<String, String>> list = new ArrayList<>();

        try {
            JSONObject jobj = new JSONObject(jsonResposnce);
            JSONArray jarray = jobj.getJSONArray("items");


            for (int i = 0; i < jarray.length(); i++) {

                JSONObject jo = jarray.getJSONObject(i);

                String itemName = jo.getString("itemName");
                String price = jo.getString("price");


                HashMap<String, String> item = new HashMap<>();
                item.put("itemName", itemName);
                item.put("price",price);

                list.add(item);


            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        adapter = new SimpleAdapter(this,list,R.layout.list_item_row,
                new String[]{"itemName","price",},new int[]{R.id.takingProduct,R.id.takingRate});


        listView.setAdapter(adapter);
        loading.dismiss();
    }

}
Onkar Pawar
  • 15
  • 1
  • 5
  • 1
    You probably want to use an adapter. Also, have you checked this SO: https://stackoverflow.com/questions/12596199/android-how-to-set-onclick-event-for-button-in-list-item-of-listview – David Lee Jun 30 '21 at 08:44
  • check this tutorial to fix above task _https://www.c-sharpcorner.com/UploadFile/9e8439/create-custom-listener-on-button-in-listitem-listview-in-a/_ – Mohd Qasim Jun 30 '21 at 09:12
  • Thanks... Mohd Qasim – Onkar Pawar Jun 30 '21 at 16:55

0 Answers0