简体   繁体   English

如何从ArrayList中删除位图?

[英]How to remove Bitmap from ArrayList?

I have a class called PlayerList, in in that class i have an ArrayList defined like this : 我有一个叫做PlayerList的类,在该类中我有一个ArrayList定义如下:

private ArrayList<Bitmap> images = new ArrayList<Bitmap>();

Now, an ArrayList remove method can exept 2 arguments, an index and an Object. 现在,一个ArrayList remove方法可以包含2个参数,一个索引和一个Object。

I'm trying to remove a Bitmap from the array by sending the Bitmap to it. 我正在尝试通过将位图发送给它来从数组中删除它。 The thing is the bit map is made of the same image but the instace of the Bitmap i saved in the array is not the same i'm trying to find, when i print the ArrayList to console i see new names all the time, stuff like : 问题是位图是由相同的图像组成的,但是我保存在数组中的位图的安装实例与我尝试查找的位图不同,当我将ArrayList打印到控制台时,我一直在看到新名称,喜欢 :

[android.graphics.Bitmap@44ea2d48, android.graphics.Bitmap@44ea2e20]

Could this be related? 这可能有关吗? and while wer're at it, The class implements Parcelable so the data being read/writen back and forth, that can't be too healthy too, right? 同时它wer're,这个类实现Parcelable所以正在读/ writen来回的数据,即不能太健康过,对吧?

Am i missing a generic problem or it's code specific and i should publish my code? 我是否缺少通用问题或特定于代码,我应该发布代码?

My problem here basiclly is when i try to remove the Bitmap, it allways removes the wrong item in the arraylist, always 1 before the 1 i need 我的问题基本上是当我尝试删除位图时,它总是删除arraylist中的错误项,总是在我需要的1之前删除1

Unless you have a handle to the BitMap's hash code or a like key based on which it is stored, you can't remove a desired one from the list. 除非您有BitMap's哈希码的句柄或存储它的类似键,否则您无法从列表中删除所需的键。 If you just want to depend on BitMap stored, you need to traverse through the list and parse elements to find desired bitmap. 如果只想依赖于存储的BitMap ,则需要遍历列表并解析元素以找到所需的位图。 Again you need a hascode to identify if the BitMap you are parsing is what you are looking for. 再次,您需要一个hascode来识别您正在解析的BitMap是否在寻找。 Meaning you need to maintain a list of hashcode's for each of the BitMap stored. 这意味着您需要为每个存储的BitMap维护一个hashcode's列表。 Alternatively use HashMap over List that allows to store a key against a BitMap being stored. 或者,在List上使用HashMap ,它允许针对要存储的BitMap存储key And you can process your requirement based on the key and use it whenever required to remove a BitMap from the HashMap table. 而且,您可以根据key处理您的需求,并在需要时从HashMap表中删除BitMap时使用它。

HashMap<String, BitMap> myBitMaps = ... // construct as you desire.
...
if ( condition satisfied on key ) {
  myBitMaps.remove( keyObject );
  // process further
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM