В моем коде я получаю список json в recyclerview. Проблема в том, что если я слово из поиска подставляю к ссылке с помощью
setOnQueryTextListener
то у меня выводит один и тот же список два раза. А в логах я получаю то что у меня выводится два раза моя ссылка
Но если не использовать поиск вообще а подставить слово к ссылке то работает нормально и в логах я вижу только один вывод ссылки.
Мой HomeFragment
public class HomeFragment extends Fragment implements FoodRecyclerViewInterface {
private FragmentHomeBinding binding;
private static String PARSE_URL = "https://api.edamam.com/api/food-database/v2/parser?app_id=e5bc806d&app_key=5f7521ffeefe491b936cea6271e13d3d&ingr=";
private static String SEARCH_TEXT = null;
private static String RESULT_URL = 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();
binding.searchs.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
SEARCH_TEXT = query;
RESULT_URL = PARSE_URL + SEARCH_TEXT;
foodModelsList = new ArrayList<>();
GetData getData = new GetData();
getData.execute();
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return view;
}
@Override
public void onItemClick(int position) {
try {
Intent i = new Intent(getActivity(), DetailActivity.class);
i.putExtra("id", foodModelsList.get(position).getId());
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());
startActivity(i);
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressLint("StaticFieldLeak")
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(RESULT_URL);
Log.i("url", String.valueOf(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 foodId = foodObject.getString("foodId");
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");
FoodModels model = new FoodModels();
model.setId(foodId);
model.setName(label);
model.setImg(image);
model.setKcal(enercKcal);
model.setProcent(procnt);
model.setFat(fat);
model.setFavStatus("0");
foodModelsList.add(model);
}
} catch (Exception 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()));
}
}