在android中替换Switch Case

时间:2017-09-16 05:10:42

标签: android switch-statement

我有一个recyclerview with an 30 images and on click of particular image i am opening a new activity which shows a different image.I have uploaded my images to a server and accessing the image from there. When my activity is opened on click of recyclerview`我通过意图传递位置,以便我可以知道点击了哪个图像,然后我在我的活动中使用switch case并加载我想要的任何图像。我写了30切换的情况,因为我有30个图像。有任何替代方案。我不想使用if和else if。

public class ModelLineUpAdapter extends RecyclerView.Adapter<ModelLineUpAdapter.MyViewHolder> {

    private Context context;

    private List<Bikers> bikersList;

    public ModelLineUpAdapter(List<Bikers> bikersList,Context context) {
        this.bikersList=bikersList;
        this.context = context;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView bikesImageView;
        public TextView nameTextView, priceTextView;
        private Button brocheurebutton;
        private final Context context;

        public MyViewHolder(View itemView) {
            super(itemView);
            context = itemView.getContext();
            bikesImageView = itemView.findViewById(R.id.bikelistitemImageview);
            nameTextView = itemView.findViewById(R.id.bikelistitemname);
            priceTextView = itemView.findViewById(R.id.bikelistitemprice);
            brocheurebutton = itemView.findViewById(R.id.bikelistitembutton);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, ModelLineUpInnerActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("position",getAdapterPosition());
            context.startActivity(intent);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.bikelistitem, null);

        return new MyViewHolder(view);
    }

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

        Bikers bikers = bikersList.get(position);
        holder.nameTextView.setText(bikers.getBikename());
        holder.priceTextView.setText(bikers.getBikeprice());

        Glide.with(context).load(bikers.getBikeImageUrl()).into(holder.bikesImageView);

    }


    @Override
    public int getItemCount() {
        //Log.i(TAG, "getItemCount: "+bikersList.size());
        return  bikersList == null ? 0 : bikersList.size();
    }

}

1 个答案:

答案 0 :(得分:0)

只需有意图传递图片网址并收到ModelLineUpInnerActivity

@Override
        public void onClick(View v) {
            Intent intent = new Intent(context, ModelLineUpInnerActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("position",getAdapterPosition());
            intent.putExtra("IMG",bikersList.get(getAdapterPosition()).getBikeImageUrl());
            context.startActivity(intent);
        }