我在Fragment上显示JSONArray数据时遇到问题。我已验证我的链接,字符串等。但是在调试控制台上,我的请求显示正确。我怀疑JSONArray请求,但找不到任何错误。
请帮忙!
npm start
这是我的片段
D/Request::: 010-8470-0486
010-8472-0964
010-8474-2372
D/Request::: 010-8475-0089
D/Request::: 010-8475-7585
010-8478-0007
D/Request::: 010-8478-0573
010-8479-0311
010-8479-9234
D/Request::: 010-8481-2030
和ModelDB类
public class FragmentDB extends Fragment {
private View v;
private RecyclerView recyclerView;
private String url = "http://www.example.com/api.php";
private List<ModelDB> modelDBList;
public FragmentDB() {
}
@SuppressWarnings("UnnecessaryLocalVariable")
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Objects.requireNonNull(getActivity()).checkSelfPermission(Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED &&
getActivity().checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED &&
getActivity().checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
return v;
} else {
Intent i10 = new Intent(getActivity(), PermissionActivity.class);
startActivity(i10);
Objects.requireNonNull(getActivity()).finish();
}
v = inflater.inflate(R.layout.frag_db, container, false);
recyclerView = v.findViewById(R.id.rv_db);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
RecyclerView.LayoutManager layoutManager = linearLayoutManager;
recyclerView.setLayoutManager(layoutManager);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation());
modelDBList = new ArrayList<>();
DbRvAdapter adapter = new DbRvAdapter(getContext(), modelDBList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.setHasFixedSize(true);
Objects.requireNonNull(getActivity()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
getDBLogs();
return v;
}
@SuppressWarnings({"LogNotTimber", "deprecation"})
private void getDBLogs() {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// Do something with the response
try {
for ( int i = 0; i < response.length(); i++) {
JSONObject obj = response.getJSONObject(i);
ModelDB dblist = new ModelDB();
dblist.setPhone_number(obj.getString("phone_number"));
dblist.setInfo1(obj.getString("info1"));
modelDBList.add(dblist);
Log.d("Request:: ", obj.getString("phone_number"));
}
} catch(JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
Log.e("Volley", error.toString());
}
});
RequestQueue rq = Volley.newRequestQueue(Objects.requireNonNull(getActivity()).getApplicationContext());
rq.add(jsonArrayRequest);
}
}
和适配器类
public class ModelDB {
public String phone_number;
public String info1;
public ModelDB() {
}
public ModelDB(String phone_number, String info1) {
this.phone_number = phone_number;
this.info1 = info1;
}
public String getPhone_number() {
return phone_number;
}
public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}
public String getInfo1() {
return info1;
}
public void setInfo1(String info1) {
this.info1 = info1;
}
}
对不起,我的英语不好。
答案 0 :(得分:1)
在解析网络调用结果之后,您似乎没有更新modelDB的内部适配器列表。
答案 1 :(得分:1)
在for循环体之后通知您的适配器,并全局声明适配器
for ( int i = 0; i < response.length(); i++) {
............
modelDBList.add(dblist);
}
if(dblist != null and dblist.size() > 0){
adapter.notifyDataSetChanged();
}