使用cardview详细信息启动另一项活动

时间:2017-03-02 10:29:55

标签: java android android-recyclerview android-cardview

我想使用cardview详细信息启动另一个活动

enter image description here

本教程 http://androidbash.com/connecting-android-app-to-a-database-using-php-and-mysql/

公共类MoviesAdapter扩展了RecyclerView.Adapter {

private Context context;
private List<Movie> movies;

public MoviesAdapter(Context context, List<Movie> movies) {
    this.context = context;
    this.movies = movies;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card,parent,false);

    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {

    holder.movieName.setText(movies.get(position).getMovieName());
    holder.movieGenre.setText(movies.get(position).getMovieGenre());
    Glide.with(context).load(movies.get(position).getImageLink()).into(holder.imageView);
}



@Override
public int getItemCount() {
    return movies.size();
}

public  class ViewHolder extends  RecyclerView.ViewHolder implements View.OnClickListener{

    public TextView movieName;
    public TextView movieGenre;
    public ImageView imageView;

    public ViewHolder(View itemView) {
        super(itemView);
        movieName = (TextView) itemView.findViewById(R.id.moviename);
        imageView = (ImageView) itemView.findViewById(R.id.image);
        movieGenre = (TextView) itemView.findViewById(R.id.genre);
        imageView.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int position = getAdapterPosition();
        showPopupMenu(v,position);
    }
}

private void showPopupMenu(View view, int poaition) {
    PopupMenu popup = new PopupMenu(context, view);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu_context, popup.getMenu());
    popup.setOnMenuItemClickListener(new MenuClickListener(poaition));
    popup.show();
}


class MenuClickListener implements PopupMenu.OnMenuItemClickListener {
    Integer pos;
    public MenuClickListener(int pos) {
        this.pos=pos;
    }

    @Override
    public boolean onMenuItemClick(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.action_favourite:
                Toast.makeText(context, movies.get(pos).getMovieName()+" is added to favourite", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.action_watch:
                Toast.makeText(context, movies.get(pos).getMovieName()+" is added to watchlist", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.action_book:
                Toast.makeText(context, "Booked Ticket for "+movies.get(pos).getMovieName(), Toast.LENGTH_SHORT).show();
                return true;



            default:
        }
        return false;
    }
}

}

1 个答案:

答案 0 :(得分:0)

必读:https://developer.android.com/training/basics/firstapp/starting-activity.html

当您点击一张卡片时,请将绑定到此卡片的数据放入意图:

@Override
public void onClick(View v) {
    int position = getAdapterPosition();
    showPopupMenu(v,position);

    Movie movie = movies.get(position);
    Intent intent = new Intent(v.getContext(), MovieActivity.class);
    intent.putExtraSerializable(MOVIE_KEY, movie);
    v.getContext().startActivity(intent);
}

要完成这项工作,请调整您的Movie类:

class Movie implements Serializable {// your fields and methods}

然后在MovieActivity onCreate:

Movie movie = (Movie) getIntent().getSerializableExtra(MOVIE_KEY);

使用此实例,您可以使用数据填充视图。