(转摘)Android腾讯微薄客户端开发十三:提及篇(

系统 1657 0
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,  0 20 0 );   
  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. }  
      public class ReferActivity extends ListActivity implements OnItemClickListener,OnItemLongClickListener{
	
	private DataHelper dataHelper;
	private UserInfo user;
	private MyWeiboSync weibo;
	private ListView listView;
	private ReferAdapter adapter;
	private JSONArray array;
	private AsyncImageLoader asyncImageLoader;
	private Handler handler;
	private ProgressDialog progressDialog;
	private View top_panel;
	private Button top_btn_left;
	private Button top_btn_right;
	private TextView top_title;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.refer);
		setUpViews();
		setUpListeners();
		
		dataHelper = DataBaseContext.getInstance(getApplicationContext());
		weibo = WeiboContext.getInstance();
		
		List<UserInfo> userList = dataHelper.GetUserList(false);
		
		SharedPreferences preferences = getSharedPreferences("default_user",Activity.MODE_PRIVATE);
		String nick = preferences.getString("user_default_nick", "");
		
		if (nick != "") {
			user = dataHelper.getUserByName(nick,userList);
			top_title.setText("提到我的");//顶部工具栏名称
		}
		
		/*weibo.setAccessTokenKey(user.getToken());
		weibo.setAccessTokenSecrect(user.getTokenSecret());*/
		
		progressDialog = new ProgressDialog(ReferActivity.this);// 生成一个进度条
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setTitle("请稍等");
		progressDialog.setMessage("正在读取数据中!");
		handler = new GetReferHandler();
		
		new GetReferThread().start();//耗时操作,开启一个新线程获取数据
		progressDialog.show();
	}
	
	private void setUpViews(){
		listView = getListView();
		top_panel = (View)findViewById(R.id.refer_top);
		top_btn_left = (Button)top_panel.findViewById(R.id.top_btn_left);
		top_btn_right = (Button)top_panel.findViewById(R.id.top_btn_right);
		top_title = (TextView)top_panel.findViewById(R.id.top_title);
	}
	
	private void setUpListeners(){
		listView.setOnItemClickListener(this);
		listView.setOnItemLongClickListener(this);
	}
	
	class GetReferHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			if(array!=null){
				adapter = new ReferAdapter(ReferActivity.this, array);
				listView.setAdapter(adapter);
			}
			
			progressDialog.dismiss();// 关闭进度条
		}
	}
	
	class GetReferThread extends Thread {
		@Override
		public void run() {
			String jsonStr = weibo.getRefers(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(), PageFlag.PageFlag_First, 0, 20, 0);
			try {
				JSONObject dataObj = new JSONObject(jsonStr).getJSONObject("data");
				if(dataObj!=null){
					array = dataObj.getJSONArray("info");
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			Message msg = handler.obtainMessage();
			handler.sendMessage(msg);
		}
	}
	
	
	class ReferAdapter extends BaseAdapter {
		private Context context;
		private LayoutInflater inflater;
		private JSONArray array;
		
		public ReferAdapter(Context context, JSONArray array) {
			super();
			this.context = context;
			this.array = array;
			this.inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return array.length();
		}

		@Override
		public Object getItem(int position) {
			return array.opt(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			asyncImageLoader = new AsyncImageLoader();
			ReferViewHolder viewHolder = new ReferViewHolder();
			JSONObject data = (JSONObject)array.opt(position);
			JSONObject source = null;
			convertView = inflater.inflate(R.layout.refer_list_item, null);
			try {
				source = data.getJSONObject("source");
			} catch (JSONException e1) {
				e1.printStackTrace(); 
			}
			viewHolder.refer_headicon = (ImageView) convertView.findViewById(R.id.refer_headicon);
			viewHolder.refer_nick = (TextView) convertView.findViewById(R.id.refer_nick);
			viewHolder.refer_hasimage = (ImageView) convertView.findViewById(R.id.refer_hasimage);
			viewHolder.refer_timestamp = (TextView) convertView.findViewById(R.id.refer_timestamp);
			viewHolder.refer_origtext = (TextView) convertView.findViewById(R.id.refer_origtext);
			viewHolder.refer_source = (TextView) convertView.findViewById(R.id.refer_source);
			
			if(data!=null){
				try {
					convertView.setTag(data.get("id"));
					viewHolder.refer_nick.setText(data.getString("nick"));
					viewHolder.refer_timestamp.setText(TimeUtil.converTime(Long.parseLong(data.getString("timestamp"))));
					viewHolder.refer_origtext.setText(data.getString("origtext"), TextView.BufferType.SPANNABLE);
					
					if(source!=null){
						viewHolder.refer_source.setText(source.getString("nick")+":"+source.getString("origtext"));
						viewHolder.refer_source.setBackgroundResource(R.drawable.source_bg);
					}
					//异步加载图片
					Drawable cachedImage = asyncImageLoader.loadDrawable(data.getString("head")+"/100",viewHolder.refer_headicon, new ImageCallback(){
	                    @Override
	                    public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl) {
	                        imageView.setImageDrawable(imageDrawable);
	                    }
	                });
					if (cachedImage == null) {
						viewHolder.refer_headicon.setImageResource(R.drawable.icon);
					} else {
						viewHolder.refer_headicon.setImageDrawable(cachedImage);
					}
					if(data.getJSONArray("image")!=null){
						viewHolder.refer_hasimage.setImageResource(R.drawable.hasimage);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return convertView;
		}
	}
	
	static class ReferViewHolder {
		private ImageView refer_headicon;
		private TextView refer_nick;
		private TextView refer_timestamp;
		private TextView refer_origtext;
		private TextView refer_source;
		private ImageView refer_hasimage;
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
		CharSequence [] items = null;
		try {
			items= new CharSequence[]{"转播","对话","点评","收藏",((JSONObject)array.opt(position)).getString("nick"),"取消"};
		} catch (JSONException e) {
			e.printStackTrace();
		}
		new AlertDialog.Builder(ReferActivity.this).setTitle("选项").setItems(items,new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
						switch (which) {
						case 0: {
						}
							break;
						case 1: {
						}
							break;
						case 2: {
						}
							break;
						case 3: {
						}
							break;
						case 4: {
						}
							break;
						case 5: {
						}
							break;
						default:
							break;
						}
			}
		}).show();
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		JSONObject weiboInfo = (JSONObject)array.opt(position);
		Intent intent = new Intent(ReferActivity.this, WeiboDetailActivity.class);
		try {
			intent.putExtra("weiboid", weiboInfo.getString("id"));
			startActivity(intent);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

}

    


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>  
      <?xml version="1.0" encoding="utf-8"?>
<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">
	<include android:id="@+id/refer_top" layout="@layout/top_panel" android:layout_alignParentTop="true"/>
	<ListView android:layout_below="@id/refer_top" android:id="@id/android:list" android:layout_width="fill_parent" android:cacheColorHint="#00000000"
		android:layout_height="wrap_content" android:layout_weight="1" android:divider="@drawable/list_divider"/>
</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>  
  14. http://helloandroid.iteye.com/blog/1136767

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


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论