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

问题描述 投票:-1回答:1

我有一个Activityrecyclerview包含3个元素,在下一个Activity通过捆绑发送。 2个文本元素很容易获得。

但是,当我通过转换位图放置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);
}
android bitmap bundle
1个回答
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);
            }}
© www.soinside.com 2019 - 2024. All rights reserved.