Объединить необязательную строку / TextView в держателе bindview адаптера представления переработчика? - PullRequest
0 голосов
/ 15 февраля 2019

Моя тема похожа на эти:

Android TextView: «Не объединять текст, отображаемый с помощью setText»

Не объединять текст, отображаемый сустановить текст, использовать вместо этого ресурс Android?

Передача динамического строкового ресурса в "setText ()"

Я загружаю список продуктов через RecyclerViewадаптер.Я отображаю две строки / 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();
    }
}
...