1

I've wanted to display TextView from content_list_drink_details.xml (topping name and topping price) to adapter of cart_layout.xml. But it appears that my getLayoutInflater() cannot be resolved.

I'm not sure if this is possible or not thus I really need help from you guys, the experts out there.

Here is my 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());
}

Here is my 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>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Not sure what `this.getLayoutInflater();` is supposed to do since there's no such method in the class. Second, don't inflate views in onBindViewHolder, that should be done in onCreateViewHolder. – patrick.elmquist Nov 23 '17 at 14:43

2 Answers2

2

getLayoutInflater() can only be used directly inside components who has their own context like Acitivty , Application etc but Fragments don't have their own context

Solution:

Declare inflater outside viewHolder and initialize it inside constructor and use inflater instead of 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);
}

Now use

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

Update : you are inflating a new view vi who's TextView toppingName and toppingName will have no data in it , so it's empty

You might want to fetch data from list like 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);
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • Thanks! It appears to be error free, but it does not displaying my TextView from the adapter. I believe this is another thing to handle. Will try to look at it. Thanks again – Mariam Syafiqah Saidi Nov 23 '17 at 14:44
  • @MariamSyafiqahSaidi i am glad that i could help, i got the issue, wait for update – Pavneet_Singh Nov 23 '17 at 14:50
  • @MariamSyafiqahSaidi you only need to use `holder.txt_price..` or similar code , no need to inflate extra views , just make sure you have data in list and `CartViewHolder` is properly defined – Pavneet_Singh Nov 23 '17 at 14:59
  • Dear sir, but my topping name and topping price data is not from List listData. They are from a textview in layout content_list_drink_detail – Mariam Syafiqah Saidi Nov 23 '17 at 15:45
  • @MariamSyafiqahSaidi you got two option to , either you should have a single layout for rows which contains all the required vies or you need to add your dynamic views (like `vi`) into `itemView` inside the `CartViewHolder` class , just look into [**this answer**](https://stackoverflow.com/a/40760201/4936904) , although having all views in single layout is simple and efficient – Pavneet_Singh Nov 23 '17 at 15:52
  • How do i add my vi into itemView? Btw thanks for your prompt replies – Mariam Syafiqah Saidi Nov 23 '17 at 16:11
  • @MariamSyafiqahSaidi you need to inflate the other views in `CartViewHolder` class , look carefully at the code of `VHShare` class and `imageView` along with `addView`, in the linked answer in above comment – Pavneet_Singh Nov 23 '17 at 16:15
  • Dear sir, GeneralFunction in my code line is showing errors. Do you have any idea on this? Do you mind if I drop you an email on this matter? – Mariam Syafiqah Saidi Nov 23 '17 at 16:51
  • nobody can give you any solution with this piece of information `some function with some error` , although i won't mind to take a look at it but explain the issue properly with concise details and proper error and i also insist you to post the link you gone through to solve your error – Pavneet_Singh Nov 24 '17 at 12:42
0

Try to replace :

LayoutInflater inflater2 = this.getLayoutInflater();

With :

 LayoutInflater inflater2 = getActivity().getLayoutInflater();
Faris Kapo
  • 346
  • 9
  • 30