简体   繁体   English

如何创建JSON存档的表单动态

[英]How to create a form dynamic of JSON archive

I need to create a dynamic form similar to this one in the image. 我需要在图像中创建类似于此表单的动态表单。

在此处输入图片说明

I tried doing it using recyclerview, as you can see in my JSON file: 我尝试使用recyclerview进行此操作,如您在JSON文件中所见:

{
"cells": [
    {
        "id": 1,
        "type": 2,
        "message": "Olá, primeiro se apresente com o seu nome:",
        "typefield": null,
        "hidden": false,
        "topSpacing": 60,
        "show": null,
        "required": false
    },
    {
        "id": 2,
        "type": 1,
        "message": "Nome completo",
        "typefield": 1,
        "hidden": false,
        "topSpacing": 35,
        "show": null,
        "required": true
    },
    {
        "id": 4,
        "type": 1,
        "message": "Email",
        "typefield": 3,
        "hidden": true,
        "topSpacing": 35,
        "show": null,
        "required": true
    },
    {
        "id": 6,
        "type": 1,
        "message": "Telefone",
        "typefield": "telnumber",
        "hidden": false,
        "topSpacing": 10,
        "show": null,
        "required": true
    },
    {
        "id": 3,
        "type": 4,
        "message": "Gostaria de cadastrar meu email",
        "typefield": null,
        "hidden": false,
        "topSpacing": 35,
        "show": 4,
        "required": false
    },
    {
        "id": 7,
        "type": 5,
        "message": "Enviar",
        "typefield": null,
        "hidden": false,
        "topSpacing": 10,
        "show": null,
        "required": true
    }
]

} }

I have a list with 6 items, the way I did instead of appearing each placeholder in its EditText, it mounted 6 forms. 我有一个包含6个项目的列表,我这样做的方式不是在其EditText中显示每个占位符,而是安装了6个表单。 As you can see in the pictures: 如您在图片中看到的:

在此处输入图片说明

My Fragment 我的片段

public class ContactFragment extends Fragment implements FormView{

View view;
RecyclerView recyclerView;
List<Cell> cells = new ArrayList<>();
FormAdapter adapter;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_contact, container, false);

    recyclerView = view.findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(true);
    final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);

    adapter = new FormAdapter(getContext(), cells);
    recyclerView.setAdapter(adapter);

    onResume();

    setupFont();

    return view;
}

@Override
public void onResume() {
    super.onResume();
    FormPresenter formPresenter = new FormPresenter(this);
    formPresenter.getForms();
}


public void setupFont() {
    TextView textContact = view.findViewById(R.id.text_contact);
    Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "DINEngschriftStd.otf");
    textContact.setTypeface(font);
}

@Override
public void forms(List<Cell> cells) {
    adapter = new FormAdapter(getContext(), cells);
    recyclerView.setAdapter(adapter);
}

} }

My Adapter 我的适配器

public class FormAdapter  extends RecyclerView.Adapter<FormAdapter.ViewHolder> {
Context context;
List<Cell> cells;

public FormAdapter(Context context, List<Cell> cells) {
    this.context = context;
    this.cells = cells;
}

@NonNull
@Override
public FormAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.contact_form, parent, false);
    return new FormAdapter.ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull FormAdapter.ViewHolder holder, int position) {

    holder.txtHello.setText(cells.get(position).getMessage());
    holder.checkBox.setText(cells.get(position).getMessage());
    holder.btnSend.setText(cells.get(position).getMessage());
    holder.iptName.setHint(cells.get(position).getMessage());
    holder.iptEmail.setHint(cells.get(position).getMessage());
    holder.iptPhone.setHint(cells.get(position).getMessage());

}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    TextInputLayout iptName, iptEmail, iptPhone;
    TextView txtHello;
    CheckBox checkBox;
    Button btnSend;

    public ViewHolder(View view) {
        super(view);
        checkBox = view.findViewById(R.id.checkBox);
        txtHello = view.findViewById(R.id.text_contact);

        iptName = view.findViewById(R.id.iptName);
        iptEmail = view.findViewById(R.id.iptEmail);
        iptPhone = view.findViewById(R.id.iptPhone);

        btnSend = view.findViewById(R.id.btnSend);
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

    }
  }
}

Thank you!! 谢谢!!

This happens because onResume can be called twice, when the fragment enters and when onCreateView, and don't put the value adapter until the callback form has finished. 发生这种情况的原因是,在进入片段和onCreateView时,onResume可以被调用两次,并且在回调形式完成之前不要放置值适配器。 Try the code as follows: 尝试如下代码:

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_contact, container, false);

    recyclerView = view.findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);

    FormPresenter formPresenter = new FormPresenter(this);
    formPresenter.getForms();

    setupFont();

    return view;
}

@Override
public void onResume() {
    super.onResume();

}
@Override
public void forms(List<Cell> cells) {
    adapter = new FormAdapter(getContext(), cells);
    recyclerView.setAdapter(adapter);
}

Also please fix this 还请解决这个问题

@Override
public void onBindViewHolder(@NonNull FormAdapter.ViewHolder holder, int position) {

    holder.txtHello.setText(cells.get(position).getMessage());
    holder.checkBox.setText(cells.get(position).getMessage());
    holder.btnSend.setText(cells.get(position).getMessage());
    holder.iptName.setHint(cells.get(position).getMessage());
    holder.iptEmail.setHint(cells.get(position).getMessage());
    holder.iptPhone.setHint(cells.get(position).getMessage());

}

Hope it works 希望能奏效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM