无法使用改造在ListView中显示数据

时间:2019-06-12 17:03:18

标签: java android listview retrofit

我在ListView中的显示数据有问题。我从Retrofit响应中获取数据,但是我的活动(应该显示此数据)一直都是空白。我敢肯定,我已经在控制台中收到了数据,并检查了它。

模型类

public class itemList_model {
@SerializedName("results")
private List<itemList_Results> results;

public List<itemList_Results> getResults() {
    return results;
}

public static class itemList_Results{
    @SerializedName("title")
    String title;
    @SerializedName("vote_average")
    Double vote;
    @SerializedName("genre_ids")
    List<Integer> genresId;
    @SerializedName("release_date")
    String releaseDate;

    public itemList_Results(String title, Double vote, String releaseDate) {
        this.title = title;
        this.vote = vote;
        this.releaseDate = releaseDate;
    }

    public String getTitle() {
        return title;
    }

    public Double getVote() {
        return vote;
    }

    public List<Integer> getGenresId() {
        return genresId;
    }

    public String getReleaseDate() {
        return releaseDate;
    }
}

public class itemList_genresId{
    @SerializedName("genre_ids")
    int id;

    public int getId() {
        return id;
    }
 }
}

适配器类

public class genres_adapter extends ArrayAdapter<itemList_model.itemList_Results> {
RetrofitCalls calls;

public genres_adapter(@NonNull Context context, ArrayList<itemList_model.itemList_Results> list) {
    super(context, 0, list);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View itemView = convertView;
    if (itemView == null){
        itemView = LayoutInflater.from(getContext()).inflate(R.layout.genres_item_view, parent, false);
    }
    itemList_model.itemList_Results model = getItem(position);

    TextView title = itemView.findViewById(R.id.title);
    title.setText(model.getTitle());
    TextView vote = itemView.findViewById(R.id.vote);
    vote.setText(String.valueOf(model.getVote()));
    TextView release = itemView.findViewById(R.id.release);
    release.setText(model.getReleaseDate());
    return itemView;
 }
}

活动Java类

ArrayList<itemList_model.itemList_Results> arrayList;
genres_adapter adapter;

String title = "";
Double average_votes = 0.0;
String date = "";

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

    arrayList = new ArrayList<itemList_model.itemList_Results>();
    adapter = new genres_adapter(this, arrayList);

    ListView listView = (ListView) findViewById(R.id.genres_listView);
    listView.setAdapter(adapter);
    getListViewItems();
}
public void getListViewItems(){

    String url = "https://api.themoviedb.org/";
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    apiCall api = retrofit.create(apiCall.class);
    Call<itemList_model> call = api.getHorror();
    call.enqueue(new Callback<itemList_model>() {
        @Override
        public void onResponse(Call<itemList_model> call, Response<itemList_model> response) {
            if (!response.isSuccessful()) {
                Log.i(TAG, "onResponse: " + response.code());
            }
            List<itemList_model.itemList_Results> list = response.body().getResults();
            for (itemList_model.itemList_Results model : list){

                title =model.getTitle();
                average_votes = Double.valueOf(model.getVote());
                date =model.getReleaseDate();
            }
            list.add(new itemList_model.itemList_Results(title,average_votes,date));
        }
        @Override
        public void onFailure(Call<itemList_model> call, Throwable t) {
            Log.i(TAG, "onFailure: "+t.getMessage());
        }
    });
}
    包含ListView的
  • activity称为activity_genres_list_view
  • 适配器将使用的
  • 活动称为genres_item_view

我猜想,这是关于列表中的数据的,也许没有添加?

3 个答案:

答案 0 :(得分:1)

显然,您应该在收到适配器后将其添加到适配器:

adapter.addAll(list);

答案 1 :(得分:1)

我在您的代码中看不到任何notifyDataSetChanged()呼叫。从Retrofit调用中找到响应后,您需要更新传递给适配器的列表,并在适配器上调用notifyDataSetChanged以查看效果。

因此,您可以考虑执行以下操作。

public void getListViewItems(){

    String url = "https://api.themoviedb.org/";
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    apiCall api = retrofit.create(apiCall.class);
    Call<itemList_model> call = api.getHorror();
    call.enqueue(new Callback<itemList_model>() {
        @Override
        public void onResponse(Call<itemList_model> call, Response<itemList_model> response) {
            if (!response.isSuccessful()) {
                Log.i(TAG, "onResponse: " + response.code());
            }

            // Do not create a new list here. Use the arrayList which was declared before and passed to the adapter
            // List<itemList_model.itemList_Results> list = response.body().getResults();

            // Clear the arrayList before pushing new data
            arrayList.clear();

            for (itemList_model.itemList_Results model : list){
                title =model.getTitle();
                average_votes = Double.valueOf(model.getVote());
                date = model.getReleaseDate();

                // Add the data into the arrayList instead of the list
                arrayList.add(new itemList_model.itemList_Results(title,average_votes,date));
            }

            // Now call notifyDataSetChanged on your adapter to see the changes in the ListView
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure(Call<itemList_model> call, Throwable t) {
            Log.i(TAG, "onFailure: "+t.getMessage());
        }
    });
}

希望能解决您的问题。

答案 2 :(得分:0)

什么都没有出现,因为您在获取信息之前正在设置适配器:

    adapter = new genres_adapter(this, arrayList);//array list is still empty here
    listView.setAdapter(adapter);
    getListViewItems();

一旦获得列表,您应该做的就是设置适配器!或在获取信息后致电适配器上的notifydatasetChanged

{
.....
arrayList.add(new itemList_model.itemList_Results(title,average_votes,date));
.....
}
adapter.notifyDataSetChanged();