リンク。
今、私は私のプロジェクトでコンテキストリークがあり、リンク上でそれを引き起こす可能性のあるすべてをほぼ説明しています。
正直なところ、コンテキスト変数を含むものを可能な限り削除しようとしましたが、グリッドビューとベースアダプターに問題があり、これに頭を悩ませている助けが本当に必要です。ガベージコレクションが行われ、他のクラスの忍者のように見えることがあります。
私の質問:「あなたたちは私が変えるべきだとあなたたちは何を提案しますか?」と「何に気をつければいいの?」
これが私がしたことです:1。描画可能な画像のハッシュマップを作成しました2.グリッドビューのベースアダプターを作成しました3.loadCoverクラスのコード
private static Map ImageLocator = Collections.synchronizedMap(new WeakHashMap());
private class BaseA extends BaseAdapter{
private LayoutInflater inflater;
public BaseA(Context context){
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
viewHolder vh = new viewHolder();
row = inflater.inflate(R.layout.book_row_view, null);
vh.authors = (TextView)row.findViewById(R.id.book_Author);
vh.image = (ImageView)row.findViewById(R.id.icon);
vh.date = (TextView)row.findViewById(R.id.Date);
vh.Titles = (TextView)row.findViewById(R.id.Book_Title);
vh.fileName = (TextView)row.findViewById(R.id.FileLocation);
try{
String temp = File_Name.get(position);
vh.fileName.setText(temp);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Book_Information bi;
bi = new Book_Information();
bi = dbh.getData(File_Name.get(position));
//Gets the right book information for all the items
new LoadCover(vh.image, bi).run();
if(bi.getBook_Author() != null || bi.getBook_Date() !=null || bi.getBook_Description() != null ||
bi.getBook_Title() != null){
vh.authors.setText(bi.getBook_Author());
vh.date.setText(bi.getBook_Date());
vh.Titles.setText(bi.getBook_Title());
}
return row;
}
public int getCount() {
// TODO Auto-generated method stub
return File_Name.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
private class LoadCover implements Runnable{
ImageView image; Book_Information bi;
public LoadCover(ImageView image, Book_Information bi){
this.image = image;
this.bi = bi;
}
public void run() {
// TODO Auto-generated method stub
Drawable draw = ImageLocator.get(bi.getBook_File_Name());
if(draw!=null){
image.setImageDrawable(draw);
}else{
new UpdateImages(image, bi).run();
}
draw = null;
}
}
private class UpdateImages implements Runnable{
ImageView image;
Book_Information book_info;
public UpdateImages(ImageView imageView, Book_Information bookInfo){
this.image = imageView;
this.book_info = bookInfo;
}
public void run(){
try{
Bitmap bm = getBitmap(book_info);
FastBitmapDrawable fbd = new FastBitmapDrawable(bm);
image.setImageDrawable(fbd);
ImageLocator.put(book_info.getBook_File_Name(), fbd);
bm = null;
}catch (OutOfMemoryError e) {
// TODO: handle exception
ImageLocator.clear();
}
}
}