如何使用流派ID获得流派名称? (themoviedb API)

时间:2019-05-30 16:56:52

标签: java android arraylist compare

我想在每个详细活动中显示每个电影的流派名称。我将genre_ids附加到带有请求的电影上,并将类型列表与id和名称附加到另一个请求。

例如,我需要用“动作,戏剧,科幻小说”代替“ 28、18、878”

screenshot

我不是Java / Android的专业人士,所以我不知道在代码中我可以比较两个id来获取名称并显示它。

我的代码:

MainActivity.java:

lambda

MovieDetailActivity.java:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private RecyclerView moviesView;
    private MoviesAdapter adapter;
    private GridLayoutManager gridLayoutManager;

    private List<Movie> moviesList;

    private int currentPage = 1;
    private boolean movieSearch = false;

    private static final String TAG = "MainActivity";

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

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        moviesView = (RecyclerView) findViewById(R.id.movies_list);
        moviesList = new ArrayList<>();
        load_movies(currentPage);
        gridLayoutManager = new GridLayoutManager(this,2);
        moviesView.setLayoutManager(gridLayoutManager);
        adapter = new MoviesAdapter(this, moviesList);
        moviesView.setAdapter(adapter);

    }

    public String getLocale() {
        String countryCode = Locale.getDefault().getCountry();
        String languageCode = Locale.getDefault().getLanguage();
        String localeCode = languageCode + "-" + countryCode;

        return localeCode;
    }

    private void load_movies(int id) {

        AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer, Void, Void>() {
            @Override
            protected Void doInBackground(Integer... integers) {

                OkHttpClient client = new OkHttpClient();

                Request request = new Request.Builder()
                        .url(BASE_URL + "/discover/movie?api_key=" + API_KEY + "&language=" + getLocale() + "&sort_by=popularity.desc&include_adult=false&include_video=false&page=" + integers[0])
                        .build();
                try {
                    Response response = client.newCall(request).execute();
                    JSONObject root = new JSONObject(response.body().string());
                    JSONArray array = root.getJSONArray("results");

                    for (int i = 0; i < array.length(); i++) {

                        JSONObject object = array.getJSONObject(i);
                        Movie data = new Movie();

                        if (object != null) {
                            JSONArray genresids = object.optJSONArray("genre_ids");
                            ArrayList<String> genre = new ArrayList<String>();
                            for (int j = 0; j < genresids.length(); j++) {                              
                                genre.add((String) genresids.getString(j));                               
                            }
                            data.setGenres(genre);
                        }


                        data.setTitle(object.getString("title"));
                        data.setPosterPath(object.getString("poster_path")); 
data.setBackdropPath(object.getString("backdrop_path"));
                        data.setReleaseDate(object.getString("release_date"));
                        data.setOverview(object.getString("overview"));
                        data.setTMDBRating(object.getDouble("vote_average"));

                        moviesList.add(data);
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    System.out.println("End of content");
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                adapter.notifyDataSetChanged();
            }
        };

        task.execute(id);
    }

}

0 个答案:

没有答案