listview加载网络图片
public class HttpUtil {
public static final String BASE_URL = "http://10.0.2.2:8080/jsontest/servlet/ProductServlet";
public static final String IMG_URL = "http://10.0.2.2:8080/jsontest/upload/";
public static HttpClient httpClient = new DefaultHttpClient();
// post方法访问服务器,返回json字符串
public static String getRequest(String url){
String result = null;
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(httpResponse.getEntity(),"utf-8");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
// 字符串转成集合数据
public static void resultString2List(List<Map<String ,Object>> list, String str,String title) {
try {
JSONObject jsonObject = new JSONObject(str);
JSONArray jsonArray = jsonObject.getJSONArray(title);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
Map<String ,Object> map = new HashMap<String, Object>();
Iterator<String> iterator = jsonObject2.keys();
while (iterator.hasNext()) {
String key = iterator.next();
Object value = jsonObject2.get(key);
map.put(key, value);
}
list.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// post方法访问服务器,返回集合数据
public static List<Map<String,Object>> getRequest2List(String url,String title){
List<Map<String,Object>> list = new ArrayList<Map<String ,Object>>();
resultString2List(list, url, title);
return list;
}
// get方法访问服务器,返回json字符串
public static String postRequest(String url, Map<String,String> rawParams) throws Exception{
HttpPost post = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (String key:rawParams.keySet()) {
params.add(new BasicNameValuePair(key, rawParams.get(key)));
}
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse httpResponse = httpClient.execute(post);
if(httpResponse.getStatusLine().getStatusCode() == 200){
String result = EntityUtils.toString(httpResponse.getEntity());
return result;
}
return null;
}
//post访问的方法,还可继续封装,略。。。
}
public class MyTask extends AsyncTask<String, Void, List<Map<String,Object>>>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog.show();
}
@Override
protected void onPostExecute(List<Map<String, Object>> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
adapter.setData(result);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
progressDialog.dismiss();
}
@Override
protected List<Map<String, Object>> doInBackground(String... params) {
// TODO Auto-generated method stub
List<Map<String,Object>> list ;
String str = HttpUtil.getRequest(params[0]);
list = HttpUtil.getRequest2List(str, "products");
return list;
}
}
progressDialog = new ProgressDialog(this);
progressDialog.setTitle("正在下载....");
adapter = new MyAdapter(this);
new MyTask().execute(HttpUtil.BASE_URL);
public class MyAdapter extends BaseAdapter{
private Context context;
private LayoutInflater layoutInflater;
private List<Map<String,Object>> list;
public MyAdapter(Context context) {
this.context = context;
layoutInflater = layoutInflater.from(context);
}
public List getData(){
return list;
}
public void setData(List<Map<String,Object>> data){
this.list = data;
}
@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;
final ViewHolder viewHolder;
if (convertView == null ) {
convertView = layoutInflater.inflate(R.layout.item, null);
viewHolder = new ViewHolder();
viewHolder.id = (TextView)convertView.findViewById(R.id.textView1);
viewHolder.name = (TextView)convertView.findViewById(R.id.textView2);
viewHolder.address = (TextView)convertView.findViewById(R.id.textView3);
viewHolder.img = (ImageView)convertView.findViewById(R.id.imageView1);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.id.setText(list.get(position).get("sid").toString());
viewHolder.name.setText(list.get(position).get("name").toString());
viewHolder.address.setText(list.get(position).get("addr").toString());
//接口回调的方法,完成图片的读取;
DownImage downImage = newDownImage(HttpUtil.IMG_URL+list.get(position).get("pic").toString()+".jpg");
downImage.loadImage(new ImageCallBack() {
@Override
public void getDrawable(Drawable drawable) {
// TODO Auto-generated method stub
viewHolder.img.setImageDrawable(drawable);
}
});
return convertView;
}
}
public class ViewHolder{
ImageView img;
TextView id;
TextView name;
TextView address;
}
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) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Drawable drawable = (Drawable) msg.obj;
callBack.getDrawable(drawable);
}
};
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);
}
}
listview加载网络图片的更多相关文章
- 详解ListView加载网络图片的优化,让你轻松掌握!
详解ListView加载网络图片的优化,让你轻松掌握! 写博客辛苦了,转载的朋友请标明出处哦,finddreams(http://blog.csdn.net/finddreams/article/de ...
- 详解ListView加载网络图片的优化
我们来了解一些ListView在加载大量网络图片的时候存在的常见问题: 1.性能问题,ListView的滑动有卡顿,不流畅,造成非常糟糕的用户体验. 2.图片的错位问题. 3.图片太大,加载Bitma ...
- [android]完美的解决方案ListView加载网络图片反弹问题
为什么 先说为什么有照片反弹. 使用convertView对ListView的每一个item优化,item的复用能够有效减少内存的占用.使ListView滑动更为流畅. 但会带来一个问题,当最顶部的i ...
- (BUG已修改,最优化)安卓ListView异步加载网络图片与缓存软引用图片,线程池,只加载当前屏之说明
原文:http://blog.csdn.net/java_jh/article/details/20068915 迟点出更新的.这个还有BUG.因为软引应不给力了.2.3之后 前几天的原文有一个线程管 ...
- wemall app商城源码Android之ListView异步加载网络图片(优化缓存机制)
wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享wemall app商城源码Android之L ...
- Android中用双缓存技术,加载网络图片
最近在学校参加一个比赛,写的一个Android应用,里面要加载大量的网络图片,可是用传统的方法图片一多就会造成程序出现内存溢出而崩溃.因为自己也在学习中,所以看了很多博客和视频,然后参照这些大神的写源 ...
- Gilde加载网络图片(一)
前两天 一个朋友要在本地加载几M的大图 用于用户滚动查看.按照思路 是压缩后加载显示但是这样会不清晰, 其实gilde用来加载图片 很牛掰 于是了解一下 下面贴上两个工具类: package com. ...
- Afianl加载网络图片(续)
上一篇已经讲了如何利用Afianl加载网络图片和下载文件,这篇文章将继续讲解使用Afinal加载网络图片的使用,主要结合listview的使用: 看效果图: listview在滑动过程中没用明显卡顿, ...
- 【WPF】wpf image控件加载网络图片不显示问题,
1.加载网络图片到内存system.drawing.image对象中2.内存中的image 转Bitmap 再转适合system.windows.controls.image 的BitmapImage ...
随机推荐
- winform 窗口点击窗体任意位置移动窗体
private bool _isDown; private Point _mousePoint; private void Users_MouseDown(object sender, MouseEv ...
- log4net使用(包括单个文件和按日期生成多个文件)
1.log4net生成单个文件 直接将这段代码考到config中即可用 <log4net> <!--定义输出到文件中--> <appender name="Lo ...
- 如何清空android ListView控件的内容
第一种方法: listView.setAdapter(null); 第二种方法: listAdapter.clear(); listAdapter.notifyDataSetChanged() ; 满 ...
- samba(转)
配置虚拟机和主机之间的文件共享之所以配置虚拟机和主机之间的文件共享,是因为我们用惯了WINDOWS操作系统下的很多工具,比如编辑工具UltraEdit,souce insight等.我们可以在wind ...
- python实现词法分析
#请先安装Ply # -*- coding: utf-8 -*- #------------------------------------------------------------------ ...
- C语言随笔_类型声明
有位同学说,“老师,我运行如下代码,结果报错了” #include <iostream.h> int main(){ char c, int b; return 0; } 报错结果是 ...
- 【转】C++ STL 相关的问题集合
3.C++ STL中vector的相关问题: (1).调用push_back时,其内部的内存分配是如何进行的? (2).调用clear时,内部是如何具体实现的?若想将其内存释放,该如何操作 ...
- POJ1988 并查集的使用
Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 21157 Accepted: 7395 Ca ...
- grep, egrep, fgrep笔记
grep, egrep, fgrep grep: 根据模式搜索文本,并将符合模式的文本行显示出来.Pattern: 文本字符和正则表达式的元字符组合而成匹配条件 grep [options] PATT ...
- HBase 4、Phoenix安装和Squirrel安装
描述 现有hbase的查询工具有很多如:Hive,Tez,Impala,Shark/Spark,Phoenix等.今天主要记录Phoenix. phoenix,中文译为“凤凰”,很美的名字.Phoen ...