您好我有一个简单的应用程序,其中包含带有列表项的recyclerView(每个都代表一个&#34; Movie&#34;对象。电影列表项的内容是通过API调用生成的(themoviedb.org)。我和#39;我试图打开一个&#34;详细信息片段&#34;点击一个recyclerView列表项。我希望点击Movie项目中的所有内容也出现在下一个片段中。现在我点击列表项,没有任何反应。没有崩溃,甚至。我认为我已经正确设置了监听器界面,但也许这就是问题所在。任何人都可以看到我出错的地方吗?先谢谢。< / p>
这是第一次出现RecyclerView(电影列表)的片段:
public class ResultsFragment extends Fragment {
private RecyclerView mRecyclerView;
Movie_Results_Adapter results_adapter;
ArrayList<Movie> movieList;
onMovieSelectedListener listener;
public ResultsFragment(){
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_layout_1, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler);
mRecyclerView.setVisibility(View.VISIBLE);
results_adapter = new Movie_Results_Adapter(movieList, listener);
listener = new onMovieSelectedListener() {
@Override
public void onMovieSelected(Movie selectedMovie) {
Intent launchIntent = new Intent(getContext(), MovieDetailActivity.class);
launchIntent.putExtra(MovieDetailActivity.EXTRA_MOVIE, "movie");
Log.d("PaulsApp", selectedMovie + "");
launchIntent.putExtra(MovieDetailActivity.NEW_EXTRA, (Serializable) selectedMovie);
startActivity(launchIntent);
}
};
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(results_adapter);
Log.d("TAGG", "AAAH" + movieList.toString());
return rootView;
}
public void parseJson(String jsonToParse) throws JSONException {
JSONObject responseObject= new JSONObject(jsonToParse);
JSONArray resultsArray = responseObject.getJSONArray("results");
for(int i = 0; i < resultsArray.length(); i++){
JSONObject movie = resultsArray.getJSONObject(i);
String movieName = movie.getString("title");
String movieReleaseDate = movie.getString("release_date");
String movieScore = movie.getString("vote_average");
String moviePopularity = movie.getString("vote_count");
String movieImage = movie.getString("poster_path");
String movieDescription = movie.getString("overview");
Movie movie1 = new Movie(movieName, movieReleaseDate, movieScore, moviePopularity, movieImage, movieDescription);
movieList.add(movie1);
}
}
@Override
public void setArguments(Bundle args) {
movieList = new ArrayList<>();
String arguments = args.getString("JSON_STRING");
try {
parseJson(arguments);
} catch (JSONException e) {
e.printStackTrace();
}
super.setArguments(args);
}
public interface onMovieSelectedListener{
public void onMovieSelected(Movie selectedMovie);
}
}
这是&#34;结果&#34;的RecyclerView适配器。片段:
public class Movie_Results_Adapter extends RecyclerView.Adapter<Movie_Results_Adapter.ResultViewHolder>{
ArrayList<Movie> MovieListArray;
ResultsFragment.onMovieSelectedListener listener;
private Activity mActivity;
public class ResultViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView titleText;
TextView dateText;
TextView scoreText;
TextView popularityText;
ImageView movieImage;
public Movie movie;
public ResultViewHolder(View itemView){
super(itemView);
titleText = (TextView) itemView.findViewById(R.id.movie_title_text);
dateText = (TextView) itemView.findViewById(R.id.movie_date_text);
scoreText = (TextView) itemView.findViewById(R.id.movie_score_text);
popularityText = (TextView) itemView.findViewById(R.id.movie_popularity_text);
movieImage = (ImageView) itemView.findViewById(R.id.movie_image);
}
@Override
public void onClick(View view) {
if (listener != null) {
listener.onMovieSelected(MovieListArray.get(getAdapterPosition()));
Toast.makeText(view.getContext(), "clicked: " + MovieListArray.get(getAdapterPosition()), Toast.LENGTH_SHORT).show();
}
}
}
public Movie_Results_Adapter(ArrayList<Movie> movieList, ResultsFragment.onMovieSelectedListener listener){
MovieListArray = movieList;
this.listener = listener;
}
@Override
public ResultViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_result_list_item, parent, false);
ResultViewHolder vh = new ResultViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ResultViewHolder holder, int position) {
holder.titleText.setText(MovieListArray.get(position).getmTitle());
Log.d("TAGG", MovieListArray.toString());
holder.dateText.setText("Released "+MovieListArray.get(position).getmReleaseYear());
holder.scoreText.setText("TMDB Score: "+MovieListArray.get(position).getmScore());
holder.popularityText.setText(MovieListArray.get(position).getmPopularity()+" votes");
Context context = holder.movieImage.getContext();
Picasso.with(context).load(Constants.TMDB_POSTER+MovieListArray.get(position).getmImage()).into(holder.movieImage);
}
@Override
public int getItemCount() {
return MovieListArray.size();
}
}
以下是详细信息片段(以及随附的活动):
package jetsetpaul.movienerd;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
/**
* Created by pauljoiner on 8/17/16.
*/
public class MovieDetailFragment extends Fragment {
public String movieTitle;
public String movieImage;
public String movieDate;
public String movieDescription;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.movie_detail_fragment, container, false);
TextView mTitleText = (TextView) rootView.findViewById(R.id.movie_detail_title);
ImageView mMovieImage = (ImageView) rootView.findViewById(R.id.movie_detail_image);
TextView mDateText = (TextView) rootView.findViewById(R.id.movie_detail_release_date);
TextView mDescriptionText = (TextView) rootView.findViewById(R.id.movie_detail_description);
mTitleText.setText(movieTitle);
mDateText.setText(movieDate);
mDescriptionText.setText(movieDescription);
Context context = mMovieImage.getContext();
Picasso.with(context).load(Constants.TMDB_POSTER+movieImage);
return rootView;
}
public void setMovieTitle(String title){
this.movieTitle = title;
}
public void setMovieImage(String image){
this.movieImage = image;
}
public void setMovieDate(String date){
this.movieDate = date;
}
public void setMovieDescription(String description){
this.movieDescription = description;
}
}
活性: 包jetsetpaul.movienerd;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
/**
* Created by pauljoiner on 8/17/16.
*/
public class MovieDetailActivity extends AppCompatActivity {
public static final String EXTRA_MOVIE = "movie";
public static final String NEW_EXTRA = "movie2";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_detail);
Intent i = getIntent();
if(i.hasExtra(NEW_EXTRA)) {
Log.d("PaulsApp", "Had Extra!");
//set the text in my fragment
Movie movie = (Movie) i.getSerializableExtra(NEW_EXTRA);
Fragment newFragment = new MovieDetailFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_fragment, newFragment);
transaction.commit();
((MovieDetailFragment) newFragment).setMovieTitle(movie.getmTitle());
((MovieDetailFragment) newFragment).setMovieImage(movie.getmImage());
((MovieDetailFragment) newFragment).setMovieDate(movie.getmReleaseYear());
((MovieDetailFragment) newFragment).setMovieDescription(movie.getmDescription());
}
}
}
如有必要,我可以发布更多代码。再次感谢!
答案 0 :(得分:1)
您的听众需要在results_adapter
之前创建listener = new onMovieSelectedListener() {
@Override
public void onMovieSelected(Movie selectedMovie) {
Intent launchIntent = new Intent(getContext(), MovieDetailActivity.class);
launchIntent.putExtra(MovieDetailActivity.EXTRA_MOVIE, "movie");
Log.d("PaulsApp", selectedMovie + "");
launchIntent.putExtra(MovieDetailActivity.NEW_EXTRA, (Serializable) selectedMovie);
startActivity(launchIntent);
}
};
results_adapter = new Movie_Results_Adapter(movieList, listener);
老实说,最好让你的片段实现监听器(接口),而不是必须使用新的onMovieSelectedListener()来创建它。