使用整数数据类型和putextra加载图像

时间:2019-02-02 17:21:34

标签: java android api oop

正在处理需要打开带有图像的新活动的回收者视图。我想通过putextra方法传递图像,代码可以很好地编译,但是在第二个活动中显示为空白。我已经读过大约9个月前noogui提出的类似问题,例如“使用putExtra传递图像”,还有其他问题...不适合...

这些是我的代码段:

Recycler View适配器:

    private static final String TAG = "RecyclerViewAdapter";
    private ArrayList<String> mImageNames= new ArrayList<>();
    private ArrayList<Integer> mImages= new ArrayList<>();
    private Context mContext;
    public RecyclerViewAdapter(Context context, 
    ArrayList<String>imageNames,ArrayList<Integer>images) {
    mContext = context;
    mImages=images;
    mImageNames=imageNames;
    }


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

    LayoutInflater.from(parent.getContext()).
    inflate(R.layout.profil,parent,false);
    ViewHolder holder = new ViewHolder(view);
    return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, 
    @SuppressLint("RecyclerView") final int position) {
    Log.d(TAG, "onBindViewHolder: called");

    Glide.with(mContext)
            .asBitmap()
            .load(mImages.get(position))
            .into(holder.Image);
    holder.ImageName.setText(mImageNames.get(position));
    holder.ParentLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d(TAG, "onClick: "+ mImageNames.get(position));
            Intent intent = new Intent(mContext, GalleryActivity.class);
            intent.setAction(Intent.ACTION_SEND);
            intent.putExtra("", mImages.get(position));
            intent.putExtra("mImageNames", mImageNames.get(position));
            mContext.startActivity(intent);
           // Toast.makeText(mContext,mImageNames.get(position), 
                    Toast.LENGTH_SHORT).show();

                   }
                 });
          }

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

隐性活动:

    if(getIntent().hasExtra("image_url") && 
    getIntent().hasExtra("mImageNames")){
        final int imageUrl = 
    getIntent().getIntExtra("image_url",R.drawable.first);
        final String imageName = 
    getIntent().getStringExtra("mImageNames");

        //Toast.makeText(this,imageName,Toast.LENGTH_SHORT).show();
        //Toast.makeText(this,imageUrl,Toast.LENGTH_SHORT).show();
        //both Toasts are not showing... meanwhile it works on the main 
          activity???
        setImage(imageUrl,imageName);
     }
    }
    private void setImage(int imageUrl, String imageName){
    Log.d(TAG, "setImage: set");
    TextView name = findViewById(R.id.image_description);
    name.setText(imageName);

    ImageView image= findViewById(R.id.image);
    Glide.with(this).load(imageUrl).into(image);
    }

请帮忙吗???

0 个答案:

没有答案