結果截圖:
包含三個類文件:CommonUri.java 、DownIoadImage.java 、 MainActivity.java
兩個layout文件:activity_main.xml item.xml
activity_main.xml文件:
<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/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
item.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imageView1"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:text="TextView" />
</RelativeLayout>
CommonUri類用於存儲Uri
</pre><pre name="code" class="java">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);
}
}
//圖文混排,先保證文字出現
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<Map<String, Object> > list;
public MyAdapter(Context context)
{
this.context = context;
layoutInflater = layoutInflater.from(context);
}
public void setData(List<Map<String, Object> > 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<String, Void, List<Map<String, Object>>>
{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.show();
}
@Override
protected void onPostExecute(List<Map<String, Object>> 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<Map<String, Object>> doInBackground(String... params) {
// TODO Auto-generated method stub
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
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;
}
}
}