如何创建自定义字符串并动态填充它?

How to create a custom string and populate it dynamically?

我正在制作一个应用程序,可以从按钮添加一些项目到 recyclerView 格式为“name / quantity / price ”,然后我点击一个名为“Print”的按钮保存我的 StringBuilder,它由添加到 recyclerView 的不同项目制成,并通过 FTP 发送到打印机。

现在它工作正常,但我现在必须添加滑动以删除到 recyclerView,我将无法删除对应于 确切项目的 StringBuilder 来自 recyclerView.

关于我如何通过向 recyclerView 添加项目并在滑动时删除相应的字符串来制作自定义字符串,您有什么建议吗?

这是我的 onClick 方法,我将项目添加到 recyclerView 并将项目添加到 stringBuilder "scontrino " 必须按照您在方法中看到的所有 "" 等格式进行格式化...

    mAdapter.setOnItemClickListener(new Adapter.OnItemClickListener() {
        @SuppressLint("SetTextI18n")
        @Override
        public void onItemClick(final int position) {

            double prezzoScont;

            itemCassas.add(new ItemCassa(filteredList.get(position).getDeskS(),filteredList.get(position).getQuant(),filteredList.get(position).getPrice()));


                prezzo = (prezzo + (filteredList.get(position).getPrice()));


            scontrino
                    .append("<SELLITEM>").append("\n")
                    .append("<DESCRIPTION>").append(filteredList.get(position).getDeskS()).append("</DESCRIPTION>").append("\n")
                    .append("<QUANTITY>").append(filteredList.get(position).getQuant()).append("</QUANTITY>").append("\n")
                    .append("<PRICE>").append(new DecimalFormat("#0.00").format(filteredList.get(position).getPrice())).append("</PRICE>").append("\n")
                    .append("</SELLITEM>").append("\n");


            price.setText(String.valueOf(new DecimalFormat("#0.00").format(prezzo)));
            exampleAdapter.notifyItemInserted(itemCassas.size());
            mRecyclerViewTOP.scrollToPosition(itemCassas.size() - 1);

        }
    });

这是我的应用中发生的事情的屏幕截图

我认为我们可以通过使用列表而不是 StringBuilder 的方式解决您的问题

首先,假设我们有 3 个字符串,例如“"name"、"quantity"、"price"

所以我们创建列表

List<String> list = new ArrayList<>();
        list.add("name");
        list.add("quantity");
        list.add("price");

当您想要删除任何项目时(通过滑动)

list.remove("quantity");

最后,当你想打印结果时

StringBuilder result = new StringBuilder();

    for (String s : list) {
        result.append(s);
    }

希望这对您有所帮助