![](/img/trans.png)
[英]NullPointerException in ListView inside a LinearLayout
[英]Adding LinearLayout inside a ListView dynamically
我創建了一個片段 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 Fragment中,我的getView function如下。
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;
}
}
我想動態添加產品詳細信息。 由於 XML,默認情況下有一組 LinearLayout(product_details)。RemoveAllViews() 能夠清除它。 如果我不使用 removeAllView function,我可以設置該布局的文本。
我已經檢查過: android: listview in listview我無法弄清楚哪里出錯了。
強烈建議您應該使用ReceyclerView
,如果您不想,那么至少將ListAdapter
與ListView
一起使用,這樣它會為您處理添加綁定和刪除。
這是一個 例子
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.