新浪微博(十九)显示用户微博列表(WeiBoListActivity类)
2012-05-08 10:55
471 查看
效果图:
/* * 这个类是由DisplayWeiBoActivity类管理的5个卡片中的第一个,“微博大厅” * 它显示的数据就是微博的信息,包括微博标题,头像等等 * 它是被tianyi.sina包下的DisplayWeiBoActivity来管理的,是放在TabHost中被管理的 * 当用户点击微博大厅的按钮(默认显示的也是这个activity,它就是微博大厅), * 就会激活它,tabHost就会把这张卡片显示出来 */ package tianyi.sina.weibo; import java.util.ArrayList; import java.util.HashMap; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import tianyi.helper.SearchAdapter; import tianyi.helper.TextUtil; import tianyi.oauth.OauthUtils; import tianyi.sina.R; import android.app.ListActivity; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.text.Html; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter; /* * 如果要用ListActivity,则 Activity的Layout文件中必须包括一个(只能一个)ListView, * 且ListView的id= "@id/android:list" * 要注意ListView的Id为固定不变的,ListActivity会根据id自动查找ListView引用。若修改就会报错 */ public class WeiBoListActivity extends ListActivity { SimpleAdapter listAdapter; JSONObject jsonObj = null; String url; String httpMethod; String jsonData; SharedPreferences pres; String sina_access_token; String sina_access_secret; // ?????????? private int add = 0; private int num = 20; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.weibo_content); // 获取先前保存的SharedPrence文件 pres = this.getSharedPreferences("parameters", Context.MODE_PRIVATE); sina_access_token = pres.getString("sina_access_token", ""); sina_access_secret = pres.getString("sina_access_secret", ""); statushow(); } private void statushow() { // 为了避免程序获取数据时,由于网络原因或者数据量很大而导致程序无响应(ANR),因而使用了异步处理 Handler handler = new Handler(); Runnable mTasks = new Runnable() { @Override public void run() { // 获取微博数据 try { getWeiBoData(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; /* * 弹出提示框,它主要为了是界面更加友好,实际上它和数据加载没有关系,只是同用户进行交互 */ final ProgressDialog myDialog = ProgressDialog.show( WeiBoContentActivity.this, "稍等", "亲,正在努力加载数据。。。", true, true); handler.post(mTasks); new Thread() { @Override public void run() { try { // 休眠10秒钟 sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } myDialog.dismiss(); } }.start(); } /* * 获取授权用户的微博信息列表,包括:头像、新动态的标题、内容、评论等等 */ private void getWeiBoData() throws JSONException { // Http请求方式,新浪官方API规定使用GET方式 httpMethod = "GET"; // 新浪官方API支持JSON方式 JSONArray jArray = null; // 请求的微博条数 String countnum; if (TextUtil.isEmpty(sina_access_token) || TextUtil.isEmpty(sina_access_secret)) { // 若有一项为空,自动刷新 Intent intent = new Intent(this, RefreshWeiBoList.class); startActivity(intent); } else { // 放置微博信息的数组 ArrayList<HashMap<String, Object>> weiBoList = new ArrayList<HashMap<String, Object>>(); // 获取当前登录用户及其所关注用户的最新微博的URL // http://api.t.sina.com.cn/statuses/friends_timeline.(json%7cxml) url = "http://api.t.sina.com.cn/statuses/friends_timeline.json"; OauthUtils.getInstance().initSinaData(); if (add == 1) { num += 20; add = 0; } countnum = num + ""; jsonData = new Weibo().getWeiBoList(url, httpMethod, sina_access_token, sina_access_secret, null, null, null, null, countnum); try { /* * 带整型参数的重载版本都构造一个空的JsonArray实例, 且指定了JsonArray初始容量的大小 * ,以避免不必要的重新分配内存。 */ jArray = new JSONArray(jsonData); } catch (JSONException e) { e.printStackTrace(); } for (int i = 0; i < jArray.length(); i++) { JSONObject jObeject = jArray.getJSONObject(i); JSONObject user = jObeject.getJSONObject("user"); // user_imgurl = user.getString("profile_image_url"); HashMap<String, Object> map = new HashMap<String, Object>(); // 微博标题 map.put("name", user.getString("name")); // 图片的路径 // 示例: // "profile_image_url" : "http://tp3.sinaimg.cn/1799833402/50/1283207796", map.put("image", user.getString("profile_image_url")); // 示例:"text" : "这里是微博的文字信息", map.put("content", jObeject.getString("text")); // 获取该微博来自哪一个平台 // "source" // :"<a href=\"http://t.sina.com.cn\" rel=\"nofollow\">新浪微博</a>" map.put("whichPlatform", Html.fromHtml(jObeject.getString("source"))); // map.put("pinglun", jObeject.getString("mcount")); // map.put("zhuanfa", jObeject.getString("count")); // 这里要强调一点,就是Map里面放置的成员个数, // 一定要和adapter中数组的成员个数相匹配,数量匹配、名字匹配,否则就会出错,而且这种错误很难调 weiBoList.add(map); } /* * SearchAdapter继承的是:SimpleAdapter,负责加载网络图片 * * @param context 上下文 * * @param data List列表存放的数据 * * @param resource 绑定的页面 ,例如:R.layout.search_item * * @param from 绑定控件对应的数组里面的值名称 * * @param to 绑定控件的ID */ listAdapter = new SearchAdapter(this, weiBoList, R.layout.weibo_content_item, new String[] { "name", "image", "content", "whichPlatform" }, new int[] { R.id.weibo_content_item_textView1_name, R.id.weibo_content_item_imageView1, R.id.weibo_content_item_textView1_content, R.id.weibo_content_item_textView1_whichPlatform}); } // 去掉两个listview元素之间的分割线 getListView().setDivider(null); setListAdapter(listAdapter); } @Override protected void onListItemClick(ListView l, View v, final int position, final long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); // 此方法负责对listView中点击的一条子项的监听和处理 Handler handler = new Handler(); Runnable mTasks = new Runnable() { @Override public void run() { Intent intent = new Intent(); // 选中的哪一条微博的位置,它的功能用微博的ID号来代替 // intent.putExtra("position", position + ""); // 用来标识这条微博的ID号 intent.putExtra("weiboID", id + ""); // 一个页面要显示的微博条数 intent.putExtra("num", num + ""); intent.setClass(WeiBoContentActivity.this, SingleWeiBoActivity.class); startActivity(intent); } }; /* * ProgressDialog.show(WeiBoContentActivity.this, "正在连接服务器..", * "连接中,请稍后..", true, true); */ handler.post(mTasks); } }
相关文章推荐
- Python 获取新浪微博指定用户的微博列表
- 求助:php通过新浪微博接口 api 如果获取某一地区下所有用户的微博列表?或使用包含某一关键词的微博列表
- Easyui Datagrid 如何实现后台交互显示用户数据列表
- Servlet(12)连接数据库并显示用户列表
- Android调用新浪微博显示用户信息
- 融云 解决用户头像会话列表不显示问题
- 计算机管理本地用户和组不显示用户列表
- 显示在线用户列表
- 零授权 抓取新浪微博任何用户的微博内容
- 通达OA-今日学习:无法显示在线人数、无法看到用户列表、无法弹出短信等
- Jquery 类似新浪微博,鼠标移到头像,用浮动窗口显示用户信息,已做成一个jquery插件
- laravel大型项目系列教程(四)之显示文章列表和用户修改文章
- PHP采用curl模仿用户登陆新浪微博发微博的方法
- PHPCMS V9后台表单向导中让“信息列表"页面直接显示用户留言的信息列表
- iOS 新浪微博-5.1 首页微博列表_时间/配图
- Python:获取新浪微博用户的收听列表和粉丝列表
- jsp实现在线用户列表显示
- 【安装调试】openfire无法显示用户列表
- 显示SAP中所有用户列表
- SAP Basis如何显示SAP中所有用户列表