Android腾讯微薄客户端开发十一:博主的粉丝

 
点击听众,显示博主的粉丝列表,点击列表中的粉丝,显示此粉丝详情 
 

Java代码  收藏代码
  1. public class FansActivity extends ListActivity implements OnItemClickListener{  
  2.   
  3.     private DataHelper dataHelper;  
  4.     private UserInfo user;  
  5.     private MyWeiboSync weibo;  
  6.     private Handler handler;  
  7.     private AsyncImageLoader asyncImageLoader;   
  8.     private FansThread thread;  
  9.     private ListView listView;  
  10.     private ProgressDialog progressDialog;  
  11.     private JSONArray array;  
  12.     private FansAdapter adapter;  
  13.     private String name;  
  14.     private String currentNick;//当前界面的昵称  
  15.     private View top_panel;  
  16.     private Button top_btn_left;  
  17.     private Button top_btn_right;  
  18.     private TextView top_title;  
  19.       
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.fans);  
  24.         setUpViews();//设置view  
  25.         setUpListeners();//设置listenter  
  26.         asyncImageLoader = new AsyncImageLoader();  
  27.         dataHelper = new DataHelper(FansActivity.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(FansActivity.this);// 生成一个进度条  
  46.         progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  47.         progressDialog.setTitle("请稍等");  
  48.         progressDialog.setMessage("正在读取数据中!");  
  49.           
  50.         handler = new FansHandler();  
  51.         thread = new FansThread();  
  52.         thread.start();//开启一个线程获取数据  
  53.         progressDialog.show();  
  54.     }  
  55.       
  56.     private void setUpViews(){  
  57.         listView = getListView();  
  58.         top_panel = (View)findViewById(R.id.fans_top);  
  59.         top_btn_left = (Button)top_panel.findViewById(R.id.top_btn_left);  
  60.         top_btn_right = (Button)top_panel.findViewById(R.id.top_btn_right);  
  61.         top_title = (TextView)top_panel.findViewById(R.id.top_title);  
  62.     }  
  63.       
  64.     private void setUpListeners(){  
  65.         listView.setOnItemClickListener(this);  
  66.     }  
  67.       
  68.     class FansThread extends Thread {  
  69.         @Override  
  70.         public void run() {  
  71.             String jsonStr = weibo.getFans(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(), 200, name);  
  72.             try {  
  73.                 JSONObject dataObj = new JSONObject(jsonStr).getJSONObject("data");  
  74.                 array = dataObj.getJSONArray("info");  
  75.             } catch (JSONException e) {  
  76.                 e.printStackTrace();  
  77.             }  
  78.             //通知handler处理数据  
  79.             Message msg = handler.obtainMessage();  
  80.             handler.sendMessage(msg);  
  81.         }  
  82.     }  
  83.       
  84.     class FansHandler extends Handler {   
  85.         @Override  
  86.         public void handleMessage(Message msg){  
  87.             adapter = new FansAdapter(FansActivity.this, array);  
  88.             listView.setAdapter(adapter);  
  89.             progressDialog.dismiss();// 关闭进度条  
  90.         }  
  91.     }  
  92.       
  93.     class FansAdapter extends BaseAdapter {  
  94.         private Context context;  
  95.         private LayoutInflater inflater;  
  96.         private JSONArray array;  
  97.           
  98.         public FansAdapter(Context context, JSONArray array) {  
  99.             super();  
  100.             this.context = context;  
  101.             this.array = array;  
  102.             this.inflater = LayoutInflater.from(context);  
  103.         }  
  104.   
  105.         @Override  
  106.         public int getCount() {  
  107.             return array.length();  
  108.         }  
  109.   
  110.         @Override  
  111.         public Object getItem(int position) {  
  112.             return array.opt(position);  
  113.         }  
  114.   
  115.         @Override  
  116.         public long getItemId(int position) {  
  117.             return position;  
  118.         }  
  119.   
  120.         @Override  
  121.         public View getView(final int position, View convertView, ViewGroup parent) {  
  122.             asyncImageLoader = new AsyncImageLoader();  
  123.             FansViewHolder viewHolder = new FansViewHolder();  
  124.             JSONObject data = (JSONObject)array.opt(position);  
  125.             convertView = inflater.inflate(R.layout.fans_list_item, null);  
  126.               
  127.             viewHolder.fans_headicon = (ImageView) convertView.findViewById(R.id.fans_headicon);  
  128.             viewHolder.fans_nick = (TextView) convertView.findViewById(R.id.fans_nick);  
  129.             viewHolder.fans_name = (TextView) convertView.findViewById(R.id.fans_name);  
  130.               
  131.             Drawable cachedImage = null;  
  132.             if(data!=null){  
  133.                 try {  
  134.                     convertView.setTag(data.get("name"));  
  135.                     viewHolder.fans_nick.setText(data.getString("nick"));  
  136.                     viewHolder.fans_name.setText("@"+data.getString("name"));  
  137.                     //异步加载图片  
  138.                     cachedImage = asyncImageLoader.loadDrawable(data.getString("head")+"/100",viewHolder.fans_headicon, new ImageCallback(){  
  139.                         @Override  
  140.                         public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl) {  
  141.                             imageView.setImageDrawable(imageDrawable);  
  142.                         }  
  143.                     });  
  144.                     if (cachedImage == null) {  
  145.                         viewHolder.fans_headicon.setImageResource(R.drawable.icon);  
  146.                     }  
  147.                 } catch (JSONException e) {  
  148.                     e.printStackTrace();  
  149.                 } catch (Exception e) {  
  150.                     e.printStackTrace();  
  151.                 }  
  152.             }  
  153.             return convertView;  
  154.         }  
  155.     }  
  156.       
  157.     static class FansViewHolder {  
  158.         private ImageView fans_headicon;  
  159.         private TextView fans_nick;  
  160.         private TextView fans_name;  
  161.     }  
  162.   
  163.     @Override  
  164.     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3){  
  165.         Intent intent = new Intent(FansActivity.this,UserInfoActivity.class);  
  166.         try {  
  167.             JSONObject fansInfo = (JSONObject)array.opt(position);  
  168.             JSONArray tweets = fansInfo.getJSONArray("tweet");  
  169.             JSONObject tweet = null;  
  170.             if(tweets!=null&&tweets.length()>0){  
  171.                 tweet = (JSONObject)tweets.opt(0);  
  172.                 intent.putExtra("origtext", tweet.getString("text"));  
  173.                 intent.putExtra("timestamp", TimeUtil.getStandardTime(tweet.getLong("timestamp")));  
  174.             }  
  175.             intent.putExtra("name", fansInfo.getString("name"));  
  176.             intent.putExtra("nick", fansInfo.getString("nick"));  
  177.         } catch (JSONException e) {  
  178.             e.printStackTrace();  
  179.         }  
  180.         startActivity(intent);//跳转到用户信息界面  
  181.     }  
  182. }  


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/fans_top" layout="@layout/top_panel" android:layout_alignParentTop="true"/>  
  5.     <ListView android:id="@id/android:list" android:layout_below="@id/fans_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/fans_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/fans_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/fans_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/fans_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/fans_name" android:layout_marginLeft="6.0dip" android:layout_below="@id/fans_nick" android:textColor="#ff000000" android:layout_width="wrap_content" android:layout_height="32.0dip" android:textSize="8.0sp" android:layout_alignParentLeft="true"/>  
  9.     </RelativeLayout>  
  10. </LinearLayout>  
posted @ 2012-02-20 17:28  aflylove  阅读(211)  评论(0编辑  收藏  举报