У меня проблема с отображением моих данных json в виде переработчика. Вот мои данные JSON, в которых я хочу отобразить значения объектов «матч» и «замена» в одном представлении переработчика. Я уже работаю с какой-то другой структурой json, но она довольно сложна для меня, поэтому я не знаю, как мне это сделать.
{
"software": {
"name": "GrammarBot",
"version": "4.3-SNAPSHOT",
"apiVersion": 1,
"premium": false,
"premiumHint": "You might be missing errors only the Premium version can find. Upgrade to see what you're missing.",
"status": ""
},
"warnings": {
"incompleteResults": false
},
"language": {
"name": "English",
"code": "en-US",
"detectedLanguage": {
"name": "English (US)",
"code": "en-US"
}
},
"matches": [
{
"message": "Statistics suggests that 'there' (as in 'Is there an answer?') might be the correct word here, not 'their' (as in 'It’s not their fault.'). Please check.",
"shortMessage": "",
"replacements": [
{
"value": "there"
}
],
"offset": 27,
"length": 5,
"context": {
"text": "I can't remember how to go their.",
"offset": 27,
"length": 5
},
"sentence": "I can't remember how to go their.",
"type": {
"typeName": "Other"
},
"rule": {
"id": "CONFUSION_RULE",
"description": "Statistically detect wrong use of words that are easily confused",
"issueType": "non-conformance",
"category": {
"id": "TYPOS",
"name": "Possible Typo"
}
}
}
]
}
Мой желаемый RecyclerView будет выглядеть так:
Match 1,
Replacement 1,
Match 2,
Match 3,
Replacement 2
Вот мои коды:
Сервис (Интерфейс)
public interface Service {
@GET("/v2/check?")
Call<GrammarBotMain> readErrorsGrammar(@Query("api_key") String api_key,
@Query("text") String text,
@Query("language") String language);
}
Класс модели:
GrammarBotMain.java
public class GrammarBotMain {
@SerializedName("software")
private Software software;
@SerializedName("warnings")
private Warning warning;
@SerializedName("language")
private Language language;
@SerializedName("matches")
private ArrayList<Match> matches;
//--getter and setter
}
Match.java
public class Match {
@SerializedName("message")
private String message;
@SerializedName("shortMessage")
private String shortMessage;
@SerializedName("replacements")
private ArrayList<Replacement> replacements;
@SerializedName("offset")
private int offset;
@SerializedName("length")
private int length;
@SerializedName("context")
private Context context;
@SerializedName("sentence")
private String sentence;
@SerializedName("type")
private Type type;
@SerializedName("rule")
private Rule rule;
//--getter and setter
}
Replacement.java
public class Replacement {
@SerializedName("value")
private String value;
//--getter and setter
}
Адаптер
public class ResultAdapter extends RecyclerView.Adapter<ResultAdapter.ResultViewHolder>{
Context mContext;
ArrayList<Match> matchList = new ArrayList<>();
ArrayList<Replacement> replacementList = new ArrayList<>();
public ResultAdapter (Context mContext, ArrayList<Match> matchList){
this.matchList = matchList;
this.mContext = mContext;
}
@Override
public ResultViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.result_card, viewGroup, false);
return new ResultViewHolder(view);
}
@Override
public void onBindViewHolder(ResultViewHolder resultViewHolder, int i) {
Match match = matchList.get(i);
resultViewHolder.mBadErrorTextView.setText(match.getSentence());
resultViewHolder.mBetterErrorTextView.setText(match.getMessage());
}
//viewholder and getcount
}
Фрагмент класса
public class Tab1Fragment_GrammarChecker extends Fragment {
@BindView(R.id.InputTextEditText)
EditText mInputGrammarEditText;
@BindView(R.id.ErrorsRecyclerView)
RecyclerView mErrorsRecyclerView;
public static String userInput;
public String apiKey = "";
public String language = "en-US";
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab1_grammar_checker, container, false);
ButterKnife.bind(this, view);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
mErrorsRecyclerView.setLayoutManager(layoutManager);
mErrorsRecyclerView.setItemAnimator(new DefaultItemAnimator());
return view;
}
//onClick
public void loadJson(){
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://api.grammarbot.io")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
Service serviceAPI = retrofit.create(Service.class);
Call<GrammarBotMain> loadErrorsCall = serviceAPI.readErrorsGrammar(apiKey, userInput, language);
final ProgressDialog progressDialog;
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMax(100);
progressDialog.setMessage(getResources().getString(R.string.progressDialogLoadingResultMessage));
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();
loadErrorsCall.enqueue(new Callback<GrammarBotMain>() {
@Override
public void onResponse(Call<GrammarBotMain> call, Response<GrammarBotMain> response) {
progressDialog.dismiss();
GrammarBotMain grammarBotMain = response.body();
ArrayList<Match> matches = grammarBotMain.getMatches();
mErrorsRecyclerView.setAdapter(new ResultAdapter(getContext(), matches));
Log.i("Fragment", "SUCCESS");
}
@Override
public void onFailure(Call<GrammarBotMain> call, Throwable t) {
progressDialog.dismiss();
Log.e("Error: ", t.getMessage());
}
});
}
}