我想将TextView从content_list_drink_details.xml
(顶部名称和顶部价格)显示为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
的组件内部使用,如Acitivty
,Application
等,但是Fragment
没有自己的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
和toppingName
中将没有数据,因此为空您可能希望像
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);
关于android - 为什么'getLayoutInflater()'无法解析,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47457720/