0

I have created a fragment OrderFragment in which I have created two tabs, Order_Subscription and Order_OneTime, each of which is also a fragment. I used a listView inside the two fragments.

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listViewSubscription">
</ListView>

I want to populate the list view in the format of row_order.xml which contains Customer Details and Product they buy(LinearLayout, id= "product_details" below). The products can be variable in number so I want to dynamically add the linear Layout.

row_order.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:id="@+id/row_card"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <TextView
            android:id="@+id/customer_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:text="Customer Name"
            android:textStyle="bold">

        </TextView>

        <TextView
            android:id="@+id/address_phoneno"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:text="Phone Number"
            android:textStyle="bold">

        </TextView>

        <TextView
            android:id="@+id/amount_left"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:text="Amount Left"
            android:textStyle="bold">

        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="20sp"
        android:paddingRight="10sp"
        android:id="@+id/product_details">

        <TextView
            android:id="@+id/product_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:gravity="center_vertical"
            android:text="Product Name">

        </TextView>

        <TextView
            android:id="@+id/quantity_order"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:gravity="center_vertical"
            android:text="Qty">

        </TextView>

        <TextView
            android:id="@+id/amount_order"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:gravity="center_vertical"
            android:text="Amount">

        </TextView>

        <CheckBox
            android:id="@+id/delivered_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">

        </CheckBox>
    </LinearLayout>



</LinearLayout>

In the Order_Subscription.java Fragment, my getView function is as follows.

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            //Following is for Customer Details. This works
            LayoutInflater layoutInflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row_order = layoutInflater.inflate(R.layout.row_order, parent, false);
            TextView cust_name = row_order.findViewById(R.id.customer_name);
            TextView ph_no_addr = row_order.findViewById(R.id.address_phoneno);
            TextView amnt = row_order.findViewById(R.id.amount_left);
            cust_name.setText(customerName[position]);
            ph_no_addr.setText(ph_no[position]);


            //Product details I want to add. Right now adding without loop
           LinearLayout linearLayout = (LinearLayout)row_order.findViewById(R.id.product_details);
           linearLayout.removeAllViews();
           View child = getLayoutInflater().inflate(R.id.product_details, null);
            TextView prod_name = linearLayout.findViewById(R.id.product_name);
            prod_name.setText("New Product");
            TextView quat_order = linearLayout.findViewById(R.id.quantity_order);
            TextView amnt_order = linearLayout.findViewById(R.id.amount_order);
            quat_order.setText("50");
            amnt_order.setText("100");
            linearLayout.addView(child);




            return row_order;
        }
    }

I want to add product details dynamically. By default there's one set of LinearLayout(product_details) because of XML. RemoveAllViews() is able to clear it. And I can set text of that one layout if I don't use removeAllView function.

I already checked: android: listview in listview I'm not able to figure out where I'm going wrong.

bejuzb
  • 55
  • 6

1 Answers1

0

Strongly suggest that you should use ReceyclerView if you don't want then at least use ListAdapter with ListView so it would handle wiew adding binding and removing for you.

Here is an example

Antonis Radz
  • 3,036
  • 1
  • 16
  • 34