在下一个活动中,通过捆绑发送的位图图像是随机的

时间:2019-04-21 07:09:09

标签: android bitmap bundle

我有第一个Activity,其中一个recyclerview包含3个元素,这些元素通过下一个Activity中的包发送。这两个文本元素很容易获得。

但是,当我通过转换位图放置image时,它会显示前一个活动的项目列表中的随机图像。

这是我的第一个活动的recyclerview adapter代码:

public class FurnitureAdapter extends RecyclerView.Adapter<FurnitureAdapter.ViewHolder> {

    //All methods in this adapter are required for a bare minimum recyclerview adapter
    private ArrayList<ItemsModel> itemList;
    private Context context;
    private Bitmap bitmap;
    private BitmapDrawable bitmapDrawable;

    // Constructor of the class
    public FurnitureAdapter(ArrayList<ItemsModel> itemList, Context context) {
        this.itemList = itemList;
        this.context = context;
    }

    // get the size of the list
    @Override
    public int getItemCount() {
        return itemList == null ? 0 : itemList.size();
    }

    // specify the row layout file and click for each row
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_recycler_item, parent, false);
        ViewHolder myViewHolder = new ViewHolder(view);
        return myViewHolder;
    }

    // load data in each row element
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int listPosition) {
        ImageView imageView = holder.imageView;
        TextView title = holder.title;
        TextView description = holder.description;
        title.setText(itemList.get(listPosition).getTitle());
        description.setText(itemList.get(listPosition).getDescription());
        imageView.setImageDrawable(itemList.get(listPosition).getImage());
        //TODO: Use this line to set drawable in a variable
        bitmapDrawable = (BitmapDrawable) itemList.get(listPosition).getImage();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView imageView;
        public TextView title, description;

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            imageView = (ImageView) itemView.findViewById(R.id.item_image_view);
            title = (TextView) itemView.findViewById(R.id.item_title);
            description = (TextView) itemView.findViewById(R.id.item_description);

        }

        @Override
        public void onClick(View v) {
            Intent i = new Intent(context, ItemDescriptionsActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("item_title", title.getText().toString());
            bundle.putString("item_description", description.getText().toString());

            //Lines added to get BitmapDrawable and then get bitmap from bitmapDrawable
            bitmapDrawable = (BitmapDrawable) itemList.get(listPosition).getImage();
            bitmap = bitmapDrawable.getBitmap();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
            byte[] bytes = byteArrayOutputStream.toByteArray();
            i.putExtra("picture", bytes);
            i.putExtras(bundle);
            context.startActivity(i);
        }
    } }

这是我的下一个接收包活动代码:

if (getIntent().getExtras() != null) {
    bundle = getIntent().getExtras();
    itemTitle.setText(bundle.getString("item_title"));
    itemDescription.setText(bundle.getString("item_description"));
    //Get Image and conversion
    byte[] bytes = bundle.getByteArray("picture");
    Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    itemImage.setImageBitmap(bmp);
}

1 个答案:

答案 0 :(得分:1)

使用列表数据发送数据另一个活动。无需“ bitmapDrawable =(BitmapDrawable)itemList.get(listPosition).getImage();” 每次在变量中分配drawable。它始终保持最后一个可绘制位置,这就是为什么您总是在另一个活动中获取随机数据的原因。注意:请检查位置!= RecyclerView.NO_POSITION,因为已删除某项,但用户在UI删除它之前先单击它

    @Override
            public void onClick(View v) {

                int listPosition = getAdapterPosition(); // gets item position
              if (position != RecyclerView.NO_POSITION) {
                Intent i = new Intent(context, ItemDescriptionsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("item_title", itemList.get(listPosition).getTitle());
                bundle.putString("item_description", 
                itemList.get(listPosition).getDescription());

            bitmap =(BitmapDrawable)itemList.get(listPosition).getImage();
                ByteArrayOutputStream byteArrayOutputStream = new 
                ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, 
                byteArrayOutputStream);
                byte[] bytes = byteArrayOutputStream.toByteArray();
                i.putExtra("picture", bytes);
                i.putExtras(bundle);
                context.startActivity(i);
            }}