gpt4 book ai didi

java - 使用 GridView、Adapters 和 Picasso 制作流行的电影应用

转载 作者:行者123 更新时间:2023-11-29 20:39:56 25 4
gpt4 key购买 nike

<分区>

我正在尝试制作一个应用程序,我希望我的应用程序在启动时显示流行电影海报的网格。电影海报是从 TheMovieDataBase API 下载的。然后我使用 Picasso 加载图像。对于 GridView,我还使用了自定义适配器。我不明白该怎么做。这是我到目前为止所做的。//删除旧代码

请告诉我我必须做什么才能让我的应用程序看起来像这样:App mock

此应用程序是 Udacity 的“Android 开发”类(class)的一部分,这是他们提供的实现指南:Implementation Guide

编辑:由于有些人将我的问题标记为“过于宽泛”,因此我重述了我的问题。以下代码从 TMDB api 请求电影详细信息,然后以文本形式显示电影名称的网格。现在我想在网格中显示电影海报而不是名字。海报路径存储在 moviePosterPath 字符串数组中。我该怎么做?

public class MainActivityFragment extends Fragment {
ArrayAdapter<String> mMovieAdapter;
String[] movieId, movieTitle, movieReleaseDate, movieVoteAverage, movieOverview, moviePosterPath;
public MainActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_main, container, false);


GridView listView = (GridView) rootView.findViewById(R.id.gridview_movies);
mMovieAdapter = new ArrayAdapter<String>(getActivity(),
R.layout.item_movies,
R.id.image_view_movie,
new ArrayList<String>());
listView.setAdapter(mMovieAdapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
{
Intent intent = new Intent(getActivity(), DetailActivity.class);
String forecast = mMovieAdapter.getItem(i);
String send = "Overview" + movieOverview[i] + "\n" + "Release Date" + movieReleaseDate[i];
intent.putExtra(Intent.EXTRA_TEXT, send);
startActivity(intent);
}
});
return rootView;
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.moviefragment, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_refresh) {
updateMovie();
return true;
}
return super.onOptionsItemSelected(item);
}

public void onStart()
{
super.onStart();
updateMovie();
}

private void updateMovie() {
FetchMovieTask movieTask = new FetchMovieTask();
movieTask.execute();
}

class FetchMovieTask extends AsyncTask<Void, Void, String[]> {
private final String LOG_TAG = FetchMovieTask.class.getSimpleName();

@Override
protected String[] doInBackground(Void... params) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;

// Will contain the raw JSON response as a string.
String movieJsonStr = null;

try {
URL url = new URL("http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=c20129fdf73b5df3ab44548ad7f73586");

urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();

// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));

String line;
while ((line = reader.readLine()) != null) {

buffer.append(line + "\n");
}

if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
movieJsonStr = buffer.toString();

} catch (IOException e) {
Log.e(LOG_TAG, "Error ", e);
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e(LOG_TAG, "Error closing stream", e);
}
}
}
try {
return getMovieDataFromJson(movieJsonStr);
} catch (JSONException j) {
Log.e(LOG_TAG, "JSON Error", j);
}
return null;
}

private String[] getMovieDataFromJson(String forecastJsonStr)
throws JSONException {
JSONObject movieJson = new JSONObject(forecastJsonStr);
JSONArray movieArray = movieJson.getJSONArray("results");
movieId = new String[movieArray.length()];
movieTitle = new String[movieArray.length()];
movieReleaseDate = new String[movieArray.length()];
movieVoteAverage = new String[movieArray.length()];
movieOverview = new String[movieArray.length()];
moviePosterPath = new String[movieArray.length()];
for (int i = 0; i < movieArray.length(); i++)
{
JSONObject movie = movieArray.getJSONObject(i);
movieId[i] = movie.getString("id");
movieTitle[i] = movie.getString("original_title");
movieReleaseDate[i] = movie.getString("release_date");
movieVoteAverage[i] = movie.getString("vote_average");
movieOverview[i] = movie.getString("overview");
moviePosterPath[i] = movie.getString("poster_path");
}
return movieTitle;
}

@Override
protected void onPostExecute(String[] strings)
{
super.onPostExecute(strings);
mMovieAdapter.clear();
mMovieAdapter.addAll(strings);
}
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com