Я создал пользовательский ArrayAdapter для обработки json ответа от моего API и поместил его в свой список. Мой адаптер разобрал json и поставил его в нужное место в макете. В моем ответе json у меня есть список с 10 значениями, но когда я передаю его адаптеру, он обрабатывает только 7 значений из 10 без каких-либо ошибок.
Мой код адаптера:
public class AchievementsAdapter extends ArrayAdapter {
private Context mContext;
int mResource;
static String core_url = ApiCalls.get_core_url();
public AchievementsAdapter(Context context, int resource, ArrayList objects){
super(context, resource, objects);
mContext = context;
mResource = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
String resp = (String) getItem(position);
System.out.println(position);
System.out.println(resp);
JSONObject json = null;
String title = null;
String desc = null;
String unlock_date = null;
String picture = null;
Boolean unlocked = null;
try {
json = new JSONObject(resp);
title = (String) json.get("title");
desc = (String) json.get("desc");
unlocked = (Boolean) json.get("unlocked");
if (unlocked == true){
unlock_date = (String) json.get("unlock_date");
}
if(json.get("picture").toString() != null){
picture = json.get("picture").toString();
}
} catch (JSONException e) {
e.printStackTrace();
}
ImageView image = convertView.findViewById(R.id.pic);
LinearLayout achievement_bg = convertView.findViewById(R.id.achievement_bg);
TextView tvTitle = convertView.findViewById(R.id.title);
TextView tvDesc = convertView.findViewById(R.id.desc);
TextView tvDate = convertView.findViewById(R.id.date);
if(unlocked == true){
achievement_bg.setBackgroundColor(mContext.getResources().getColor(R.color.Gold));
String[] date_arr = unlock_date.split("-");
tvDate.setText(String.format("%s.%s.%s",date_arr[2], date_arr[1], date_arr[0]));
} else {
achievement_bg.setBackgroundColor(mContext.getResources().getColor(R.color.Grey));
}
if(picture != null){
Picasso.get().load(String.format("%s%s", core_url,picture)).into(image);
}
tvTitle.setText(title);
tvDesc.setText(desc);
return convertView;
}
}
Вызов API и настройка адаптера
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()){
String resp = response.body().string();
JSONObject json = null;
try {
json = new JSONObject(resp);
JSONArray unlocked_json = (JSONArray) json.get("unlocked");
JSONArray locked_json = (JSONArray) json.get("locked");
ArrayList arr_achievements = new ArrayList();
if (unlocked_json != null) {
for (int i=0;i<unlocked_json.length();i++){
JSONObject unl_achievement = unlocked_json.getJSONObject(i);
unl_achievement.put("unlocked", true);
arr_achievements.add(unl_achievement.toString());
}
}
if (locked_json != null) {
for (int i=0;i<locked_json.length();i++){
JSONObject loc_achievement = locked_json.getJSONObject(i);
loc_achievement.put("unlocked", false);
arr_achievements.add(loc_achievement.toString());
}
}
AchievementsAdapter adapter = new AchievementsAdapter(activity, R.layout.unl_adapter_achievements, arr_achievements);
ListView unlListView = activity.findViewById(R.id.achievements);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
unlListView.setAdapter(adapter);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
response.body().close();
}
});
Ответ, который идет на адаптер:
[
{
"id":10,
"title":"title 5",
"desc":"Follow 5 friends",
"picture":null,
"unlock_date":"2020-02-19",
"unlocked":true
},
{
"id":6,
"title":"title 1",
"desc":"Notify your friend about your success for the first time!",
"picture":null,
"unlock_date":"2020-02-19",
"unlocked":true
},
{
"id":13,
"title":"title 8",
"desc":"Ring 100 times",
"picture":null,
"unlock_date":"2020-02-19",
"unlocked":true
},
{
"id":14,
"title":"title 9",
"desc":"Ring 300 times",
"picture":null,
"unlock_date":"2020-02-19",
"unlocked":true
},
{
"id":7,
"title":"title 2",
"desc":"Get 10 followers",
"picture":null,
"unlocked":false
},
{
"id":8,
"title":"title 3",
"desc":"Get 30 followers",
"picture":null,
"unlocked":false
},
{
"id":9,
"title":"title 4",
"desc":"Get 50 followers",
"picture":null,
"unlocked":false
},
{
"id":15,
"title":"title 10",
"desc":"Ring 365 times in one year",
"picture":null,
"unlocked":false
},
{
"id":11,
"title":"title 6",
"desc":"Hit the button every day at least for week!",
"picture":null,
"unlocked":false
},
{
"id":12,
"title":"title 7",
"desc":"Hit the button at least 20 times in current month",
"picture":null,
"unlocked":false
}
]
И что у меня есть в виде списка. Здесь должно быть еще 3 элемента. Почему его там нет?