简体   繁体   中英

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:

{
"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. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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