MainActivityFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    GridView gridView = (GridView) rootView.findViewById(R.id.grid_view_movies);
    final ImageAdapter mAdapter= new ImageAdapter(getActivity(),listMovie);
    gridView.setAdapter(mAdapter);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            String movieInfo= listMovie.get(position);
            Intent i = new Intent( getActivity() ,DetailsMovies.class)
                    .putExtra(Intent.EXTRA_TEXT,  movieInfo);
            startActivity(i);
        }
    });


    return rootView;
}


private void sendJsonRequest() {
    //In the case of theMovieDB it is JSON Object Request
    //Specify several argument in JSON Object Request
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
            getRequestUrl("vote_average"),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    parseJsonResponse(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
    requestQueue.add(request);

}

private void parseJsonResponse(JSONObject response) {
    if (response == null || response.length() == 0) {
        return;
    }

    long id = -1;
    String title = Constants.NA;
    String releaseDate = Constants.NA;
    String synopsis = Constants.NA;
    String urlThumbnail = Constants.NA;
    String rating = Constants.NA;

    try {
        if (response.has(KEY_RESULTS)) {
            JSONArray jsonArray = response.getJSONArray(KEY_RESULTS);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject currentMovies = jsonArray.getJSONObject(i);
                //Get the id of the current movie
                //If statement is used to check whether id is null or not.
                if (currentMovies.has(KEY_ID) && !currentMovies.isNull(KEY_ID)) {
                    id = currentMovies.getLong(KEY_ID);
                }
                //Get the synopsis of the current movie
                if (currentMovies.has(KEY_OVERVIEW) && !currentMovies.isNull(KEY_OVERVIEW)) {
                    synopsis = currentMovies.getString(KEY_OVERVIEW);
                }
                //Get the title of the current movie
                if (currentMovies.has(KEY_TITLE) && !currentMovies.isNull(KEY_TITLE)) {
                    title = currentMovies.getString(KEY_TITLE);
                }
                //Get the urlThumbnail of the current movie
                if (currentMovies.has(KEY_POSTER_PATH) && !currentMovies.isNull(KEY_POSTER_PATH)) {
                    urlThumbnail = currentMovies.getString(KEY_POSTER_PATH);
                }
                //Get the release date of the current movie
                if (currentMovies.has(KEY_RELEASE_DATE) && !currentMovies.isNull(KEY_RELEASE_DATE)) {
                    releaseDate = currentMovies.getString(KEY_RELEASE_DATE);
                }
                //Get the rating of current movie
                if (currentMovies.has(KEY_VOTE_AVERAGE) && !currentMovies.isNull(KEY_VOTE_AVERAGE)) {
                    rating = currentMovies.getString(KEY_VOTE_AVERAGE);
                }
                //Create movie object
                movie=new Movie();
                movie.setId(id);
                movie.setTitle(title);
                movie.setUrlThumbnail("http://image.tmdb.org/t/p/w185/" + urlThumbnail);
                movie.setReleaseDate(releaseDate);
                movie.setOverview(synopsis);
                movie.setRating(rating);
                //This decides when to add movies to the ArrayList
                if (id != -1 && !title.equals(Constants.NA)) {
                    listMovie.add(movie);
                }

            }

        }

    } catch (JSONException e) {
        Log.e("error", e.getMessage());

    }

}

public static String getRequestUrl(String sortby) {
    return URL + sortby + UrlEndpoints.URL_PARAM + MyApplication.API_KEY;
}


//Base Adapter which is used to put poster in grid view
public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<Movie> movieItems;

    public ImageAdapter(Context c, ArrayList<Movie> movieList) {
        this.mContext = c;
        this.movieItems = movieList;
    }

    public int getCount() {
        return movieItems.size();
    }

    public Object getItem(int position) {
        return movieItems.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        if (inflater == null) {
            inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.grid_item_movies, null);
        }

            mNetworkImageView = (NetworkImageView) convertView.findViewById
                    (R.id.networkImageView);
            //Getting movie data for the row
            Movie m = movieItems.get(position);
            //Thumbnail Image
            //ImageLoader is used to load the images from json object retrieved.
            imageLoader=VolleySingleton.getInstance().getImageLoader();
            mNetworkImageView.setImageUrl(m.getUrlThumbnail(), imageLoader);
            return convertView;
        }

    }


}

DetailActivityFragment.java

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

    View rootView= inflater.inflate(R.layout.fragment_details_movies, container, false);
    Intent intent=getActivity().getIntent();

    if(intent!=null && intent.hasExtra(Intent.EXTRA_TEXT))
    {
        mMovieStr= intent.getStringExtra(Intent.EXTRA_TEXT);
        TextView t = (TextView) rootView.findViewById(R.id.movie_detail);
        t.setText(mMovieStr);
    }
    return rootView;
}


我无法从图像适配器检索电影的详细信息。当用户单击特定的电影海报时,有什么方法可以将电影标题,简介,图像URL传递给另一个活动。

最佳答案

我认为这是一个问题。
String movieInfo= listMovie.get(position);
您的arraylist listMovie是Movie对象的列表,因此每个对象都是Movie

Movie movieSelected = listMovie.get(position);

字符串moviveInfo = movieSelected.getInfor();

字符串moviceTitle = movieSelected.getTitle();

...

希望有帮助

09-11 17:58