Android ListView加载网络数据和图片

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012440207/article/details/36674009

上一篇写到了ListView的分页功能,这篇接着介绍ListView。

一般情况下的APP中的ListView的显示包括:图片+数据,所以这篇文章介绍Listview加载网络数据和图片,数据采用的是JSON格式。

实现效果图:

布局不是很完美,但功能实现了,大家可在研究明白后,进行相应的优化。


源代码:


布局文件:

activity_main.xml(一个ListView):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</RelativeLayout>

item.xml(ListView的每个item布局):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/picname_hospital_s"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />

    <TextView
        android:id="@+id/hdf_yy_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/picname_hospital_s" />

    <TextView
        android:id="@+id/hdf_yy_tese"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@id/hdf_yy_name" />

    <TextView
        android:id="@+id/hdf_yy_dizhi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@id/picname_hospital_s" />

    <TextView
        android:id="@+id/id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@id/hdf_yy_dizhi" />

</RelativeLayout>

MainActivity:

package com.listviewdemo_imageanddata;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private ListView listView;
	private MyAdapter adapter;
	private ProgressDialog dialog;
	private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	private static final String PATH = "http://phone.manle.com/yaodian.php?mod=yy_list_by_city&q=%E6%B5%8E%E5%8D%97&start=0";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		listView = (ListView) findViewById(R.id.listView);
		dialog = new ProgressDialog(this);
		dialog.setTitle("提示!");
		dialog.setMessage("正在加载中...");
		/**
		 * 开始获取数据
		 */
		new MyTask().execute(PATH);
		
		/**
		 * 每个item的点击事件
		 */
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(MainActivity.this, "您点击了:" + position,
						Toast.LENGTH_SHORT).show();
			}
		});
	}

	/**
	 * MyTask继承线程池AsyncTask用来网络数据请求、json解析、数据更新等操作。
	 */
	class MyTask extends AsyncTask<String, Void, String> {
		/**
		 * 数据请求前显示dialog。
		 */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog.show();
		}

		/**
		 * 在doInBackground方法中,做一些诸如网络请求等耗时操作。
		 */
		@Override
		protected String doInBackground(String... params) {
			return RequestData();
		}

		/**
		 * 在该方法中,主要进行一些数据的处理,更新。
		 */
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null) {
				// 如果获取的result数据不为空,那么对其进行JSON解析。并显示在手机屏幕上。
				list = JSONAnalysis(result);
				adapter = new MyAdapter(MainActivity.this, list);
				listView.setAdapter(adapter);
				adapter.notifyDataSetChanged();
				dialog.dismiss();
			} else if (result == null) {
				Toast.makeText(MainActivity.this, "请求数据失败...",
						Toast.LENGTH_LONG).show();
			}
			dialog.dismiss();
		}
	}
	
	/**
	 * 网络数据请求
	 * @return
	 */
	public String RequestData() {
		HttpGet get = new HttpGet(PATH);
		HttpClient client = new DefaultHttpClient();
		StringBuilder builder = null;
		try {
			HttpResponse response = client.execute(get);
			if (response.getStatusLine().getStatusCode() == 200) {
				InputStream inputStream = response.getEntity().getContent();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(inputStream));
				builder = new StringBuilder();
				String s = null;
				for (s = reader.readLine(); s != null; s = reader.readLine()) {
					builder.append(s);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return builder.toString();
	}
	
	/**
	 * JSON解析
	 * @param result
	 * @return
	 */
	public List<Map<String, Object>> JSONAnalysis(String result) {
		JSONArray array = null;
		try {
			array = new JSONArray(result);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		for (int i = 0; i < array.length(); i++) {
			JSONObject objectOne = array.optJSONObject(i);
			String id = objectOne.optString("id");
			String hdf_yy_name = objectOne.optString("hdf_yy_name");
			String hdf_yy_tese = objectOne.optString("hdf_yy_tese");
			String hdf_yy_dizhi = objectOne.optString("hdf_yy_dizhi");
			String picname_hospital_s = objectOne
					.optString("picname_hospital_s");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", id);
			map.put("hdf_yy_name", hdf_yy_name);
			map.put("hdf_yy_tese", hdf_yy_tese);
			map.put("hdf_yy_dizhi", hdf_yy_dizhi);
			map.put("picname_hospital_s", picname_hospital_s);
			list.add(map);
		}
		return list;
	}

}

ListView的适配器:MyAdapter:

package com.listviewdemo_imageanddata;

import java.util.List;
import java.util.Map;

import com.listviewdemo_imageanddata.DownImage.ImageCallBack;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * ListView适配器
 *
 */
public class MyAdapter extends BaseAdapter {
	private Context context;
	private List<Map<String, Object>> list;
	public MyAdapter(Context context, List<Map<String, Object>> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		final ViewHolder holder;
		if (convertView == null) {
			convertView  = LayoutInflater.from(context).inflate(R.layout.item, null);
			holder = new ViewHolder();
			
			holder.picname_hospital_s = (ImageView) convertView.findViewById(R.id.picname_hospital_s);
			holder.id = (TextView) convertView.findViewById(R.id.id);
			holder.hdf_yy_name = (TextView) convertView.findViewById(R.id.hdf_yy_name);
			holder.hdf_yy_dizhi = (TextView) convertView.findViewById(R.id.hdf_yy_dizhi);
			holder.hdf_yy_tese = (TextView) convertView.findViewById(R.id.hdf_yy_tese);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.picname_hospital_s.setImageResource(R.drawable.ic_launcher);
		holder.id.setText(list.get(position).get("id").toString());
		holder.hdf_yy_name.setText(list.get(position).get("hdf_yy_name").toString());
		holder.hdf_yy_dizhi.setText(list.get(position).get("hdf_yy_dizhi").toString());
		holder.hdf_yy_tese.setText(list.get(position).get("hdf_yy_tese").toString());
		//接口回调的方法,完成图片的读取;
		DownImage downImage = new DownImage(list.get(position).get("picname_hospital_s").toString());
		downImage.loadImage(new ImageCallBack() {
			
			@Override
			public void getDrawable(Drawable drawable) {
				holder.picname_hospital_s.setImageDrawable(drawable);
			}
		});
		
		return convertView;
	}
	class ViewHolder{
		TextView id,hdf_yy_name,hdf_yy_tese,hdf_yy_dizhi;
		ImageView picname_hospital_s;
	}
}

DownImage(获取网络图片):

package com.listviewdemo_imageanddata;

import java.net.URL;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
/**
 * 用来获取网络图片
 * @author Administrator
 *
 */
public class DownImage {
	public String image_path;

	public DownImage(String image_path) {
		this.image_path = image_path;
	}

	public void loadImage(final ImageCallBack callBack) {

		final Handler handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				Drawable drawable = (Drawable) msg.obj;
				callBack.getDrawable(drawable);
			}

		};

		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Drawable drawable = Drawable.createFromStream(new URL(
							image_path).openStream(), "");

					Message message = Message.obtain();
					message.obj = drawable;
					handler.sendMessage(message);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

	public interface ImageCallBack {
		public void getDrawable(Drawable drawable);
	}
}


下面给出源码:

点击下载源码



展开阅读全文

没有更多推荐了,返回首页