android 图文列表的实现方法

方法一:AsyncTask  看代码

package com.blogchina.activity;


import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.blogchina.activity.R;
import com.blogchina.utils.HttpUtil;
import com.fedorvlasov.lazylist.LazyAdapter;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class HomelistActivity extends Activity{ 
	
	static final int MENU_MANUAL_REFRESH = 0;
	static final int MENU_DISABLE_SCROLL = 1;
	static final int MENU_SET_MODE = 2;
	static final int MENU_DEMO = 3;
	
	LazyAdapter lazyAdapter;
	HttpUtil httpUtil = new HttpUtil();
	private PullToRefreshListView pullToRefreshListView;
	private ArrayList<HashMap<String, Object>> data;
	final private String url = "http://*****/article/lists/is_only_pic/2/is_show_summary/1/limit/20";
	
	private ProgressDialog loadingDialog;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_homelist);
		loadingDialog = new ProgressDialog(this);
		loadingDialog.setTitle("");
		loadingDialog.setMessage("正在加载,请稍候");
		//listView = (ListView) findViewById(R.id.hl_listView);
		
		
		
		pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
		pullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				// TODO Auto-generated method stub
				String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
						DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

				// Update the LastUpdatedLabel
				refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
				new MyTask().execute(url);
				//loadingDialog.show();
				
			}
		});
		
		// Add an end-of-list listener
		pullToRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

			@Override
			public void onLastItemVisible() {
				Toast.makeText(HomelistActivity.this, "已经是最后一页了!", Toast.LENGTH_SHORT).show();
			}
		});
		
		loadingDialog.show();
		new MyTask().execute(url);
		//listView.setOnItemClickListener(new MyOnItemClickListener());
		pullToRefreshListView.setOnItemClickListener(new MyOnItemClickListener());
		
	}
	
	/**
	 * 内部类,click item 传参跳转
	 * @author entere
	 *
	 */
	private class MyOnItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			ListView actualListView = pullToRefreshListView.getRefreshableView();
			@SuppressWarnings("unchecked")
			HashMap<String, Object> map = (HashMap<String, Object>) actualListView.getItemAtPosition(arg2);
			Intent intent = new Intent();
			intent.setClass(HomelistActivity.this, DetailActivity.class);
			intent.putExtra("detail_bid", map.get("hli_bid").toString());
			intent.putExtra("detail_name", map.get("hli_name").toString());
			startActivity(intent);
			
		}
		
	}
	
	/**
	 * 异步操作,并更新主ui
	 * @author entere
	 *
	 */
	private class MyTask extends AsyncTask<String, Void, ArrayList<HashMap<String, Object>>> {
		
		//任务执行之前的操作
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			//loadingDialog.show();
		}

		//在后台完成解析json操作
		@Override
		protected ArrayList<HashMap<String,Object>> doInBackground(String... params) {
			// TODO Auto-generated method stub
			String jsonString = httpUtil.getHttpJson(params[0]);
			data = parseJsonMulti(jsonString);
			return data;
		}

		//完成任务之后更新ui
		@Override
		protected void onPostExecute(ArrayList<HashMap<String, Object>> result) {
			// TODO Auto-generated method stub
			
			if(result.size()<1) {
				Toast.makeText(HomelistActivity.this, "亲,网络很不给力哦!", Toast.LENGTH_SHORT).show();
			}
			super.onPostExecute(result);
			
			lazyAdapter = new LazyAdapter(HomelistActivity.this, result);
	        pullToRefreshListView.setAdapter(lazyAdapter);
	        
	        pullToRefreshListView.onRefreshComplete();
	        
	        loadingDialog.dismiss();
			
		}

		
	}
	
	
	
	//解析json
	private ArrayList<HashMap<String, Object>> parseJsonMulti(String string) {
		ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String,Object>>();
		try {
			//JSONArray jsonObjs = new JSONObject(string.toString()).getJSONArray("article_list");
			JSONObject jsonObject = new JSONObject(string);
			JSONArray jsonArray = jsonObject.getJSONArray("article_list");
			for(int i=0; i<jsonArray.length(); i++) {
				HashMap<String,Object> hashMap = new HashMap<String, Object>();
				
				JSONObject obj = jsonArray.getJSONObject(i);
				
				Object hli_bid  = obj.get("article_id");
				Object hli_name = obj.get("user_name");
				Object hli_title = obj.get("article_title");
				Object hli_summary = obj.get("article_summary");
				Object hli_pubdate = obj.get("article_pubdate");
				Object hli_image = obj.get("article_pic");
				
				hashMap.put("hli_bid", hli_bid);
				hashMap.put("hli_name", hli_name);
				hashMap.put("hli_title", hli_title);
				hashMap.put("hli_summary", hli_summary);
				hashMap.put("hli_pubdate", hli_pubdate);
				hashMap.put("hli_image", hli_image);
				arrayList.add(hashMap);
			}
			return arrayList;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return arrayList;
		
	}
	
	
	
	

	
	
	
	

}





方法二:Runnable+handler

package com.blogchina.activity;


import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.blogchina.activity.R;
import com.blogchina.utils.HttpUtil;
import com.fedorvlasov.lazylist.LazyAdapter;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ListView;

public class BloglistActivity extends ListActivity{ 
	
	
	LazyAdapter adapter;
	HttpUtil httpUtil;
	MyHandler myHandler;
	private ListView listView;
	private ArrayList<HashMap<String, Object>> data;
	final private String url = "http://********/article/lists/is_only_pic/2/is_show_summary/1/limit/20";
	
	private ProgressDialog loadingDialog;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bloglist);
		loadingDialog = ProgressDialog.show(this, "", "加载中……");
		listView = (ListView) findViewById(R.id.bl_listView);
		
		myHandler = new MyHandler();
		MyTheard myTheard = new MyTheard();
		new Thread(myTheard).start();
		
		
		
	}
	
	class MyHandler extends Handler {
		public MyHandler() {
			// TODO Auto-generated constructor stub
		}
		public MyHandler(Looper L) {
			super(L);
		}
		
		@Override
		public void handleMessage(Message message){
			super.handleMessage(message);
			Bundle b = message.getData();
			String jsonStr = b.getString("jsonData");
	        data = parseJsonMulti(jsonStr);
	        adapter = new LazyAdapter(BloglistActivity.this, data);
	        listView.setAdapter(adapter);
	        
	        
	        loadingDialog.dismiss();
	        
	        listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					@SuppressWarnings("unchecked")
					HashMap<String, Object> map = (HashMap<String, Object>) listView.getItemAtPosition(arg2);
					Intent intent = new Intent();
					intent.setClass(BloglistActivity.this, DetailActivity.class);
					intent.putExtra("detail_bid", map.get("bli_bid").toString());
					intent.putExtra("detail_name", map.get("bli_name").toString());
					startActivity(intent);
					
					
					
				}
			});
	        
	        
	        
	        
		}
		
	}
	
	class MyTheard implements Runnable {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			httpUtil = new HttpUtil();
			
			Bundle bundle = new Bundle();
			bundle.putString("jsonData", httpUtil.getHttpJson(url));
			
			Message message = new Message();
			message.setData(bundle);
			BloglistActivity.this.myHandler.sendMessage(message);
			
		}
		
	}
	
	
	
	
	
	
	private ArrayList<HashMap<String, Object>> parseJsonMulti(String string) {
		ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String,Object>>();
		try {
			//JSONArray jsonObjs = new JSONObject(string.toString()).getJSONArray("article_list");
			JSONObject jsonObject = new JSONObject(string);
			JSONArray jsonArray = jsonObject.getJSONArray("article_list");
			for(int i=0; i<jsonArray.length(); i++) {
				HashMap<String,Object> hashMap = new HashMap<String, Object>();
				
				JSONObject obj = jsonArray.getJSONObject(i);
				
				Object bli_bid  = obj.get("article_id");
				Object bli_name = obj.get("user_name");
				Object bli_title = obj.get("article_title");
				Object bli_summary = obj.get("article_summary");
				Object bli_pubdate = obj.get("article_pubdate");
				Object bli_image = obj.get("article_pic");
				
				hashMap.put("bli_bid", bli_bid);
				hashMap.put("bli_name", bli_name);
				hashMap.put("bli_title", bli_title);
				hashMap.put("bli_summary", bli_summary);
				hashMap.put("bli_pubdate", bli_pubdate);
				hashMap.put("bli_image", bli_image);
				arrayList.add(hashMap);
			}
			return arrayList;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return arrayList;
		
	}
	
	
	
	

	
	
	
	

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值