Почему я получаю не полный список в RecyclerView?
Почему я получаю не полный список в RecyclerView?
public class HomeFragment extends Fragment implements FoodRecyclerViewInterface {
public HomeFragment() {
// Required empty public constructor
}
private FragmentHomeBinding binding;
private static String PARSE_URL = "https://api.edamam.com/api/food-database/v2/parser?app_id=e5bc806d&app_key=5f7521ffeefe491b936cea6271e13d3d&ingr=apple";
private static String SEARCH_TEXT = null;
List<FoodModels> foodModelsList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentHomeBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
foodModelsList = new ArrayList<>();
GetData getData = new GetData();
getData.execute();
return view;
}
@Override
public void onItemClick(int position) {
Intent i = new Intent(getActivity(), DetailActivity.class);
i.putExtra("name", foodModelsList.get(position).getName());
i.putExtra("img", foodModelsList.get(position).getImg());
i.putExtra("kcal", foodModelsList.get(position).getKcal());
i.putExtra("procent", foodModelsList.get(position).getProcent());
i.putExtra("fat", foodModelsList.get(position).getFat());
i.putExtra("chocdf", foodModelsList.get(position).getChocdf());
i.putExtra("fidtg", foodModelsList.get(position).getFidtg());
startActivity(i);
}
public class GetData extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
String current = "";
try {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(PARSE_URL);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int data = isr.read();
while(data != -1) {
current += (char) data;
data = isr.read();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(urlConnection != null) {
urlConnection.disconnect();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return current;
}
@Override
protected void onPostExecute(String s) {
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("hints");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject hintObject = jsonArray.getJSONObject(i);
JSONObject foodObject = hintObject.getJSONObject("food");
String label = foodObject.getString("label");
String image = foodObject.getString("image");
JSONObject nutrientsObject = foodObject.getJSONObject("nutrients");
String enercKcal = nutrientsObject.getString("ENERC_KCAL");
String procnt = nutrientsObject.getString("PROCNT");
String fat = nutrientsObject.getString("FAT");
String chocdf = nutrientsObject.getString("CHOCDF");
String fibtg = nutrientsObject.getString("FIBTG");
FoodModels model = new FoodModels();
model.setName(label);
model.setImg(image);
model.setKcal(enercKcal);
model.setProcent(procnt);
model.setFat(fat);
model.setChocdf(chocdf);
model.setFidtg(fibtg);
foodModelsList.add(model);
}
} catch (JSONException e) {
e.printStackTrace();
}
PutDataIntoRecyclerView(foodModelsList);
}
}
private void PutDataIntoRecyclerView(List<FoodModels> categoryList) {
FoodAdapter categoryAdapter = new FoodAdapter(getContext(), categoryList, this);
binding.recyclerMain.setHasFixedSize(true);
binding.recyclerMain.setAdapter(categoryAdapter);
binding.recyclerMain.setLayoutManager(new LinearLayoutManager(getContext()));
}
}
Вот мой адаптер
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.MyViewHolder> {
private final FoodRecyclerViewInterface recyclerViewInterface;
private Context context;
private List<FoodModels> mData;
public FoodAdapter(Context context, List<FoodModels> mData, FoodRecyclerViewInterface recyclerViewInterface) {
this.recyclerViewInterface = recyclerViewInterface;
this.context = context;
this.mData = mData;
}
@NonNull
@Override
public FoodAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.recycler_item, parent, false);
return new FoodAdapter.MyViewHolder(view, recyclerViewInterface);
}
@Override
public void onBindViewHolder(@NonNull FoodAdapter.MyViewHolder holder, int position) {
holder.name.setText(mData.get(position).getName());
holder.img.setText(mData.get(position).getImg());
holder.kcal.setText(mData.get(position).getKcal());
holder.procent.setText(mData.get(position).getProcent());
holder.fat.setText(mData.get(position).getFat());
holder.chocdf.setText(mData.get(position).getChocdf());
holder.fidtg.setText(mData.get(position).getFidtg());
}
@Override
public int getItemCount() {
return mData.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView name, img, kcal, procent, fat, chocdf, fidtg;
public MyViewHolder(@NonNull View itemView, FoodRecyclerViewInterface recyclerViewInterface) {
super(itemView);
name = itemView.findViewById(R.id.name);
img = itemView.findViewById(R.id.img);
kcal = itemView.findViewById(R.id.kcal);
procent = itemView.findViewById(R.id.procent);
fat = itemView.findViewById(R.id.fat);
chocdf = itemView.findViewById(R.id.chocdf);
fidtg = itemView.findViewById(R.id.fidtg);
itemView.setOnClickListener(v -> {
if (recyclerViewInterface != null){
int pos = getAdapterPosition();
if(pos != RecyclerView.NO_POSITION) {
recyclerViewInterface.onItemClick(pos);
}
}
});
}
}
}
Пример ответа
"hints": [
{
"food": {
"foodId": "food_amqspy5ap567v6bun60usbgsaor7",
"label": "Nuts",
"knownAs": "nuts",
"nutrients": {
"ENERC_KCAL": 594,
"PROCNT": 17.3,
"FAT": 51.45,
"CHOCDF": 25.35,
"FIBTG": 9
},
"category": "Generic foods",
"categoryLabel": "food",
"image": "https://www.edamam.com/food-img/e29/e2985d3585a6950c276e204ba7ba9b2e.jpg"
},
"measures": [
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_unit",
"label": "Whole",
"weight": 2
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_pouch",
"label": "Pouch",
"weight": 113
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_serving",
"label": "Serving",
"weight": 34
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_nut",
"label": "Nut",
"weight": 2
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_bag",
"label": "Bag",
"weight": 113
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_container",
"label": "Container",
"weight": 113
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_package",
"label": "Package",
"weight": 113
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_packet",
"label": "Packet",
"weight": 113
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_gram",
"label": "Gram",
"weight": 1
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_ounce",
"label": "Ounce",
"weight": 28.349523125
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_pound",
"label": "Pound",
"weight": 453.59237
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_kilogram",
"label": "Kilogram",
"weight": 1000
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_handful",
"label": "Handful",
"weight": 17.125
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_cup",
"label": "Cup",
"weight": 137
}
]
},