为什么'getLayoutInflater()'不能解析

时间:2017-11-23 14:25:48

标签: android layout-inflater

我想将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>

2 个答案:

答案 0 :(得分:1)

getLayoutInflater()只能直接在拥有context Acitivty ApplicationFragment之类的组件中使用,但context没有自己的inflater viewHolder

解决方案:

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:您正在为toppingName toppingNamelist的新视图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); 获取数据plot(diff(diff(colormap('rainbow'),1),1))

git tag -a `date “+%d%b%Y%H%M%S”` -m "enter your comment here" 

答案 1 :(得分:0)

尝试替换:

LayoutInflater inflater2 = this.getLayoutInflater();

使用:

 LayoutInflater inflater2 = getActivity().getLayoutInflater();