There is a JSON object. An array is nested in this object ( second model ). For each such attachment, I created a class in the model package, it turned out something like this:

First model

public class Form { @SerializedName("groups") List<Groupss> mGroupss; public List<Groupss> getmGroupss() { return mGroupss; } 

Second model

 public class Groupss { @SerializedName("2") List<First> mFirst; public List<First> getmFirst() { return mFirst; } 

Third model

 public class First { @SerializedName("type") String title; public String getTitle() { return title; } 

I registered a request to getTitle in MainActivity (using retrofit).

 public void onResponse(Call<DropDown> call, Response<DropDown> response) { DropDown jsonResponse = response.body(); Log.d("type",jsonResponse.getForm().getmGroupss().get(1).getmControls().get(2).getTitle()+""); 

What should I do if I have at least two JSON objects that need to be accessed?

  • one
    jsonResponse.getForm().getmGroupss().get(<тут индекс элемента в массиве>).getmFirst().get(<тут индекс элемента в массиве>).getTitle(); - Vladyslav Matviienko

2 answers 2

This is done through a for loop. You need to run through the elements of the array and select the value you need.

  • The fact is that I will display all the obtained values ​​in recyclerView so this option is probably not quite suitable. Correct me if I am mistaken and better visually, because I still don't know many tricks. - Morozov
  • The answer is not, clearly, is advice. - Coke
  • @RustamUmarov; It is better to leave tips in the comments to the question. - HamSter
  • @Elena, thanks for the helpful advice. But what if a person wanted to add a comment, and accidentally wrote in the field for an answer? - Coke
  • one
    @RustamUmarov and yet the question remains open)) - Morozov

How to solve this problem:

Since we have two JSON objects that we have to receive, and on our way, the List “runs” (and the second JSON object is also String).

We do the following:

1) Add to MainActivity

 private List<Groupss> mList =new ArrayList<>(); 

2) Next, we announce:

 adapter = new DataAdapter(mList); 

3) Adjust the onResponse method:

 public void onResponse(Call<DropDown> call, Response<DropDown> response) { DropDown jsonResponse = response.body(); mList.addAll(jsonResponse.getForm().getmGroupss()); adapter.notifyDataSetChanged(); Log.d("type",jsonResponse.getForm().getmGroupss().get(1).getmControls().get(2).getTitle()+" drop"); 

4) Then we adjust our adapter. Personally, I used two ViewHolder's in the application. Briefly about it, the code is like this:

 public class DataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { public static final int TYPE_TEXTVIEW = 0; public static final int TYPE_EDITVIEW = 1; private List<Groupss> mList; public DataAdapter(List<Groupss> list) { this.mList = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view; switch (viewType) { case 0: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_numeric, parent, false); return new NumericViewHolder(view); case 1: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.drop_down_options, parent, false); return new DropDownViewHolder(view); default: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.drop_down_options, parent, false); return new DropDownViewHolder(view); } } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { Groupss grup = mList.get(position); final int itemType = getItemViewType(position); switch (itemType){ case TYPE_TEXTVIEW: ((NumericViewHolder) holder).title.setText(String.valueOf(grup.getmControls().get(0).getTitle())); break; case TYPE_EDITVIEW: ((DropDownViewHolder) holder).title_options.setText(String.valueOf(grup.getmControls().get(0).getTitle())); break; default: ((DropDownViewHolder) holder).title_options.setText("Not found"); break; } } @Override public int getItemCount() { if (mList == null) return 0; return mList.size(); } @Override public int getItemViewType(int position) { int temp= -1; if (mList.get(position).getmControls().size()>0) switch (mList.get(position).getmControls().get(0).getTitle()){ case "textbox_numeric": temp=0; break; case "drop_down_options": temp = 1; break; default: temp =1; } return temp; } public class NumericViewHolder extends RecyclerView.ViewHolder { private TextView title; public NumericViewHolder(View itemView) { super(itemView); title = (TextView) itemView.findViewById(R.id.title); } } public class DropDownViewHolder extends RecyclerView.ViewHolder { private EditText title_options; public DropDownViewHolder(View itemView) { super(itemView); title_options = (EditText) itemView.findViewById(R.id.title_options); } } 

Something like this)