为什么 'getLayoutInflater()' 没有解析

Why isn't the 'getLayoutInflater()' resolving

我想显示 content_list_drink_details.xml 的 TextView(顶层名称和顶层价格)到 cart_layout.xml. 的适配器,但似乎我的 getLayoutInflater() 无法解析。

我不确定这是否可行,因此我真的需要你们这些专家的帮助。

这是我的 CartAdapter.java:

   public class CartAdapter extends     RecyclerView.Adapter<CartViewHolder> {

private List<Order> listData = new ArrayList<>();
private Context context;

public CartAdapter(List<Order> listData, Context context) {
    this.listData = listData;
    this.context = context;
}


@Override
public CartViewHolder onCreateViewHolder(ViewGroup parent, int     viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View itemView = inflater.inflate(R.layout.cart_layout,parent,false);
    return new CartViewHolder(itemView);
}

@Override
public void onBindViewHolder(CartViewHolder holder, int position) {
    TextDrawable drawable = TextDrawable.builder()
            .buildRound(""+listData.get(position).getQuantity(),     Color.parseColor("#ed008c"));
    holder.img_cart_count.setImageDrawable(drawable);

    Locale locale = new Locale("ms","MY");
    NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
    double price =     (Double.parseDouble(listData.get(position).getPrice()))*    (Integer.parseInt(listData.get(position).getQuantity()));
    holder.txt_price.setText(fmt.format(price));
holder.txt_cart_name.setText(listData.get(position).getProductName());

    //Retrieve topping name
    LayoutInflater inflater2 = this.getLayoutInflater();
    View vi = inflater2.inflate(R.layout.content_list_drink_detail,     null);
    TextView toppingName =     (TextView)vi.findViewById(R.id.toppingSelected);
    holder.txt_item_topping.setText(toppingName.getText().toString());

    //Retrieve topping price
    TextView toppingPrice =     (TextView)vi.findViewById(R.id.toppingSelectedPrice);
    holder.txt_topping_price.setText(toppingPrice.getText().toString());
}

这是我的 cart_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
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="wrap_content"
android:layout_margin="8dp"
app:cardElevation="4dp"
>

<LinearLayout
    android:layout_marginTop="16dp"
    android:layout_marginBottom="16dp"
    android:orientation="horizontal"
    android:layout_gravity="center_vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:orientation="vertical"
        android:layout_weight="9"
        android:layout_width="0dp"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_marginLeft="8dp"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/cart_item_name"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:text="Drink 01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/cart_item_price"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="italic"
                android:text="RM 1.00"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_marginLeft="8dp"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/cart_item_topping"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:text="Topping 01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/cart_item_topping_price"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="italic"
                android:text="RM "
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

    </LinearLayout>

    <ImageView
        android:layout_gravity="center_vertical|end"
        android:layout_marginRight="16dp"
        android:id="@+id/cart_item_count"
        android:layout_width="32dp"
        android:layout_height="32dp"
        />

    </LinearLayout>
</android.support.v7.widget.CardView>

getLayoutInflater() 只能直接在有自己的 context 的组件内部使用,例如 AcitivtyApplication 等,但 Fragment 没有他们自己的 context

解决方案:

viewHolder 外声明 inflater 并在构造函数内初始化它并使用 inflater 而不是 getLayoutInflater()

LayoutInflater inflater;
public CartAdapter(List<Order> listData, Context context) {
    this.listData = listData;
    this.context = context;
    inflater = LayoutInflater.from(context);
}


@Override
public CartViewHolder onCreateViewHolder(ViewGroup parent, int     viewType) {
    View itemView = inflater.inflate(R.layout.cart_layout,parent,false);
    return new CartViewHolder(itemView);
}

现在使用

// remove it , redundant 
// LayoutInflater inflater2 = this.getLayoutInflater();
View vi = inflater.inflate(R.layout.content_list_drink_detail,null);

Update :您正在膨胀一个新视图 vi 谁是 TextView toppingNametoppingName 将没有数据,所以它是空的

您可能想从 list 获取数据,例如 listData.get(position).getQuantity()

// this below code is not needed at all
//LayoutInflater inflater2 = this.getLayoutInflater();
//View vi = inflater2.inflate(R.layout.content_list_drink_detail,     null);
//TextView toppingName =     (TextView)vi.findViewById(R.id.toppingSelected);
//TextView toppingPrice = (TextView)vi.findViewById(R.id.toppingSelectedPrice);

尝试替换:

LayoutInflater inflater2 = this.getLayoutInflater();

与:

 LayoutInflater inflater2 = getActivity().getLayoutInflater();