Android之图文混排 (二)源代码

结果截图:

Android之图文混排 (二)源代码_第1张图片

包含三个类文件:CommonUri.java 、DownIoadImage.java 、 MainActivity.java

两个layout文件:activity_main.xml     item.xml

activity_main.xml文件:



    

    


item.xml文件:




    

    

    

    





CommonUri类用于存储Uri

public class CommonUri {

	public static String PRODUCT_URL = "";
	public static String PRODUCT_IMG = "";

}


DownloadImage类用于从服务器下载图片


public class DownloadImage {

	private String image_path;
	
	public DownloadImage(String image_path) {
		// TODO Auto-generated constructor stub
		this.image_path = image_path;
	}
	public void loadImage(final ImageCallBack callBack)
	{
		final Handler handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				
				callBack.getDrawable((Drawable)msg.obj);
			}
		};
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Drawable drawable = Drawable.createFromStream(new URL(image_path).openStream(), "");
					Message message = Message.obtain();
					message.obj = drawable;
					handler.sendMessage(message);
					
					
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}
	//接口的回调方式
	public interface ImageCallBack
	{
		public void getDrawable(Drawable drawable);  
	}

}



MainActivity类:


//图文混排,先保证文字出现
public class MainActivity extends Activity {

	private MyAdapter adapter = null;
	private ListView listView = null;
	private ProgressDialog dialog = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView = (ListView)findViewById(R.id.listView1);
		adapter  = new MyAdapter(this);
		
		dialog = new ProgressDialog(this);
		dialog.setTitle("提示下载信息");
		dialog.setMessage("正在下载,请稍后...");
		
		new MyTask().execute(CommonUri.PRODUCT_URL);
	}

	public class MyAdapter extends BaseAdapter
	{
		Context context = null;
		private LayoutInflater layoutInflater ;
		private List > list;
		
		public MyAdapter(Context context)
		{
			this.context = context;
			layoutInflater = layoutInflater.from(context);
		}

		public void setData(List > list)
		{
			this.list = list;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

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

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view = null;
			if(convertView == null)
			{
				view = layoutInflater.inflate(R.layout.item, null);
			}
			else
				view = convertView;
			TextView name = (TextView)findViewById(R.id.textView1);
			TextView address = (TextView)findViewById(R.id.textView2);
			TextView price = (TextView)findViewById(R.id.textView3);
			name.setText(list.get(position).get("proname").toString());//产品名
			address.setText(list.get(position).get("proaddress").toString());//产品地址
			price.setText(list.get(position).get("proprice").toString());//产品价格
			
			final ImageView imageView = (ImageView)findViewById(R.id.imageView1);
			DownloadImage downloadImage = new DownloadImage(CommonUri.PRODUCT_IMG + list.get(position).get("proimage").toString());
			downloadImage.loadImage(new DownloadImage.ImageCallBack() {
				
				@Override
				public void getDrawable(Drawable drawable) {
					// TODO Auto-generated method stub
					imageView.setImageDrawable(drawable);
				}
			});
			return view;
		//	imageView.setImageBitmap(bm);实际开发中,最好选用Bitmap
		}
		
	}
	public class MyTask extends AsyncTask>>
	{

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog.show();
		}
		@Override
		protected void onPostExecute(List> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			
			dialog.dismiss();
			adapter.setData(result);
			listView.setAdapter(adapter);
			adapter.notifyDataSetChanged();
		}
		@Override
		protected void onProgressUpdate(Void... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
		}
		@Override
		protected List> doInBackground(String... params) {
			// TODO Auto-generated method stub
			List> list = new ArrayList>();
			
			try {
				
				HttpClient client = new DefaultHttpClient();
				HttpPost  post = new HttpPost(params[0]);
				HttpResponse response = client.execute(post);
				if(response.getStatusLine().getStatusCode() == 200)
				{
					String jsonString = EntityUtils.toString(response.getEntity(), "utf-8");
					//接下来,对于jsonString进行解析
					//略......见图
					
					//
					
					//
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return list;
		}
		
	}

}



你可能感兴趣的:(Android)