当前位置: 首页 > 知识库问答 >
问题:

当我手动将自定义对象添加到列表中时,RecycleView可以使用它,但当我使用RESTful服务中的数据实例化它时,它不能使用

凤扬
2023-03-14

我正在Android Studio中做一个项目,我需要在一个RecyclerView中列出几部电影(在本例中,来自RESTful API的几部意味着20)。现在我已经设置好了所有内容,我的静态虚拟内容显示在使用cardview的recyclerview中。然而,当我切换到RESTful API时,我的真实数据(来自RESTful API)会出现问题。这是我的主要活动。带有手动数据的java代码(显示数据时):

public class MainActivity extends RecyclerViewActivity {

private static List<Film> listFilm;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setLayoutManager(new LinearLayoutManager(this));
    setAdapter(new FilmAdapter());

    listFilm = new ArrayList<>();

    listFilm.add(new Film("Star Wars", "Princess Leia is captured and held hostage by the evil Imperial forces in their effort to take over the galactic Empire. Venturesome Luke Skywalker and dashing captain Han Solo team together with the loveable robot duo R2-D2 and C-3PO to rescue the beautiful princess and restore peace and justice in the Empire."));
    listFilm.add(new Film("E.T. the Extra-Terrestrial", "A science fiction fairytale about an extra-terrestrial who is left behind on Earth and is found by a young boy who befriends him. This heart-warming fantasy from Director Steven Spielberg became one of the most commercially successful films of all time."));
    listFilm.add(new Film("Jurassic Park", "A wealthy entrepreneur secretly creates a theme park featuring living dinosaurs drawn from prehistoric DNA. Before opening day, he invites a team of experts and his two eager grandchildren to experience the park and help calm anxious investors. However, the park is anything but amusing as the security systems go off-line and the dinosaurs escape."));
    listFilm.add(new Film("The Lion King", "A young lion cub named Simba can't wait to be king. But his uncle craves the title for himself and will stop at nothing to get it."));
    listFilm.add(new Film("Independence Day", "On July 2, a giant alien mothership enters orbit around Earth and deploys several dozen saucer-shaped 'destroyer' spacecraft that quickly lay waste to major cities around the planet. On July 3, the United States conducts a coordinated counterattack that fails. On July 4 the a plan is devised to gain access to the interior of the alien mothership in space in order to plant a nuclear missile."));
    listFilm.add(new Film("Titanic", "84 years later, a 101-year-old woman named Rose DeWitt Bukater tells the story to her granddaughter Lizzy Calvert, Brock Lovett, Lewis Bodine, Bobby Buell and Anatoly Mikailavich on the Keldysh about her life set in April 10th 1912, on a ship called Titanic when young Rose boards the departing ship with the upper-class passengers and her mother, Ruth DeWitt Bukater, and her fiancé, Caledon Hockley. Meanwhile, a drifter and artist named Jack Dawson and his best friend Fabrizio De Rossi win third-class tickets to the ship in a game. And she explains the whole story from departure until the death of Titanic on its first and last voyage April 15th, 1912 at 2:20 in the morning."));
    listFilm.add(new Film("Star Wars: Episode I - The Phantom Menace", "Anakin Skywalker, a young slave strong with the Force, is discovered on Tatooine. Meanwhile, the evil Sith have returned, enacting their plot for revenge against the Jedi."));
    listFilm.add(new Film("Harry Potter and the Philosopher's Stone", "Harry Potter has lived under the stairs at his aunt and uncle's house his whole life. But on his 11th birthday, he learns he's a powerful wizard -- with a place waiting for him at the Hogwarts School of Witchcraft and Wizardry. As he learns to harness his newfound powers with the help of the school's kindly headmaster, Harry uncovers the truth about his parents' deaths -- and about the villain who's to blame."));
    listFilm.add(new Film("The Lord of the Rings: The Fellowship of the Ring", "Young hobbit Frodo Baggins, after inheriting a mysterious ring from his uncle Bilbo, must leave his home in order to keep it from falling into the hands of its evil creator. Along the way, a fellowship is formed to protect the ringbearer and make sure that the ring arrives at its final destination: Mt. Doom, the only place where it can be destroyed."));
    listFilm.add(new Film("Spider-Man", "After being bitten by a genetically altered spider, nerdy high school student Peter Parker is endowed with amazing powers."));

}

电影模型的适配器是:

private class FilmAdapter extends RecyclerView.Adapter<RowHolder> {

    Context context;

    public Context getContext() {
        return this.context;
    }

    @Override
    public RowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RowHolder(getLayoutInflater().inflate(R.layout.row, parent, false));
    }

    @Override
    public void onBindViewHolder(RowHolder holder, int position) {
        Film movie = listFilm.get(position);

        TextView title   = holder.title;
        title.setText(movie.getTitle());
        TextView desc    = holder.desc;
        desc.setText(movie.getDescription());
        ImageView poster = holder.poster;
        Picasso.with(getApplicationContext()).load("http://cdn2-www.comingsoon.net/assets/uploads/2015/03/avengersorder5.jpg").into(poster);
    }

    @Override
    public int getItemCount() {
        return listFilm.size();
    }
}

而ViewHolder类是:

private class RowHolder extends RecyclerView.ViewHolder {

    TextView  title  = null;
    TextView  desc   = null;
    ImageView poster = null;

    public RowHolder(View itemView) {
        super(itemView);

        title  = (TextView)  itemView.findViewById(R.id.title);
        desc   = (TextView)  itemView.findViewById(R.id.desc);
        poster = (ImageView) itemView.findViewById(R.id.poster);
    }

}

当联系我的RESTful API Main Activity.java看起来像:

公共类MainActivity扩展了RecycleServiceActivity{

private static List<Film> listFilm;
public static final String apiURL = "https://api.themoviedb.org/4/list/10?page=1&api_key=8e20230f25939a349c2e37680cdaff95&sort_by=release_date.asc";
private JSONObject jsonObject;
private JSONArray  jsonArray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setLayoutManager(new LinearLayoutManager(this));
    setAdapter(new FilmAdapter());

    listFilm = new ArrayList<>();

    RequestQueue queue = Volley.newRequestQueue(this);

    StringRequest stringRequest = new StringRequest(Method.GET, apiURL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(getApplicationContext(), "RADI", Toast.LENGTH_SHORT).show();

                    try {


                        jsonObject = new JSONObject(response);
                        jsonArray  = jsonObject.getJSONArray("results");
                        int i = 0;

                        while(i < jsonArray.length()) {
                            JSONObject movie = jsonArray.getJSONObject(i);

                            Film movieInstance = new Film(movie.getString("original_title"), movie.getString("overview"), "https://image.tmdb.org/t/p/w500" + movie.getString("backdrop_path"));
                            listFilm.add(movieInstance);
                            i ++ ;
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), "NE RADI", Toast.LENGTH_SHORT).show();
        }
    });

}

其余的在这两种情况下都是一样的。我记录了几次所有的东西,数据是有效的,所有的东西都很好,列表中充满了20部电影,所有的电影都对应于应用编程接口。所以有什么想法吗?总结一下,在第一种情况下,我让应用程序工作正常,在后一种情况下,没有显示数据。有人知道或有类似的问题吗?提前谢谢你。

共有1个答案

秦鸿羽
2023-03-14

您必须让适配器知道您更改了数据。它将通知所附的回收商,并更新其视图。

在从api添加项的循环之后,添加以下内容:

adapter.notifyDataSetChanged();

在第一个例子中,你不需要这个,因为在回收商进行初始布局(onCreate之后)时,内容已经存在了。

 类似资料: