MainActivity. java is as follows:
Package cn. ideallistview; import java. util. arrayList; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. listView; // items in ListView display a network image // process: // 1 try to get from memory cache // 1.1 from first-level cache (strong reference) search for the image // 1.2 from the second-level cache (if referenced) search for images in // if 1 fails to be transferred in 2 // 2 tries to get from the SDCard File // If 2 fails to be transferred in 3 // 3 downloads the image from the network // 3.1 will be downloaded after save the image to the SDCard File // 3.2 returns the image obtained from the SDCard File to the ListView display // 3.3 saves the image obtained from the SDCard File to the memory cache // 3.3.1 if the remaining space in the level-1 cache is sufficient, save the image to the level-1 cache. // If the cache fails to be transferred, 3.3.2 // 3.3.2 put the oldest elements in the level-1 cache into the level-2 cache. // verify the remaining space in the first-level cache until there is sufficient space to store the image public class MainActivity extends Activity {ListView listView; ListViewAdapter adapter; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); initView ();} public void initView () {Button button = (Button) findViewById (R. id. button); button. setOnClickListener (new ButtonOnClickListener (); listView = (ListView) findViewById (R. id. listView); ArrayList <ListViewItem> arrayList = new ArrayList <ListViewItem> (); ListViewItem item1 = new ListViewItem ("xxxx", "http://images.csdn.net/20121018/zazhi-68-78-1018.jpg"); arrayList. add (item1); ListViewItem item2 = new ListViewItem ("xxxx", "http://info-database.csdn.net/Upload/2012-10-08/zazhi-210-90-1008.jpg"); arrayList. add (item2); ListViewItem item3 = new ListViewItem ("xxxx", "http://images.csdn.net/20121119/20111211223655841.jpg"); arrayList. add (item3); ListViewItem item4 = new ListViewItem ("xxxx", "http://images.csdn.net/20121119/20120619174604972.jpg"); arrayList. add (item4); ListViewItem item5 = new ListViewItem ("xxxx", "http://csdnimg.cn/www/images/pic_foot_report110.png"); arrayList. add (item5); ListViewItem item6 = new ListViewItem ("xxxx", "http://csdnimg.cn/www/images/pic_foot_report.png"); arrayList. add (item6); ListViewItem item7 = new ListViewItem ("xxxx", "http://csdnimg.cn/www/images/pic_foot_BNIA.png"); arrayList. add (item7); ListViewItem item8 = new ListViewItem ("xxxx", "http://csdnimg.cn/www/images/pic_foot_gongshang.png"); arrayList. add (item8); ListViewItem item9 = new ListViewItem ("xxxx", "http://images.csdn.net/20120803/logo-qixing02.jpg"); arrayList. add (item9); ListViewItem item10 = new ListViewItem ("xxxx", "http://images.csdn.net/20120726/quanjing-logo-shouye.jpg"); arrayList. add (item10); ListViewItem item11 = new ListViewItem ("xxxx", "http://images.csdn.net/20120726/nhn-logo-shouye.jpg"); arrayList. add (item11); ListViewItem item12 = new ListViewItem ("xxxx", "http://images.csdn.net/20120510/shanghai-jiaoda-logo.jpg"); arrayList. add (item12); ListViewItem item13 = new ListViewItem ("xxxx", "http://images.csdn.net/20120312/bigman2.gif"); arrayList. add (item13); ListViewItem item14 = new ListViewItem ("xxxx", "http://images.csdn.net/20120216/csdn2.gif"); arrayList. add (item14); ListViewItem item15 = new ListViewItem ("xxxx", "http://images.csdn.net/20121109/win8_100x74.jpg"); arrayList. add (item15); ListViewItem item16 = new ListViewItem ("xxxx", "http://images.csdn.net/20120816/cf-20120816.jpg"); arrayList. add (item16); ListViewItem item17 = new ListViewItem ("xxxx", "http://images.csdn.net/20120704/bi05.jpg"); arrayList. add (item17); ListViewItem item18 = new ListViewItem ("xxxx", "http://images.csdn.net/20120816/amd-20120816.jpg"); arrayList. add (item18); ListViewItem item19 = new ListViewItem ("xxxx", "http://y2.ifengimg.com/2012/12/08/09383369.jpg"); arrayList. add (item19); ListViewItem item20 = new ListViewItem ("xxxx", "http://y1.ifengimg.com/2012/12/08/09320040.jpg"); arrayList. add (item20); ListViewItem item21 = new ListViewItem ("xxxx", "http://y3.ifengimg.com/4ccd6020154f55cb/2012/1207/2.jpg"); arrayList. add (item21); ListViewItem item22 = new ListViewItem ("xxxx", "http://y2.ifengimg.com/2012/12/08/09425571.jpg"); arrayList. add (item22); ListViewItem item23 = new ListViewItem ("xxxx", "http://y2.ifengimg.com/b19fadab6fcc0645/2012/1011/123.jpg"); arrayList. add (item23); ListViewItem item24 = new ListViewItem ("xxxx", "http://y2.ifengimg.com/2012/11/15/07414772.jpg"); arrayList. add (item24); ListViewItem item25 = new ListViewItem ("xxxx", "http://y2.ifengimg.com/2012/11/15/07532374.jpg"); arrayList. add (item25); ListViewItem item26 = new ListViewItem ("xxxx", "http://y0.ifengimg.com/2012/11/15/07583017.jpg"); arrayList. add (item26); ListViewItem item27 = new ListViewItem ("xxxx", "http://y0.ifengimg.com/2012/02/20/14175732.jpg"); arrayList. add (item27); ListViewItem item28 = new ListViewItem ("xxxx", "http://y2.ifengimg.com/2012/10/29/21175274.jpg"); arrayList. add (item28); ListViewItem item29 = new ListViewItem ("xxxx", "http://res.news.ifeng.com/2727e09814adca26/2011/1221/rdn_4ef0fb0333564.jpg"); arrayList. add (item29); ListViewItem item30 = new ListViewItem ("xxxx", "http://y1.ifengimg.com/2012/12/04/10060551.jpg"); arrayList. add (item30); ListViewItem item31 = new ListViewItem ("xxxx", "http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec068537d8f6.jpg"); arrayList. add (item31); ListViewItem item32 = new ListViewItem ("xxxx", "http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec068554fa3f.jpg"); arrayList. add (item32); ListViewItem item33 = new ListViewItem ("xxxx", "http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec06853bd85b.jpg"); arrayList. add (item33); ListViewItem item34 = new ListViewItem ("xxxx", "http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec06854ace88.jpg"); arrayList. add (item34); ListViewItem item35 = new ListViewItem ("xxxx", "http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec06855bad01.jpg"); arrayList. add (item35); ListViewItem item36 = new ListViewItem ("xxxx", "http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec0685649c4e.jpg"); arrayList. add (item36); ListViewItem item37 = new ListViewItem ("xxxx", "http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec068562ac3d.jpg"); arrayList. add (item37); ListViewItem item38 = new ListViewItem ("xxxx", "http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec06854dc836.jpg"); arrayList. add (item38); ListViewItem item39 = new ListViewItem ("xxxx", "http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec06855ef0ea.jpg"); arrayList. add (item39); // ListViewItem item40 = new ListViewItem // ("xxxx", "http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec068568beda.jpg"); // arrayList. add (item40); ListViewItem item40 = new ListViewItem ("xxxx", "http://s12.sinaimg.cn/middle/4b181bd0gbf6cb07c2a1b&690"); arrayList. add (item40); adapter = new ListViewAdapter (arrayList, MainActivity. this); listView. setAdapter (adapter) ;}@ Override public void onDestroy () {listView. setAdapter (null); super. onDestroy ();} private class ButtonOnClickListener implements OnClickListener {@ Overridepublic void onClick (View v) {adapter. imageLoader. sort AchE (); // adapter. notifyDataSetChanged ();}}}
ListViewAdapter. java is as follows:
Package cn. ideallistview; import java. util. arrayList; import android. content. context; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. textView; import cn. loadImages. imageLoader; public class ListViewAdapter extends BaseAdapter {private static LayoutInflater inflater = null; pu Blic ImageLoader imageLoader; private ArrayList <ListViewItem> mArrayList; private Context mContext; public ListViewAdapter (ArrayList <ListViewItem> mArrayList, Context mContext) {super (); this. mArrayList = mArrayList; this. mContext = mContext; inflater = (LayoutInflater) mContext. getSystemService (Context. LAYOUT_INFLATER_SERVICE); imageLoader = new ImageLoader (mContext);} public int getCount () {if (mAr RayList = null) {return 0;} else {return mArrayList. size () ;}} public Object getItem (int position) {if (mArrayList = null) {return null;} else {return mArrayList. get (position) ;}} public long getItemId (int position) {return position;} public View getView (int position, View convertView, ViewGroup parent) {ViewHolder holder = null; if (convertView = null) {holder = new ViewHolder (); convertView = inflater. inflat E (R. layout. listviewitem, null, false); holder. textView = (TextView) convertView. findViewById (R. id. textView); holder. imageView = (ImageView) convertView. findViewById (R. id. imageView); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag ();} if (this. mArrayList! = Null) {ListViewItem listViewItem = this. mArrayList. get (position); if (holder. textView! = Null) {holder. textView. setText (listViewItem. getContent ();} if (holder. imageView! = Null) {try {String imageUrl = listViewItem. getImageURL (); // loads the imageLoader image on the network. displayImage (imageUrl, holder. imageView);} catch (Exception e) {e. printStackTrace () ;}}return convertView;} private class ViewHolder {ImageView imageView; TextView textView ;}}
ListViewItem. java is as follows:
package cn.ideallistview;public class ListViewItem {String content;String imageURL;public ListViewItem(String content, String imageURL) {super();this.content = content;this.imageURL = imageURL;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public String getImageURL() {return imageURL;}public void setImageURL(String imageURL) {this.imageURL = imageURL;}}
Main. xml is as follows:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"/> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Clear FileCache"/></LinearLayout>
Listviewitem. xml is as follows:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/imageView" android:layout_width="50dip" android:layout_height="50dip" android:src="@drawable/stub" android:scaleType="centerCrop"/> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="left|center_vertical" android:textSize="20dip" android:layout_marginLeft="10dip"/></LinearLayout>