Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 通过捆绑包发送的位图图像在下一个活动中是随机的_Android_Bitmap_Bundle - Fatal编程技术网

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

Android 通过捆绑包发送的位图图像在下一个活动中是随机的,android,bitmap,bundle,Android,Bitmap,Bundle,我有一个第一个活动,带有一个回收视图,其中包含3个元素,这些元素在下一个活动中通过捆绑包发送。这两个文本元素很容易获取 但是,当我通过转换位图放置图像时,它会显示上一个活动项目列表中的随机图像 这是我的recyclerviewadapter第一个活动的代码: public class FurnitureAdapter extends RecyclerView.Adapter<FurnitureAdapter.ViewHolder> { //All methods in th

我有一个第一个
活动
,带有一个
回收视图
,其中包含3个元素,这些元素在下一个
活动
中通过捆绑包发送。这两个文本元素很容易获取

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

这是我的
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);
        }
    } }

使用列表数据向其他活动发送数据。无需“bitmapDrawable=(bitmapDrawable)itemList.get(listPosition).getImage();” 每次在变量中指定drawable。它始终保持可绘制的最后一个位置,这就是为什么在另一个活动中总是获得随机数据的原因。注意:检查位置!=RecyclerView.NO_位置,因为某个项目已被删除,但用户在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);
            }}

为什么要处理位图并使数据对象复杂化?为什么不使用或使用任何有助于图像资源的图像库?如果你正在存储图像或将其作为资源与你的应用捆绑在一起,那么在下一个活动中通过传递一些图像名称或id等再次获取图像?我同意Naseem的观点,一个大位图可能会使你的应用崩溃,因为捆绑有内存限制。从逻辑上讲,你的代码是有效的。但是,我在“bitmap=(BitmapDrawable)itemList.get(listPosition).getImage();”行中得到一个错误,该行表示不兼容的类型需要位图,但发现BitmapDrawable。我试着压缩一个位图,得到一个bitmapDrawableNevermind,完成了,我刚刚添加了另一行。。(在查询代码中编辑)。
    @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);
            }}