This blog simulates the connection to a server built by MyEclipse to download images, so that it can be used in actual development scenarios.
MainActivity code
Package com. example. e13_listview3; import java. util. hashMap; import java. util. list; import android. OS. asyncTask; import android. OS. bundle; import android. app. activity; import android. app. progressDialog; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. util. log; import android. view. layoutInflater; import android. view. menu; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. listView; import android. widget. textView; public class MainActivity extends Activity {private ListView listView; private MyAdapter adapter; private ProgressDialog dialog; // create a progress bar private byte [] data; private final static String PATH = "http: // 111.0.166.84: 8080/http/"; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listView = (ListView) this. findViewById (R. id. listView1); adapter = new MyAdapter (); dialog = new ProgressDialog (MainActivity. this); dialog. setTitle ("downloading"); dialog. setMessage ("guest, please wait ..... "); new mytask(cmd.exe cute (PATH);} class MyTask extends AsyncTask <String, Void, List <HashMap <String, Object >>{ private List <HashMap <String, object> list; @ Overrideprotected void onPreExecute () {// TODO Auto-generated method stubdialog. show (); super. onPreExecute () ;}@ Overrideprotected List <HashMap <String, Object> doInBackground (String... params) {// TODO Auto-generated method stub // download the image list = CitySource through an asynchronous task. getList (); String name = (String) list. get (0 ). get ("view"); data = HttpUtils. sendPostMethod (params [0] + name + ". jpg "); Log. I ("info", "--------->" + PATH + name + ". jpg "); return list ;}@ Overrideprotected void onPostExecute (List <HashMap <String, Object> result) {// TODO Auto-generated method stubadapter. setList (result); // if data is updated, this method automatically updates listviewadapter. yydatasetchanged (); listView. setAdapter (adapter); dialog. dismiss (); super. onPostExecute (result) ;}@overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;} public class MyAdapter extends BaseAdapter {private List <HashMap <String, Object> list; public List <HashMap <String, Object> getList () {return list;} public void setList (List <HashMap <String, Object> list) {this. list = list ;}@ Overridepublic int getCount () {// TODO Auto-generated method stubreturn list. size () ;}@ Overridepublic Object getItem (int arg0) {// TODO Auto-generated method stubreturn list. get (arg0) ;}@ Overridepublic long getItemId (int arg0) {// TODO Auto-generated method stubreturn arg0 ;}@ Overridepublic View getView (int position, View convertView, ViewGroup viewGroup) {// TODO Auto-generated method stubView view = null; if (convertView = null) {// fill the layout file, this method is called only when the system starts for the first time. view = LayoutInflater. from (MainActivity. this ). inflate (R. layout. item_list, null);} else {view = convertView;} Log. I ("info", "-------->" + view); TextView text = (TextView) view. findViewById (R. id. textView1); ImageView imageView = (ImageView) view. findViewById (R. id. imageView1); text. setText (CharSequence) list. get (position ). get ("name"); Bitmap bm = BitmapFactory. decodeByteArray (data, 0, data. length); imageView. setImageBitmap (bm); return view ;}}}
HttpUtiles
package com.example.e13_listview3;import java.io.IOException;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;public class HttpUtils { public static byte[] sendPostMethod(String url){byte[] result=null;HttpPost httpPost=new HttpPost(url);HttpClient client=new DefaultHttpClient();try {HttpResponse httpResponse=client.execute(httpPost);result=EntityUtils.toByteArray(httpResponse.getEntity());} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} return result; }}
CitySource simulates the list data of remote servers
Package com. example. e13_listview3; import java. util. arrayList; import java. util. hashMap; import java. util. list; public class CitySource {public static List <HashMap <String, Object> getList () {List <HashMap <String, Object> list = new ArrayList <HashMap <String, object >>> (); HashMap <String, Object> map = new HashMap <String, Object> (); map. put ("name", "Hangzhou"); map. put ("view", "hangzhou"); HashMap <String, Object> map1 = new HashMap <String, Object> (); map1.put ("name", "hangzhou "); map1.put ("view", "hangzhou"); list. add (map); list. add (map1); return list ;}}