gpt4 book ai didi

安卓改造 : Display JSON data in Recyclerview

转载 作者:搜寻专家 更新时间:2023-11-01 09:22:07 25 4
gpt4 key购买 nike

我对如何将我的 json 数据显示到回收站 View 有疑问。这是我的 JSON 数据,我想在其中显示单个回收器 View 中的“匹配”和“替换”对象值。我已经使用了一些其他的 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 看起来像:

匹配 1,
替换 1,
第 2 场比赛,
第 3 场比赛,
替换 2

这是我的代码:

服务(接口(interface))

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
}

匹配.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
}

替换.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
}

fragment 类

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());
}
});
}

}

最佳答案

我不知道你是如何决定维护列表顺序的。但是如果你想在同一个 RecyclerView 中混合两种类型的对象值,那么你可以按照以下步骤操作:

1) 在您的适配器中使用 Object 的 ArrayList 而不是 MatchReplacement ArrayLists。根据您想要的顺序将您的 MatchReplacement 对象添加到该对象列表中。

ArrayList<Object> data = new ArrayList<>();
data.add(match1);
data.add(replacement1);
data.add(match2);

2) 通过这种方式确定当前需要在 onCreateViewHolder()onBindViewHolder() 中处理的 View 类型方法:

int VIEW_TYPE_MATCH = 1;
int VIEW_TYPE_REPLQCEMENT = 2;

private int getViewType(int position) {
Object value = data.get(position);
if (value instanceof Match) return VIEW_TYPE_MATCH;
else return VIEW_TYPE_REPLQCEMENT;
}

关于安卓改造 : Display JSON data in Recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54056001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com