这样做,
private JokeListAdapter jokeListAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
jokeListAdapter = new JokeListAdapter();
setListAdapter(jokeListAdapter);
call = service.jokes();
call.enqueue(new retrofit2.Callback<Joke>() {
@Override
public void onResponse(Call<Joke> call, Response<Joke> response) {
if(response.isSuccessful()) {
List<Joke> jokes = response.body();
Log.e(TAG, "response is successful");
jokeListAdapter.setValue(jokes);
//setListAdapter(new JokeListAdapter(jokes));
Log.e(TAG, "response is successful");
} else {
Log.e(TAG, "response is not successful");
System.out.println(response.errorBody());
}
}
@Override
public void onFailure(Call<Joke> call, Throwable t) {
Log.e(TAG, "response on failure");
}
});
}
在JokelistaAdapter中。java是这样变化的。
private class JokeListAdapter extends BaseAdapter {
List<Joke> mJokes = new ArrayList<Joke>(); // use this list for every ever in adapter
public JokeListAdapter() {
}
public void setValue(List<Joke> jokes) {
mJokes.clear(); // Clearing old items(To add new items only)
if(jokes != null && jokes.size() > 0) { // We dont need to add if the size is 0
mJokes.addAll(jokes); // Adding all the item to the list item
}
notifyDataSetChanged(); // We are refreshing View the latest with list items.
}
@Override
public int getCount() {
return mJokes.size();
}