在回收器视图适配器bindview持有人中串联一个可选的字符串/ TextView?

时间:2019-02-15 05:19:21

标签: android xml data-binding recycler-adapter string-concatenation

我的线程类似于这些线程:

Android TextView : "Do not concatenate text displayed with setText"

Do not concatenate text display with set text, use android resource instead?

Passing dynamic string resource to "setText()"

我正在通过RecyclerView适配器下载产品列表。我正在显示两个Strings / TextViews:名称和数量。当前,它们显示如下:

咖啡壶

2

我想在数量行中添加一个字符串(“库存:”),并显示如下:

咖啡壶

库存:2

该字符串已添加到xml布局中,但由于不属于已下载列表的一部分,因此不会显示。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/products_list">

  <TextView
        android:id="@+id/product_name"
        style="@style/FragmentProductListStyle"
        tools:text="@string/product_name" />

    <TextView
        android:id="@+id/product_quantity"
        style="@style/FragmentProductListStyle"
        android:layout_below="@id/product_name"
        android:layout_marginTop="@dimen/product_list_top"
        android:text="In Stock: " />
</RelativeLayout>

是否可以将其添加到适配器类?类似于下面的第59行?该代码有效,但是消息是:“不要连接显示为'set Text'的文本。将资源字符串与占位符一起使用”。  预先谢谢你。

public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.ProductsAdapterViewHolder>
{
    private static final String TAG = ProductsAdapter.class.getSimpleName();

    private ArrayList<Products> productsList = new ArrayList<Products>();
    private Context context;

    /**
     * Creates a Products Adapter.
     */
    public ProductsAdapter(ArrayList<Products> productsList,Context context)
    {
        this.productsList = productsList;
        this.context = context;
    }

    /**
     * Cache of the children views for a products list item.
     */
    public class ProductsAdapterViewHolder extends RecyclerView.ViewHolder
    {
        @BindView(R.id.product_quantity)
        public TextView productQuantity;

        @BindView(R.id.product_name)
        public TextView productName;

        public ProductsAdapterViewHolder(View view)
        {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    @Override
    public ProductsAdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
    {
        Context context = viewGroup.getContext();
        int layoutIdForListItem = R.layout.products_list_item;
        LayoutInflater inflater = LayoutInflater.from(context);
        boolean shouldAttachToParentImmediately = false;
        View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
        return new ProductsAdapterViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ProductsAdapterViewHolder holder, int position)
    {
        //Binding data
        final Products productsView = productsList.get(position);

        holder.productName.setText(productsView.getProductName());
     line 59   
holder.productQuantity.setText("In stock: + "productsView.getProductQuantity());

    }

    @Override
    public int getItemCount()
    {
        return productsList.size();
    }

    public void setProductsList(ArrayList<Products> mProductsList)
    {
        this.productsList= mProductsList;
        notifyDataSetChanged();
    }
}

0 个答案:

没有答案