you need to make the textview visible when an empty json {"cities":[]} comes from the server {"cities":[]} How can this be caught?

 adapter = new CityAdapter(response.body().getCities(),getActivity()); if(response.body().getCities().toString().equals("[]")){ Toast.makeText(getActivity(),"Нету данных",Toast.LENGTH_SHORT); emptyTV.setVisibility(TextView.VISIBLE); } Log.e("body",response.body().getCities().toString()); Log.e("adapter",adapter.toString()); 

Log:

 E/body: [] E/adapter: com.example.danilshik.testapplication.helper.CityAdapter@536e01a8 

Temporarily done using .equals (), but I think there is a more correct way

Class:

 public class Cities { private ArrayList<City> cities; public Cities() { } public ArrayList<City> getCities() { return cities; } } 
  • and getCities what returns? - Komdosh
  • @Komdosh returns a list of cities, added code - danilshik

2 answers 2

See, you get a response from the server in the form of "{" cities ": []}".

In this case, you have an array of cities whose size is 0.

Accordingly, you need to check whether the size of the array is 0 or not.

  if(response.body().getCities().size()==0){ Toast.makeText(getActivity(),"Нету данных",Toast.LENGTH_SHORT); emptyTV.setVisibility(TextView.VISIBLE); } 

    Do you need to check whether the sheet is empty?

     response.body().getCities().isEmpty() 

    https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#isEmpty ()

    • you didn’t understand a little, or I didn’t put it that way, the answer comes as though, but empty: {"cities": []}. How to check it except my version? And look carefully at the log - danilshik
    • @danilshik well, check, I don’t understand what the problem is, we call toString() on an empty sheet and get "[]" in the log, well, check this sheet for emptiness. - katso