Listview not displaying response from retrofit api call

时间:2018-02-01 18:28:08

标签: android listview retrofit

I made a retrofit call in android and got a list response objects back but for some reason, they are not getting displayed in the listview and the application is not crashing either.

public class MainActivity extends AppCompatActivity {

    private OpenNotifyService mService;
    ListView listView ;

    List<Response> arrayList = new ArrayList<Response>();
    ArrayAdapter<Response> adapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mService = ApiUtils.getOpenNotifyService();

            // Get ListView object from xml
            listView =  findViewById(R.id.myListView);

            adapter = new ArrayAdapter<Response>(this,android.R.layout.simple_list_item_1, android.R.id.text1, arrayList);

            loadAnswers();

        }

    public void loadAnswers() {
        mService.getTimes(12,10).enqueue(new Callback<InternationalSpaceStationPassTimes>() {
            @Override
            public void onResponse(Call<InternationalSpaceStationPassTimes> call, retrofit2.Response<InternationalSpaceStationPassTimes> response) {
                Log.e("Request URL", call.request().url().toString());
                if (response.isSuccessful()) {
                    arrayList= response.body().getResponse();

// Assign adapter to ListView

                    listView.setAdapter(adapter);
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            int itemPosition     = position;

                            // ListView Clicked item value
                            String  itemValue    = (String) listView.getItemAtPosition(position);

                            // Show Alert
                            Toast.makeText(getApplicationContext(),
                                    "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
                                    .show();
                        }
                    });
                    Log.d("MainActivity", "posts loaded from API");
                } else {
                    int statusCode = response.code();
                    // handle request errors depending on status code
                }
            }

            @Override
            public void onFailure(Call<InternationalSpaceStationPassTimes> call, Throwable t) {
                Log.d("MainActivity", "error loading from API");
            }
        });
    }

Please help me..

1 个答案:

答案 0 :(得分:0)

Your list is initialized when your Activity instance is constructed:

List<Response> arrayList = new ArrayList<Response>();

During onCreate(), your adapter is constructed:

adapter = new ArrayAdapter<Response>(this, 
                  android.R.layout.simple_list_item_1,
                  android.R.id.text1,
                  arrayList);

Inside your loadAnswers() callback, your list is reinitialized:

arrayList= response.body().getResponse();

Finally, you attach the adapter to the listview:

listView.setAdapter(adapter);

Your problem is that your callback assigns a different List instance to your arrayList variable. Since your adapter was already created at this point, this means that the list inside of adapter is different from the list pointed to by arrayList. So nothing will appear.

You can solve this a couple of different ways. Probably the easiest would be to change your loadAnswers() callback to do this instead:

arrayList.clear();
arrayList.addAll(response.body().getResponse());
adapter.notifyDataSetChanged();