Android開發之LisitView的圖文並排效果實現(原始碼分享)

來源:互聯網
上載者:User

     此篇部落格通過類比串連MyEclipse搭建的伺服器下載圖片,實現在實際開發中的情境運用。

MainActivity的代碼

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; //建立一個進度條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("正在下載");dialog.setMessage("客官,請稍後.....");new MyTask().execute(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//通過非同步任務下載圖片list = CitySource.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);//如果資料有更新,該方法會自動更新listviewadapter.notifyDataSetChanged();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) {               //填充布局檔案,僅在第一次啟動的時候系統會調用該方法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類比遠程伺服器的列表資料

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", "杭州");map.put("view", "hangzhou");HashMap<String, Object> map1=new HashMap<String, Object>();map1.put("name", "杭州");map1.put("view", "hangzhou");list.add(map);list.add(map1);return list;}}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.