我正试图通过意图在活动之间传递图像/海报。我使用过Picasso库和Recycler Adapter。但是在获得意图后我无法在捆绑附加功能中设置图像。年后和头衔。
public class ShowMovieDetails extends AppCompatActivity implements OnNetworkCallHandled,RecyclerViewAdapter.OnClickListener {
private RecyclerViewAdapter adapter;
public RecyclerView recyclerView;
private DataWrapper list;
public Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_movie_details);
context=this;
Intent intent = getIntent();
String movieName = intent.getExtras().getString("moviename", "");
if (movieName != null) {
new MovieTask(movieName, this).execute();
}
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public void onNetworkcallSuccess(Object object) {
//json to gson
String result = (String) object;
Gson gson = new Gson();
list = gson.fromJson(result, DataWrapper.class);
adapter = new RecyclerViewAdapter(list.getSearch(),this, this);
recyclerView.setAdapter(adapter);
System.out.print(result);
System.out.println("On success");
}
@Override
public void onNetworkcallFailure(Object object) {
System.out.println("On failure");
}
@Override
public void onClick(View view, int position, long itemId) {
startActivity(position);
System.out.println();
}
private void startActivity(int position) {
Intent intent = new Intent(this, DetailActivity.class);
Bundle extras = new Bundle();
extras.putString("Title", list.getSearch().get(position).getTitle());
extras.putString("Year", list.getSearch().get(position).getYear());
extras.putString("Poster",list.getSearch().get(position).getPoster());
intent.putExtras(extras);
startActivity(intent);
}
}
和另外一个m在外部设置的东西
public class DetailActivity extends AppCompatActivity {
private TextView Title;
private TextView Year;
private ImageView Image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
Title = (TextView) findViewById(R.id.Title);
Year = (TextView) findViewById(R.id.Year);
Image = (ImageView) findViewById(R.id.iVPoster);
if (intent != null) {
String title = extras.getString("Title");
String year=extras.getString("Year");
String image=extras.getString("Poster");
Title.setText(title);
Year.setText(year);
System.out.println();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
我的recyclerAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MovieDetailsHolder> {
private ArrayList<MovieDetails> movieDetails;
private Context context;
private OnClickListener onClick;
RecyclerViewAdapter(ArrayList<MovieDetails> movieDetails, Context context, OnClickListener onClick) {
this.onClick = onClick;
this.movieDetails = movieDetails;
this.context = context;
}
@Override
public MovieDetailsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent, false);
return new MovieDetailsHolder(view);
}
@Override
public void onBindViewHolder(MovieDetailsHolder holder, final int position) {
holder.tVMovieName.setText(movieDetails.get(position).getTitle());
Picasso
.with(context)
.load(movieDetails.get(position).getPoster())
.into(holder.iVPoster);
holder.setOnClickListener(onClick);
}
@Override
public int getItemCount() {
return movieDetails.size();
}
public static class MovieDetailsHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private OnClickListener onClickListener;
CardView cv;
TextView tVMovieName;
ImageView iVPoster;
MovieDetailsHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cardView);
tVMovieName = (TextView) itemView.findViewById(R.id.tVMovieName);
iVPoster = (ImageView) itemView.findViewById(R.id.iVPoster);
itemView.setOnClickListener(this);
}
public void setOnClickListener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
@Override
public void onClick(View v) {
onClickListener.onClick(v, getPosition(), getItemId());
}
}
public interface OnClickListener {
public void onClick(View view, int position, long itemId);
}
}
答案 0 :(得分:0)
在startActivity()函数中,就像获得Title,Year和Poster一样;还可以获取图片的网址。