Android腾讯微薄客户端开发十三:提及篇(与我有关的微博)

 
Java代码  收藏代码
  1. public class ReferActivity extends ListActivity implements OnItemClickListener,OnItemLongClickListener{  
  2.       
  3.     private DataHelper dataHelper;  
  4.     private UserInfo user;  
  5.     private MyWeiboSync weibo;  
  6.     private ListView listView;  
  7.     private ReferAdapter adapter;  
  8.     private JSONArray array;  
  9.     private AsyncImageLoader asyncImageLoader;  
  10.     private Handler handler;  
  11.     private ProgressDialog progressDialog;  
  12.     private View top_panel;  
  13.     private Button top_btn_left;  
  14.     private Button top_btn_right;  
  15.     private TextView top_title;  
  16.       
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.refer);  
  21.         setUpViews();  
  22.         setUpListeners();  
  23.           
  24.         dataHelper = DataBaseContext.getInstance(getApplicationContext());  
  25.         weibo = WeiboContext.getInstance();  
  26.           
  27.         List<UserInfo> userList = dataHelper.GetUserList(false);  
  28.           
  29.         SharedPreferences preferences = getSharedPreferences("default_user",Activity.MODE_PRIVATE);  
  30.         String nick = preferences.getString("user_default_nick""");  
  31.           
  32.         if (nick != "") {  
  33.             user = dataHelper.getUserByName(nick,userList);  
  34.             top_title.setText("提到我的");//顶部工具栏名称  
  35.         }  
  36.           
  37.         /*weibo.setAccessTokenKey(user.getToken()); 
  38.         weibo.setAccessTokenSecrect(user.getTokenSecret());*/  
  39.           
  40.         progressDialog = new ProgressDialog(ReferActivity.this);// 生成一个进度条  
  41.         progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  42.         progressDialog.setTitle("请稍等");  
  43.         progressDialog.setMessage("正在读取数据中!");  
  44.         handler = new GetReferHandler();  
  45.           
  46.         new GetReferThread().start();//耗时操作,开启一个新线程获取数据  
  47.         progressDialog.show();  
  48.     }  
  49.       
  50.     private void setUpViews(){  
  51.         listView = getListView();  
  52.         top_panel = (View)findViewById(R.id.refer_top);  
  53.         top_btn_left = (Button)top_panel.findViewById(R.id.top_btn_left);  
  54.         top_btn_right = (Button)top_panel.findViewById(R.id.top_btn_right);  
  55.         top_title = (TextView)top_panel.findViewById(R.id.top_title);  
  56.     }  
  57.       
  58.     private void setUpListeners(){  
  59.         listView.setOnItemClickListener(this);  
  60.         listView.setOnItemLongClickListener(this);  
  61.     }  
  62.       
  63.     class GetReferHandler extends Handler {  
  64.         @Override  
  65.         public void handleMessage(Message msg) {  
  66.             if(array!=null){  
  67.                 adapter = new ReferAdapter(ReferActivity.this, array);  
  68.                 listView.setAdapter(adapter);  
  69.             }  
  70.               
  71.             progressDialog.dismiss();// 关闭进度条  
  72.         }  
  73.     }  
  74.       
  75.     class GetReferThread extends Thread {  
  76.         @Override  
  77.         public void run() {  
  78.             String jsonStr = weibo.getRefers(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(), PageFlag.PageFlag_First, 0200);  
  79.             try {  
  80.                 JSONObject dataObj = new JSONObject(jsonStr).getJSONObject("data");  
  81.                 if(dataObj!=null){  
  82.                     array = dataObj.getJSONArray("info");  
  83.                 }  
  84.             } catch (JSONException e) {  
  85.                 e.printStackTrace();  
  86.             }  
  87.             Message msg = handler.obtainMessage();  
  88.             handler.sendMessage(msg);  
  89.         }  
  90.     }  
  91.       
  92.       
  93.     class ReferAdapter extends BaseAdapter {  
  94.         private Context context;  
  95.         private LayoutInflater inflater;  
  96.         private JSONArray array;  
  97.           
  98.         public ReferAdapter(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.             ReferViewHolder viewHolder = new ReferViewHolder();  
  124.             JSONObject data = (JSONObject)array.opt(position);  
  125.             JSONObject source = null;  
  126.             convertView = inflater.inflate(R.layout.refer_list_item, null);  
  127.             try {  
  128.                 source = data.getJSONObject("source");  
  129.             } catch (JSONException e1) {  
  130.                 e1.printStackTrace();   
  131.             }  
  132.             viewHolder.refer_headicon = (ImageView) convertView.findViewById(R.id.refer_headicon);  
  133.             viewHolder.refer_nick = (TextView) convertView.findViewById(R.id.refer_nick);  
  134.             viewHolder.refer_hasimage = (ImageView) convertView.findViewById(R.id.refer_hasimage);  
  135.             viewHolder.refer_timestamp = (TextView) convertView.findViewById(R.id.refer_timestamp);  
  136.             viewHolder.refer_origtext = (TextView) convertView.findViewById(R.id.refer_origtext);  
  137.             viewHolder.refer_source = (TextView) convertView.findViewById(R.id.refer_source);  
  138.               
  139.             if(data!=null){  
  140.                 try {  
  141.                     convertView.setTag(data.get("id"));  
  142.                     viewHolder.refer_nick.setText(data.getString("nick"));  
  143.                     viewHolder.refer_timestamp.setText(TimeUtil.converTime(Long.parseLong(data.getString("timestamp"))));  
  144.                     viewHolder.refer_origtext.setText(data.getString("origtext"), TextView.BufferType.SPANNABLE);  
  145.                       
  146.                     if(source!=null){  
  147.                         viewHolder.refer_source.setText(source.getString("nick")+":"+source.getString("origtext"));  
  148.                         viewHolder.refer_source.setBackgroundResource(R.drawable.source_bg);  
  149.                     }  
  150.                     //异步加载图片  
  151.                     Drawable cachedImage = asyncImageLoader.loadDrawable(data.getString("head")+"/100",viewHolder.refer_headicon, new ImageCallback(){  
  152.                         @Override  
  153.                         public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl) {  
  154.                             imageView.setImageDrawable(imageDrawable);  
  155.                         }  
  156.                     });  
  157.                     if (cachedImage == null) {  
  158.                         viewHolder.refer_headicon.setImageResource(R.drawable.icon);  
  159.                     } else {  
  160.                         viewHolder.refer_headicon.setImageDrawable(cachedImage);  
  161.                     }  
  162.                     if(data.getJSONArray("image")!=null){  
  163.                         viewHolder.refer_hasimage.setImageResource(R.drawable.hasimage);  
  164.                     }  
  165.                 } catch (JSONException e) {  
  166.                     e.printStackTrace();  
  167.                 } catch (Exception e) {  
  168.                     e.printStackTrace();  
  169.                 }  
  170.             }  
  171.             return convertView;  
  172.         }  
  173.     }  
  174.       
  175.     static class ReferViewHolder {  
  176.         private ImageView refer_headicon;  
  177.         private TextView refer_nick;  
  178.         private TextView refer_timestamp;  
  179.         private TextView refer_origtext;  
  180.         private TextView refer_source;  
  181.         private ImageView refer_hasimage;  
  182.     }  
  183.   
  184.     @Override  
  185.     public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position,long arg3) {  
  186.         CharSequence [] items = null;  
  187.         try {  
  188.             items= new CharSequence[]{"转播","对话","点评","收藏",((JSONObject)array.opt(position)).getString("nick"),"取消"};  
  189.         } catch (JSONException e) {  
  190.             e.printStackTrace();  
  191.         }  
  192.         new AlertDialog.Builder(ReferActivity.this).setTitle("选项").setItems(items,new DialogInterface.OnClickListener() {  
  193.             @Override  
  194.             public void onClick(DialogInterface dialog, int which) {  
  195.                         switch (which) {  
  196.                         case 0: {  
  197.                         }  
  198.                             break;  
  199.                         case 1: {  
  200.                         }  
  201.                             break;  
  202.                         case 2: {  
  203.                         }  
  204.                             break;  
  205.                         case 3: {  
  206.                         }  
  207.                             break;  
  208.                         case 4: {  
  209.                         }  
  210.                             break;  
  211.                         case 5: {  
  212.                         }  
  213.                             break;  
  214.                         default:  
  215.                             break;  
  216.                         }  
  217.             }  
  218.         }).show();  
  219.         return false;  
  220.     }  
  221.   
  222.     @Override  
  223.     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {  
  224.         JSONObject weiboInfo = (JSONObject)array.opt(position);  
  225.         Intent intent = new Intent(ReferActivity.this, WeiboDetailActivity.class);  
  226.         try {  
  227.             intent.putExtra("weiboid", weiboInfo.getString("id"));  
  228.             startActivity(intent);  
  229.         } catch (JSONException e) {  
  230.             e.printStackTrace();  
  231.         }  
  232.     }  
  233.   
  234. }  


Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="#ffffffff" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">  
  3.     <include android:id="@+id/refer_top" layout="@layout/top_panel" android:layout_alignParentTop="true"/>  
  4.     <ListView android:layout_below="@id/refer_top" android:id="@id/android:list" android:layout_width="fill_parent" android:cacheColorHint="#00000000"  
  5.         android:layout_height="wrap_content" android:layout_weight="1" android:divider="@drawable/list_divider"/>  
  6. </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/refer_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/refer_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/refer_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/refer_hasimage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/refer_timestamp"/>  
  10.         <TextView android:id="@+id/refer_origtext" android:textColor="#081008" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12.0sp" android:layout_below="@id/refer_nick"/>  
  11.         <TextView android:layout_marginLeft="6.0dip" android:id="@+id/refer_source" android:textColor="#101810" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10.0sp" android:layout_below="@id/refer_origtext" android:layout_alignParentBottom="true"/>  
  12.     </RelativeLayout>  
  13. </LinearLayout>  
posted @ 2012-02-20 17:30  aflylove  阅读(194)  评论(0编辑  收藏  举报