2017-03-26 5 views
0

Android StudioのプロジェクトでRecyclerViewに複数のムービーをリストする必要がありましたこの場合はRESTful APIからいくつかの手段20)。今私はすべてを設定して、私の静的なダミーのコンテンツは、cardviewを使用してrecyclerview内に表示されます。それにもかかわらず、私はそれに切り替えると私の実際のデータ(RESTful APIから)に問題があります。 (データが表示されている場合)ここでは、マニュアルのデータと私のMainActivity.javaコードは次のとおりです。作品のモデルのRecyclerViewは、リストに手動で追加するとカスタムオブジェクトと一緒に動作しますが、RESTfulサービスのデータでインスタンス化するときは動作しません。

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 MainActivity.javaに連絡すると、次のようになります。

public class MainActivity extends R ecyclerViewActivity {

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(); 
     } 
    }); 

}

残りは両方の場合で同じです。私は何度も何度もログに記録し、データはすべて有効です。だから、どんな考え?最初のケースでは、アプリケーションは正常に動作していますが、後者ではデータは表示されません。誰もが知っている、または同様の問題があった?前もって感謝します。

答えて

0

データを変更したことをアダプタに知らせる必要があります。それは接続されたリサイクラに通知し、そのビューを更新します。あなたは、APIから項目を追加するループした後

、この追加:時点でリサイクル業者が(のonCreate後)その初期レイアウトを行うので、あなたはこれを必要としない最初の例では

adapter.notifyDataSetChanged(); 

を、内容は既にそこにあります。

+0

感謝します!それを試して、魅力のように動作します。これを知らなかった再びありがとう。 – nikjov92

+0

問題ありません。始めると、[公式ドキュメント](https://developer.android.com/guide/index.html)で閲覧できます。たとえば、[このページ](https://developer.android.com)があります。 /guide/topics/ui/layout/recyclerview.html)を参照してください。また、問題を解決したときには必ず回答を受け入れるようにしてください。 – RobCo

+0

私は試しましたが、私がここに来てから明らかに投票できません。私はすぐにその矢印キーを押し上げた。私は1つの評判を持っています(現在) – nikjov92

関連する問題