我对如何将Json数据显示到回收者视图有问题.这是我的JSON数据,我想在其中显示回收站视图中的“错误”.我尝试了一些方法,但是它总是会强制关闭.
{ "result": true, "errors": [ { "ID": "e1812696024", "offset": 2, "length": 2, "bad": "is", "better": [ "am" ], "type": "grammar" }, { "ID": "e962925648", "offset": 8, "length": 8, "bad": "engeneer", "better": [ "engineer", "engender" ], "type": "spelling" } ]}
这是我的代码:
Service.class(接口)
public interface Service { @POST("/check.PHP") Call<inputResult> readErrors(@query("userinput") String userinput, @query("APIKey") String APIKey); }
模型类别:
inputResult.java
public class inputResult { @Serializedname("result") private String result; @Serializedname("errors") private List<Errors> errors; //getter and setter}
错误.java
public class Errors { @Serializedname("ID") private String ID; @Serializedname("offset") private int offset; @Serializedname("length") private int length; @Serializedname("bad") private String bad; //getter and setter}
适配器
public class ResultAdapter extends RecyclerVIEw.Adapter<ResultAdapter.ResultVIEwHolder>{Context mContext;List<Photo> photoList = new ArrayList<>();List<Errors> errorsList = new ArrayList<>();public ResultAdapter (Context mContext, List<Errors> errorsList){ this.errorsList = errorsList; this.mContext = mContext;}@OverrIDepublic ResultVIEwHolder onCreateVIEwHolder(VIEwGroup vIEwGroup, int i) { VIEw vIEw = LayoutInflater.from(vIEwGroup.getContext()) .inflate(R.layout.result_card, vIEwGroup, false); return new ResultVIEwHolder(vIEw);}@OverrIDepublic voID onBindVIEwHolder(ResultVIEwHolder resultVIEwHolder, int i) { Errors errors = errorsList.get(i); resultVIEwHolder.mNumErrorsTextVIEw.setText(errorsList.size()); resultVIEwHolder.mIDErrorTextVIEw.setText(errors.getID()); resultVIEwHolder.mLengthErrorTextVIEw.setText(errors.getLength()); resultVIEwHolder.mBadErrorTextVIEw.setText(errors.getBad());}@OverrIDepublic int getItemCount() { return errorsList.size();} //VIEwholder ... }
片段类:
ublic class Tab1Fragment_Gram@R_301_6598@hecker extends Fragment {private static final String TAG = "Tab1Fragment";private PhotoAdapter adapter;@BindVIEw(R.ID.inputTextEditText)EditText minputGrammarEditText;@BindVIEw(R.ID.ErrorsRecyclerVIEw)RecyclerVIEw mErrorsRecyclerVIEw;List<Errors> errors = new ArrayList<>();public ArrayList<Errors> errorArrayList = new ArrayList<>();List<inputResult> inputResults = new ArrayList<>();public ArrayList<inputResult> inputResultArrayList = new ArrayList<>();public static String userinput = "I is an engeneer!";public String APIKey = ""; @OverrIDepublic 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); loadJson(); return vIEw;}public voID loadJson(){ Retrofit.Builder builder = new Retrofit.Builder() .baseUrl("https://API.textgears.com") .addConverterFactory(GsonConverterFactory.create()); Retrofit retrofit = builder.build(); Service serviceAPI = retrofit.create(Service.class); Call<inputResult> loadErrorsCall = serviceAPI.readErrors(userinput, APIKey); loadErrorsCall.enqueue(new Callback<inputResult>() { @OverrIDe public voID onResponse(Call<inputResult> call, Response<inputResult> response) { if (response.isSuccessful()){ inputResults = response.body(); Log.i("ORIG. ARRAY SIZE", String.valueOf(inputResultArrayList.size())); if (inputResultArrayList != null){ for (int i = 0; i < 5; i++){ inputResultArrayList.add(inputResults.get(i)); } Log.i("NEW ARRAY SIZE", String.valueOf(errorArrayList.size())); } mErrorsRecyclerVIEw.setItemAnimator(new DefaultItemAnimator()); mErrorsRecyclerVIEw.setAdapter(new ResultAdapter1(getContext(), inputResultArrayList)); Log.e("Fragment", response.body().toString()); Log.e("Fragment", "SUCCESS"); } else { Log.i("ERROR", ""); } } @OverrIDe public voID onFailure(Call<inputResult> call, Throwable t) { Log.i("Error: ", t.getMessage()); } });}}
Logcat:
E/AndroIDRuntime: FATAL EXCEPTION: mainProcess: renelyn.austria.grammatika, PID: 32547java.lang.classCastException: renelyn.austria.grammatika.Model.inputResult cannot be cast to java.util.List at renelyn.austria.grammatika.Tab1Fragment_Gram@R_301_6598@hecker.onResponse(Tab1Fragment_Gram@R_301_6598@hecker.java:95) at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.run(ExecutorCallAdapterFactory.java:71) at androID.os.Handler.handleCallback(Handler.java:739) at androID.os.Handler.dispatchMessage(Handler.java:95) at androID.os.Looper.loop(Looper.java:145) at androID.app.ActivityThread.main(ActivityThread.java:5832) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
解决方法:
该模型可能应该看起来像JsON输入…
与ArrayList< Error>错误而不是列表:
public class Results { @Serializedname("result") private Boolean success; @Serializedname("errors") private ArrayList<Error> errors; ...}
另一个(这里的List可用于映射String项):
public class Error { @Serializedname("ID") private String ID; @Serializedname("offset") private Long offset; @Serializedname("length") private Long length; @Serializedname("bad") private String bad; @Serializedname("better") private List<String> better; @Serializedname("type") private String type; ...}
那么它应该可以正常工作,而不会引起任何混乱:
Results results = response.body();if(results.getSuccess()) ArrayList<Error> errors = results.getErrors();}
总结 以上是内存溢出为你收集整理的Android改造:在Recyclerview中显示全部内容,希望文章能够帮你解决Android改造:在Recyclerview中显示所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)