我正在使用带有 Cardview 的 Recycler View 并使用 Volley 显示来自 JSON 的数据。它在targetsdkversion 19上正常工作,但在targetsdkversion为26时不显示7个列表项后的数据。
btnsend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chatLayout.setVisibility(View.VISIBLE);
subject = etsub.getText().toString();
details = etdetail.getText().toString();
if (etsub.getText().toString().equals("")) {
Toast.makeText(getActivity(), "Enter subject", Toast.LENGTH_SHORT).show();
subj_layout.setError("Plese Enter Subject!");
}
else if (etdetail.getText().toString().equals("")) {
detail_layout.setError("Please Enter Details!");
}
else {
Log.d(TAG, "onClick:send ");
String url = "http://webapi.eduware.in/test/api/ParentsCommunication?StudentCurrentID=" + studentId.trim() + "&SchoolID=" + schoolid.trim() + "&CommunicationSubject=" + subject.trim() + "&CommunicationDetails=" + details.trim();
Log.d(TAG, "onClick: " + url);
Log.d(TAG, "fetchNotificationByVolley: url = " + url);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "onResponse: " + response);
try {
JSONObject jobj = new JSONObject(response);
String status = jobj.getString("status");
if (status.equals("1")) {
Toast.makeText(getActivity(), "Successfully added", Toast.LENGTH_SHORT).show();
getChatbyVolley();
txtnocomm.setVisibility(View.GONE);
} else {
Toast.makeText(getActivity(), "Data not added", Toast.LENGTH_SHORT).show();
chatLayout.setVisibility(View.VISIBLE);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "onErrorResponse: " + error.getMessage());
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return null;
}
};
ApplicationController.getInstance().addToRequestQueue(stringRequest);
d.dismiss();
}
}
});
private void getChatbyVolley() {
chatLayout.setVisibility(View.VISIBLE);
String url = "http://webapi.eduware.in/test/api/ParentsCommunication?StudentCurrentID="+studentId.trim();
final ArrayList<Chat> arrayList = new ArrayList<>();
final ArrayList<String> array = new ArrayList<>();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "onResponse: " + response);
try {
JSONObject jobj = new JSONObject(response);
String status = jobj.getString("status");
if(status.equals("1"))
{
JSONArray jsonArray = jobj.getJSONArray("data");
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject = jsonArray.getJSONObject(i);
String date = jsonObject.getString("Date");
Log.d(TAG, "onResponse: "+date);
String[] separated = date.split("[T :]");
Log.d(TAG, "onResponse: splitted string0" + separated[0]);
Log.d(TAG, "onResponse: splitted string1" + separated[1]);
Log.d(TAG, "onResponse: splitted string2" + separated[2]);
Log.d(TAG, "onResponse: splitted string3" + separated[3]);
String time = separated[1] + ":" + separated[2];
Log.d(TAG, "onResponse: time is:::::::::::" + time);
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");
String inputDateStr=separated[0];
Date date1 = inputFormat.parse(inputDateStr);
String outputDateStr = outputFormat.format(date1);
String detail = jsonObject.getString("CommunicationDetails");
Log.d(TAG, "onResponse: details========="+detail);
String pid = jsonObject.getString("ParentID");
Log.d(TAG, "onResponse: pid is*********"+pid);
array.add(pid);
Log.d(TAG, "onResponse: pid is*********"+array);
editor.putString("parent_id",pid);
editor.putString("parentid_array",array.toString());
editor.commit();
Chat c = new Chat();
c.setCommsubject(jsonObject.getString("CommunicationSubject"));
c.setCommdetails(jsonObject.getString("CommunicationDetails"));
c.setDate(outputDateStr);
c.setParentid(pid);
arrayList.add(c);
progress.dismiss();
}
chatAdapter = new ChatAdapter(getActivity(), arrayList);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(chatAdapter);
recyclerView.setHasFixedSize(true);
chatAdapter.notifyDataSetChanged();
Log.d(TAG, "onResponse: " + arrayList.size());
progress.dismiss();
}
else
{
Toast.makeText(context, "No communication available", Toast.LENGTH_SHORT).show();
progress.dismiss();
rlnocomm.setVisibility(View.VISIBLE);
btnadd.setVisibility(View.VISIBLE);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "onErrorResponse: " + error.getMessage());
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return null;
}
};
ApplicationController.getInstance().addToRequestQueue(stringRequest);
}