Android腾讯微薄客户端开发十:博主广播篇

 
点击广播,显示此博主所发表的所有微博 
 

Java代码  收藏代码
  1. public class TweetsActivity extends ListActivity implements OnItemClickListener{  
  2.     private DataHelper dataHelper;  
  3.     private UserInfo user;  
  4.     private MyWeiboSync weibo;  
  5.     private Handler handler;  
  6.     private AsyncImageLoader asyncImageLoader;   
  7.     private TweetsThread thread;  
  8.     private ListView listView;  
  9.     private ProgressDialog progressDialog;  
  10.     private JSONArray array;  
  11.     private TweetsAdapter adapter;  
  12.     private String name;  
  13.     private String currentNick;//当前界面的昵称  
  14.     private View top_panel;  
  15.     private Button top_btn_left;  
  16.     private Button top_btn_right;  
  17.     private TextView top_title;  
  18.       
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.tweets);  
  23.         setUpViews();//设置view  
  24.         setUpListeners();//设置listenter  
  25.           
  26.         asyncImageLoader = new AsyncImageLoader();  
  27.         dataHelper = new DataHelper(TweetsActivity.this);  
  28.         weibo = new MyWeiboSync();  
  29.         List<UserInfo> userList = dataHelper.GetUserList(false);  
  30.           
  31.         SharedPreferences preferences = getSharedPreferences("default_user",Activity.MODE_PRIVATE);  
  32.         String nick = preferences.getString("user_default_nick""");  
  33.         if (nick != "") {  
  34.             user = dataHelper.getUserByName(nick,userList);  
  35.         }  
  36.           
  37.         weibo.setAccessTokenKey(user.getToken());  
  38.         weibo.setAccessTokenSecrect(user.getTokenSecret());  
  39.           
  40.         Intent intent = getIntent();  
  41.         name = intent.getStringExtra("name");//获取从前面页面传递过来的数据  
  42.         currentNick = intent.getStringExtra("nick");  
  43.         top_title.setText(currentNick+"的广播");  
  44.           
  45.         progressDialog = new ProgressDialog(TweetsActivity.this);// 生成一个进度条  
  46.         progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  47.         progressDialog.setTitle("请稍等");  
  48.         progressDialog.setMessage("正在读取数据中!");  
  49.           
  50.         handler = new TweetsHandler();  
  51.         thread = new TweetsThread();  
  52.         thread.start();//开启一个线程获取数据  
  53.         progressDialog.show();  
  54.           
  55.     }  
  56.       
  57.     private void setUpViews(){  
  58.         listView = getListView();  
  59.         top_panel = (View)findViewById(R.id.tweets_top);  
  60.         top_btn_left = (Button)top_panel.findViewById(R.id.top_btn_left);  
  61.         top_btn_right = (Button)top_panel.findViewById(R.id.top_btn_right);  
  62.         top_title = (TextView)top_panel.findViewById(R.id.top_title);  
  63.     }  
  64.       
  65.     private void setUpListeners(){  
  66.         listView.setOnItemClickListener(this);  
  67.     }  
  68.       
  69.     class TweetsThread extends Thread {  
  70.         @Override  
  71.         public void run() {  
  72.             String jsonStr = weibo.getTweets(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(),PageFlag.PageFlag_First, 0200,name);  
  73.             try {  
  74.                 JSONObject dataObj = new JSONObject(jsonStr).getJSONObject("data");   
  75.                 array = dataObj.getJSONArray("info");  
  76.             } catch (JSONException e) {  
  77.                 e.printStackTrace();  
  78.             }  
  79.             //通知handler处理数据  
  80.             Message msg = handler.obtainMessage();  
  81.             handler.sendMessage(msg);  
  82.         }  
  83.     }  
  84.       
  85.     class TweetsHandler extends Handler {   
  86.         @Override  
  87.         public void handleMessage(Message msg){  
  88.             adapter = new TweetsAdapter(TweetsActivity.this, array);  
  89.             listView.setAdapter(adapter);  
  90.             progressDialog.dismiss();// 关闭进度条  
  91.         }  
  92.     }  
  93.       
  94.     class TweetsAdapter extends BaseAdapter {  
  95.         private Context context;  
  96.         private LayoutInflater inflater;  
  97.         private JSONArray array;  
  98.           
  99.         public TweetsAdapter(Context context, JSONArray array) {  
  100.             super();  
  101.             this.context = context;  
  102.             this.array = array;  
  103.             this.inflater = LayoutInflater.from(context);  
  104.         }  
  105.   
  106.         @Override  
  107.         public int getCount() {  
  108.             return array.length();  
  109.         }  
  110.   
  111.         @Override  
  112.         public Object getItem(int position) {  
  113.             return array.opt(position);  
  114.         }  
  115.   
  116.         @Override  
  117.         public long getItemId(int position) {  
  118.             return position;  
  119.         }  
  120.   
  121.         @Override  
  122.         public View getView(final int position, View convertView, ViewGroup parent) {  
  123.             asyncImageLoader = new AsyncImageLoader();  
  124.             TweetsViewHolder viewHolder = new TweetsViewHolder();  
  125.             JSONObject data = (JSONObject)array.opt(position);   
  126.             JSONObject source = null;  
  127.             convertView = inflater.inflate(R.layout.tweets_list_item, null);  
  128.             try {  
  129.                 source = data.getJSONObject("source");  
  130.             } catch (JSONException e1) {  
  131.                 e1.printStackTrace();   
  132.             }  
  133.             viewHolder.tweets_headicon = (ImageView) convertView.findViewById(R.id.tweets_headicon);  
  134.             viewHolder.tweets_nick = (TextView) convertView.findViewById(R.id.tweets_nick);  
  135.             viewHolder.tweets_hasimage = (ImageView) convertView.findViewById(R.id.tweets_hasimage);  
  136.             viewHolder.tweets_timestamp = (TextView) convertView.findViewById(R.id.tweets_timestamp);  
  137.             viewHolder.tweets_origtext = (TextView) convertView.findViewById(R.id.tweets_origtext);  
  138.             viewHolder.tweets_source = (TextView) convertView.findViewById(R.id.tweets_source);  
  139.               
  140.             if(data!=null){  
  141.                 try {  
  142.                     convertView.setTag(data.get("id"));  
  143.                     viewHolder.tweets_nick.setText(data.getString("nick"));  
  144.                     viewHolder.tweets_timestamp.setText(TimeUtil.converTime(Long.parseLong(data.getString("timestamp"))));  
  145.                     viewHolder.tweets_origtext.setText(data.getString("origtext"), TextView.BufferType.SPANNABLE);  
  146.                       
  147.                     if(source!=null){  
  148.                         viewHolder.tweets_source.setText(source.getString("origtext"));  
  149.                         viewHolder.tweets_source.setBackgroundResource(R.drawable.source_bg);  
  150.                     }  
  151.                     //异步加载图片  
  152.                     Drawable cachedImage = asyncImageLoader.loadDrawable(data.getString("head")+"/100",viewHolder.tweets_headicon, new ImageCallback(){  
  153.                         @Override  
  154.                         public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl) {  
  155.                             imageView.setImageDrawable(imageDrawable);  
  156.                         }  
  157.                     });  
  158.                     if (cachedImage == null) {  
  159.                         viewHolder.tweets_headicon.setImageResource(R.drawable.icon);  
  160.                     } else {  
  161.                         viewHolder.tweets_headicon.setImageDrawable(cachedImage);  
  162.                     }  
  163.                     if(data.getJSONArray("image")!=null){  
  164.                         viewHolder.tweets_hasimage.setImageResource(R.drawable.hasimage);  
  165.                     }  
  166.                 } catch (JSONException e) {  
  167.                     e.printStackTrace();  
  168.                 } catch (Exception e) {  
  169.                     e.printStackTrace();  
  170.                 }  
  171.             }  
  172.             return convertView;  
  173.         }  
  174.     }  
  175.       
  176.     static class TweetsViewHolder {  
  177.         private ImageView tweets_headicon;  
  178.         private TextView tweets_nick;  
  179.         private TextView tweets_timestamp;  
  180.         private TextView tweets_origtext;  
  181.         private TextView tweets_source;  
  182.         private ImageView tweets_hasimage;  
  183.     }  
  184.       
  185.     @Override  
  186.     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {  
  187.         JSONObject weiboInfo = (JSONObject)array.opt(position);  
  188.         Intent intent = new Intent(TweetsActivity.this, WeiboDetailActivity.class);  
  189.         try {  
  190.             intent.putExtra("weiboid", weiboInfo.getString("id"));  
  191.             startActivity(intent);  
  192.         } catch (JSONException e) {  
  193.             e.printStackTrace();  
  194.         }  
  195.     }  
  196. }  


Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffffff"  
  3.     xmlns:android="http://schemas.android.com/apk/res/android">  
  4.     <include android:id="@+id/tweets_top" layout="@layout/top_panel" android:layout_alignParentTop="true"/>  
  5.     <ListView android:id="@id/android:list" android:layout_below="@id/tweets_top" android:layout_width="fill_parent" android:cacheColorHint="#00000000"  
  6.         android:layout_height="wrap_content" android:layout_weight="1" android:divider="@drawable/list_divider"/>  
  7.     <RelativeLayout android:layout_width="fill_parent" android:layout_height="40.0dip" android:layout_alignParentBottom="true">  
  8.         <Button android:id="@+id/mytweet_back_btn" android:layout_width="40.0dip" android:drawableTop="@drawable/btn_back_selector" android:background="@drawable/bottom_back_bg"  
  9.             android:layout_height="40.0dip"  android:layout_alignParentLeft="true"/>  
  10.         <Button android:id="@+id/mytweet_tohome_btn" android:layout_width="40.0dip"  
  11.             android:layout_height="40.0dip" android:drawableTop="@drawable/btn_home_selector" android:background="@drawable/bottom_home_bg" android:layout_alignParentRight="true"/>  
  12.     </RelativeLayout>  
  13. </RelativeLayout>  


Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:paddingTop="3.0dip" android:orientation="horizontal" android:background="@drawable/listitem_selector"  android:layout_width="fill_parent" android:layout_height="wrap_content">  
  3.     <RelativeLayout android:layout_width="50.0dip" android:layout_height="50.0dip" android:layout_weight="0.0">  
  4.         <ImageView android:id="@+id/tweets_headicon" android:layout_width="45.0dip" android:layout_height="45.0dip" android:scaleType="fitCenter" android:layout_centerInParent="true" />  
  5.     </RelativeLayout>  
  6.     <RelativeLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="4.0dip" android:layout_weight="1.0">  
  7.         <TextView android:id="@+id/tweets_nick" android:textColor="#000000" android:layout_width="wrap_content" android:layout_height="32.0dip" android:textSize="14.0sp" android:layout_alignParentLeft="true"/>  
  8.         <TextView android:id="@+id/tweets_timestamp" android:textColor="#ff000000" android:layout_width="wrap_content" android:layout_height="32.0dip" android:textSize="8.0sp" android:layout_alignParentRight="true"/>  
  9.         <ImageView android:id="@+id/tweets_hasimage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/tweets_timestamp"/>  
  10.         <TextView android:id="@+id/tweets_origtext" android:textColor="#081008" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12.0sp" android:layout_below="@id/tweets_nick"/>  
  11.         <TextView android:layout_marginLeft="6.0dip" android:id="@+id/tweets_source" android:textColor="#101810" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10.0sp" android:layout_below="@id/tweets_origtext" android:layout_alignParentBottom="true"/>  
  12.     </RelativeLayout>  
  13. </LinearLayout>  
posted @ 2012-02-20 17:28  aflylove  阅读(191)  评论(0编辑  收藏  举报