How to delete an item detail from the item list

I am creating an android app using Android Studio and Kotlin and managed to create a new field to add more item to a list using tutorials online. I couldn’t get the delete button work.

Following is the code for NewSale.kt

package com.example.trad

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout

class NewSale : AppCompatActivity() {
    private var parentLinearLayout: LinearLayout? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new_sale)
        parentLinearLayout = findViewById(R.id.parent_linear_layout)
    }

    fun deleteItem(view: View) {
        parentLinearLayout!!.removeView(view.parent as View)
    }
    fun addMoreItem(view: View) {
        val inflater =
                getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val rowView: View = inflater.inflate(R.layout.field, null)
        parentLinearLayout!!.addView(rowView, parentLinearLayout!!.childCount - 1)
    }
}

Following is the code for activity_new_sale.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/parent_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".NewSale">

    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="30dp"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:paddingRight="40dp">

                    <EditText
                        android:id="@+id/etxItemCode"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:hint="Enter Item Code"
                        android:paddingRight="40dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:hint="Item description.."
                        android:paddingRight="80dp"
                        android:textSize="12sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <EditText
                        android:id="@+id/etxItemquality"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="10dp"
                        android:hint="Pcs" />

                    <EditText
                        android:id="@+id/etxSellPrice"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="10dp"
                        android:hint="Price" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:hint="Amt"
                        android:textSize="20sp" />

                    <Button
                        android:id="@+id/btnDeleteItem"
                        android:layout_width="0dp"
                        android:layout_height="40dp"
                        android:layout_gravity="center"
                        android:layout_marginStart="10dp"
                        android:layout_weight="1"
                        android:background="@android:drawable/ic_delete"
                        android:onClick="deleteItem" />

                </LinearLayout> 

            </LinearLayout>

            <Button
                android:id="@+id/btnAddMoreItem"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="10dp"
                android:layout_marginTop="15dp"
                android:onClick="addMoreItem"
                android:text="Add More Item" />

            <Button
                android:id="@+id/btnGenerateInvocie"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="10dp"
                android:layout_marginTop="15dp"
                android:text="Generate Invoice" />
        </LinearLayout>

    </ScrollView>

</LinearLayout>

The following code is for field.xml

<LinearLayout 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="match_parent"
    android:layout_marginTop="30dp"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:paddingRight="40dp">

                    <EditText
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:hint="Enter Item Code"
                        android:paddingRight="40dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:hint="Item description.."
                        android:paddingEnd="80dp"
                        android:textSize="12sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <EditText
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="10dp"
                        android:hint="Pcs" />

                    <EditText
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="10dp"
                        android:hint="Price" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:hint="Amt"
                        android:textSize="20sp" />

                    <Button
                        android:id="@+id/btnDeleteItem"
                        android:layout_width="0dp"
                        android:layout_height="40dp"
                        android:layout_weight="1"
                        android:background="@android:drawable/ic_delete"
                        android:onClick="deleteItem" />

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

What I am trying to create is a billing/invoicing part of the app.

I will input items with their price, quantity and calculate item wise amount and calculate the total amount after entering all the items. Then, add it to the firebase database.

Could someone help me with this?

Answer

Update Your feild.xml File with Below Code. remove Scrollbar to fix extra space issue. I also remove unnecessary linear layout from XML.

**feild.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="5dp">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingRight="40dp">

                <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Enter Item Code"
                    android:paddingRight="40dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Item description.."
                    android:paddingEnd="80dp"
                    android:textSize="12sp" />

            </LinearLayout>

                <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="10dp"
                    android:hint="Pcs" />

                <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="10dp"
                    android:hint="Price" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Amt"
                    android:textSize="20sp" />

                <Button
                    android:id="@+id/btnDeleteItem"
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:layout_weight="1"
                    android:background="@android:drawable/ic_delete"
                    android:onClick="deleteItem" />

        </LinearLayout>

Leave a Reply

Your email address will not be published. Required fields are marked *