Добавление LinearLayout внутри ListView динамически - PullRequest
0 голосов
/ 14 апреля 2020

Я создал фрагмент OrderFragment, в котором я создал две вкладки, Order_Subscription и Order_OneTime, каждая из которых также является фрагментом. Я использовал listView внутри двух фрагментов.

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

Я хочу заполнить представление списка в формате row_order. xml, который содержит сведения о клиенте и продукте, который они покупают (LinearLayout, id = "product_details" ниже). Количество продуктов может варьироваться, поэтому я хочу динамически добавить линейный макет.

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>

В Order_Subscription. java Фрагмент, моя функция getView следующим образом.

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;
        }
    }

Я хочу добавить информацию о продукте динамически. По умолчанию есть один набор LinearLayout (product_details) из-за XML. RemoveAllViews () может очистить его. И я могу установить текст этого макета, если я не использую функцию removeAllView.

Я уже проверил: android: просмотр списка в просмотре списка Я не могу определить, где Я иду не так.

1 Ответ

0 голосов
/ 14 апреля 2020

Настоятельно рекомендуем вам использовать ReceyclerView, если вы не хотите, тогда, по крайней мере, используйте ListAdapter с ListView, чтобы он справился с добавлением и удалением wiew.

Вот пример

...