Android开发之LisitView的图文并排效果实现(源代码分享)

     此篇博客通过模拟连接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/";

	@Override
	protected 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;

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			dialog.show();
			super.onPreExecute();
		}

		@Override
		protected 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;
		}

		@Override
		protected void onPostExecute(List<HashMap<String, Object>> result) {
			// TODO Auto-generated method stub
			adapter.setList(result);
			//如果数据有更新,该方法会自动更新listview
			adapter.notifyDataSetChanged();
			listView.setAdapter(adapter);
			dialog.dismiss();
			super.onPostExecute(result);
		}

	}

	@Override
	public 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;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup viewGroup) {
			// TODO Auto-generated method stub
			View 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 block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.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;
	}

}


Android开发之LisitView的图文并排效果实现(源代码分享),布布扣,bubuko.com

Android开发之LisitView的图文并排效果实现(源代码分享)

上一篇:iphone比安卓的手机流畅的四个主要原因


下一篇:Appium自动化测试之键盘操作pressKeyCode()方法(Android特有)