关于Android开发完全讲义将网络图像装载到ListView控件的修改

今天看9.1节的可装载网络数据的控件这一节的内容,发现随着Android版本的更新,这部分的代码由于Android已经不允许在主程序中添加任何关于网络请求的代码而失效了(弹出android.os.NetworkOnMainThreadException异常),因此,我将代码修改成了以下:

注意:本文中程序因为要连接网络,所以一定要在AndroidManifest.xml中加入Internet权限:

<uses-permission android:name="android.permission.INTERNET" />

主程序中的变量:

private ApkListAdapter apkListAdapter;
	public static Handler mHandler;
	private final int APK_LIST_FINISHED = 1;
	private ListView listView;
	private String rootUrl = "http://10.108.77.85:8080/myhttp/";
	private Context context;
	private String listUrl = rootUrl + "list.txt";

主函数的onCreate()方法:

protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView = (ListView) findViewById(R.id.activity_main_listView);
		mHandler = new Handler() {//使用Handle对加载完成的数据进行UI的更新
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == APK_LIST_FINISHED) {
					Log.i("Connection", "handleMessage");
					listView.setAdapter(apkListAdapter);
				}
			}
		};
		Thread thread = new Thread(new Runnable() {//新建一个线程,将网上的数据下载下来,并且通过handle的sendMessage()方法发送到mHandle中

			@Override
			public void run() {
				// TODO Auto-generated method stub
				List<ImageData> list = getImageData(listUrl);
				Iterator<ImageData> iterator = list.iterator();
				while (iterator.hasNext()) {
					ImageData tempImageData;
					tempImageData = iterator.next();
					InputStream is = getNetInputStream(rootUrl
							+ tempImageData.url);
					tempImageData.bitmap = BitmapFactory.decodeStream(is);
					try {
						is.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Log.i("Connection", tempImageData.toString());

				}
				apkListAdapter = new ApkListAdapter(list, MainActivity.this);
				Message msg = new Message();
				msg.what = APK_LIST_FINISHED;
				msg.obj = apkListAdapter;
				mHandler.sendMessage(msg);
			}
		});
		thread.start();//开启下载线程
	}
ImageData类:
class ImageData {//跟书上内容对比,我加入了Bitmap,这样可以直接将Bitmap封装进去
		public String url;
		public String applicationName;
		public float rating;
		public Bitmap bitmap;

		@Override
		public String toString() {
			return "ImageData [url=" + url + ", applicationName="
					+ applicationName + ", rating=" + rating + "]";
		}

	}
</pre><pre code_snippet_id="364803" snippet_file_name="blog_20140527_7_3074688" name="code" class="html">

跟书上基本没有变化的类和方法:

getImageData方法

public List<ImageData> getImageData(String urlStr) {
		List<ImageData> imageDataList = new ArrayList<>();
		ImageData imageData;
		InputStream is = getNetInputStream(urlStr);
		InputStreamReader isr;
		try {
			isr = new InputStreamReader(is, "GBK");
			BufferedReader br = new BufferedReader(isr);
			String s = null;
			while ((s = br.readLine()) != null) {
				String[] data = s.split(",");
				if (data.length > 2) {
					imageData = new ImageData();
					imageData.url = data[0];
					imageData.applicationName = data[1];
					imageData.rating = Float.parseFloat(data[2]);
					imageData.toString();
					imageDataList.add(imageData);
				}
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return imageDataList;
	}
</pre>getNetInputStream方法:<pre code_snippet_id="364803" snippet_file_name="blog_20140527_7_9647992" name="code" class="java">private InputStream getNetInputStream(String urlStr) {
		InputStream is = null;
		try {
			URL url = new URL(urlStr);
			URLConnection conn = url.openConnection();
			Log.i("Connection", conn.toString());
			conn.connect();
			is = conn.getInputStream();
			return is;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return is;
	}

继承BaseAdapter的ApkListAdapter类(稍微有点修改,已用红色标注):

class ApkListAdapter extends BaseAdapter {
		private List<ImageData> imageDataList;
		<span style="color:#ff6666;">private LayoutInflater mInflater;</span>

		Context context;

		public ApkListAdapter(List<ImageData> list, Context context) {
			// TODO Auto-generated constructor stub
			this.imageDataList = list;
			<span style="color:#ff6666;">this.mInflater = LayoutInflater.from(context);</span>
		}

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

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return 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
			<span style="color:#ff6666;">View view = mInflater.inflate(R.layout.apklistadapter, null);</span>
			ImageView imageView = (ImageView) view
					.findViewById(R.id.listadapter_imageView);
			TextView textViewApplicationName = (TextView) view
					.findViewById(R.id.listadapter_textView);
			TextView textViewRatingBar = (TextView) view
					.findViewById(R.id.listadapter_ratingTextView);
			RatingBar ratingBar = (RatingBar) view
					.findViewById(R.id.listadapter_ratingBar);
			textViewApplicationName
					.setText(imageDataList.get(position).applicationName);
			textViewRatingBar.setText(String.valueOf(imageDataList
					.get(position).rating));
			ratingBar.setRating(imageDataList.get(position).rating);
			imageView.setImageBitmap(imageDataList.get(position).bitmap);
			return view;
		}

	}
最后是实现的效果图(偷懒没有修改RatingBar的大小,不过默认样式的RatingBar真的是太反人类了。。。):

转载请注明来源:http://blog.csdn.net/simpsonst

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值