我正在尝试优化我的应用程序中的照片库,但遇到了 Universal Image Loader。但是,在我的应用程序中实现它的所有尝试都失败了。这是我当前应用程序中的 2 个类:活动类:
public class Galerija extends Activity {
ArrayList<RSSItem> lista = new ArrayList<RSSItem>();
ArrayList<String> lst_slika = new ArrayList<String>();
RSSItem tempItem = new RSSItem();
ImageAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_galerija);
try
{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader myReader = sp.getXMLReader();
URL url = new URL("http://erdut.gausstesting.info/generateXML/galerija.php");
XMLHandler myXMLHandler = new XMLHandler();
myReader.setContentHandler(myXMLHandler);
myReader.parse(new InputSource(url.openStream()));
lista = myXMLHandler.getRss_lista();
lst_slika = lista.get(0).getImages();
} catch (Exception e) {
System.out.println(e);
}
adapter = new ImageAdapter(this, lst_slika);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
} }
这是适配器类:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> lista;
public ImageAdapter(Context c, ArrayList<String> lista) {
mContext = c;
this.lista = lista;
}
public int getCount() {
return lista.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
new ImageDownloadTask(imageView).execute(lista.get(position));
return imageView;
}}
我想通过使用通用图像加载器(或任何其他缓存我的图像的方法)来修改这些类,因为目前我的照片库活动由于内存不足错误而崩溃。