AddLineCustomLabelBackendAdapter.java 5.66 KB
package etelligens.com.foodsafety.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.net.URLDecoder;
import java.util.ArrayList;

import etelligens.com.foodsafety.R;
import etelligens.com.foodsafety.model.AddLineCustomLabelBackendModal;

public class AddLineCustomLabelBackendAdapter extends RecyclerView.Adapter<AddLineCustomLabelBackendAdapter.AddLineViewHolder> {
    Context context;
    ArrayList<AddLineCustomLabelBackendModal> lineCustomLabelBackendModals;
    int count = 0;

    Passitem passitem;

    public interface Passitem {
        void description(AddLineCustomLabelBackendModal addLineCustomLabelBackendModal, int id);
    }

    public AddLineCustomLabelBackendAdapter(Context context, ArrayList<AddLineCustomLabelBackendModal> labelBackendModals, Passitem passitem) {
        this.context = context;
        this.lineCustomLabelBackendModals = labelBackendModals;
        this.passitem = passitem;
    }

    View view;

    @NonNull
    @Override

    public AddLineCustomLabelBackendAdapter.AddLineViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        view = LayoutInflater.from(context).inflate(R.layout.item_layout_add_line_custom_label, parent, false);
        return new AddLineViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull final AddLineCustomLabelBackendAdapter.AddLineViewHolder holder, @SuppressLint("RecyclerView") int i) {
        final AddLineCustomLabelBackendModal addLineCustomLabelBackendModal = lineCustomLabelBackendModals.get(i);
        String description = addLineCustomLabelBackendModal.getDescription();

        if (description != null) {
            holder.customdescriptiontext.setText(decodeString(description));
        }
        String ids = String.valueOf(addLineCustomLabelBackendModal.getId());
        for (int j = 0; j < addLineCustomLabelBackendModal.getId(); j++) {
            if (j != 0) {
                holder.customdeletetext.setVisibility(View.VISIBLE);
            }
        }
        holder.Cpttxt.setText("Pt");
        holder.customdescriptiontext.setOnClickListener(v -> {
            final int id = addLineCustomLabelBackendModal.getId();
            passitem.description(addLineCustomLabelBackendModal, id);
        });
        holder.customdeletetext.setOnClickListener(v -> {
            lineCustomLabelBackendModals.remove(i);
            notifyDataSetChanged();
        });
        holder.custom_boldtext.setOnClickListener(v -> {
            String bolddd = holder.customdescriptiontext.getText().toString().trim();
            String sourceString = "<b>" + bolddd + "</b> ";
            holder.customdescriptiontext.setText(Html.fromHtml(sourceString));

        });
        holder.custom_italictext.setOnClickListener(view -> {
            String bolddd = holder.customdescriptiontext.getText().toString().trim();
            String sourceString = "<i>" + bolddd + "</i> ";
            holder.customdescriptiontext.setText(Html.fromHtml(sourceString));
        });
        holder.custom_utext.setOnClickListener(view -> {
            String bolddd = holder.customdescriptiontext.getText().toString().trim();
            String sourceString = "<u>" + bolddd + "</u> ";
            holder.customdescriptiontext.setText(Html.fromHtml(sourceString));
        });

        holder.Cpttxt.setOnClickListener(view -> {
            String bolddd = holder.customdescriptiontext.getText().toString().trim();
            holder.customdescriptiontext.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
        });
    }

    private String decodeString(String productPrice) {
        String value= URLDecoder.decode(productPrice);
        return value;
    }
    @Override
    public int getItemCount() {
        return lineCustomLabelBackendModals.size();
    }

    public class AddLineViewHolder extends RecyclerView.ViewHolder {
        TextView customdescriptiontext, custom_boldtext, custom_utext, custom_italictext, Cpttxt;
        ImageView customdeletetext;
        int maxLength = 30;// 设置最大输入长度为40个字符
        public AddLineViewHolder(View itemview) {
            super(itemview);
            customdescriptiontext = itemview.findViewById(R.id.custom_description_text);
            Cpttxt = itemview.findViewById(R.id.custom_pttext);
            custom_boldtext = itemview.findViewById(R.id.cus_bold_text);
            custom_utext = itemview.findViewById(R.id.custom_under_text);
            custom_italictext = itemview.findViewById(R.id.custom_italic_text);
            customdeletetext = itemview.findViewById(R.id.custom_delete_text);
            customdescriptiontext.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }


                @Override
                public void afterTextChanged(Editable s) {
                    if (s != null && s.length() > maxLength) {
                        customdescriptiontext.setError("The maximum length limit is exceeded");
                    } else {
                        customdescriptiontext.setError(null);
                    }
                }
            });

        }
    }
}