亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

(轉摘)Android騰訊微薄客戶端開發十二:博主的偶

系統 1823 0

(轉摘)Android騰訊微薄客戶端開發十二:博主的偶像
點擊收聽,顯示此博主的偶像列表,點擊列表,將顯示偶像的詳情界面
(轉摘)Android騰訊微薄客戶端開發十二:博主的偶像

Java代碼 復制代碼 ? 收藏代碼
  1. public ? class ?IdolActivity? 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 ?IdolThread?thread; ??
  9. ???? private ?ProgressDialog?progressDialog; ??
  10. ???? private ?JSONArray?array; ??
  11. ???? private ?IdolAdapter?adapter; ??
  12. ???? private ?ListView?listView; ??
  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.idol); ??
  24. ????????setUpViews(); //設置view ??
  25. ????????setUpListeners(); //設置listenter ??
  26. ???????? ??
  27. ????????asyncImageLoader?=? new ?AsyncImageLoader(); ??
  28. ????????dataHelper?=? new ?DataHelper(IdolActivity. this ); ??
  29. ????????weibo?=? new ?MyWeiboSync(); ??
  30. ????????List<UserInfo>?userList?=?dataHelper.GetUserList( false ); ??
  31. ???????? ??
  32. ????????SharedPreferences?preferences?=?getSharedPreferences( "default_user" ,Activity.MODE_PRIVATE); ??
  33. ????????String?nick?=?preferences.getString( "user_default_nick" ,? "" ); ??
  34. ???????? if ?(nick?!=? "" )?{ ??
  35. ????????????user?=?dataHelper.getUserByName(nick,userList); ??
  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(IdolActivity. this ); //?生成一個進度條 ??
  46. ????????progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); ??
  47. ????????progressDialog.setTitle( "請稍等" ); ??
  48. ????????progressDialog.setMessage( "正在讀取數據中!" ); ??
  49. ???????? ??
  50. ????????handler?=? new ?IdolHandler(); ??
  51. ????????thread?=? new ?IdolThread(); ??
  52. ????????thread.start(); //開啟一個線程獲取數據 ??
  53. ????????progressDialog.show(); ??
  54. ????} ??
  55. ???? ??
  56. ???? private ? void ?setUpViews(){ ??
  57. ????????listView?=?getListView(); ??
  58. ????????top_panel?=?(View)findViewById(R.id.idol_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 ?IdolThread? extends ?Thread?{ ??
  69. ???????? @Override ??
  70. ???????? public ? void ?run()?{ ??
  71. ????????????String?jsonStr?=?weibo.getIdols(weibo.getAccessTokenKey(),?weibo.getAccessTokenSecrect(),? 20 ,? 0 ,?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 ?IdolHandler? extends ?Handler?{? ??
  85. ???????? @Override ??
  86. ???????? public ? void ?handleMessage(Message?msg){ ??
  87. ????????????adapter?=? new ?IdolAdapter(IdolActivity. this ,?array); ??
  88. ????????????listView.setAdapter(adapter); ??
  89. ????????????progressDialog.dismiss(); //?關閉進度條 ??
  90. ????????} ??
  91. ????} ??
  92. ???? ??
  93. ???? class ?IdolAdapter? extends ?BaseAdapter?{ ??
  94. ???????? private ?Context?context; ??
  95. ???????? private ?LayoutInflater?inflater; ??
  96. ???????? private ?JSONArray?array; ??
  97. ???????? ??
  98. ???????? public ?IdolAdapter(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. ????????????IdolViewHolder?viewHolder?=? new ?IdolViewHolder(); ??
  124. ????????????JSONObject?data?=?(JSONObject)array.opt(position); ??
  125. ????????????convertView?=?inflater.inflate(R.layout.idol_list_item,? null ); ??
  126. ???????????? ??
  127. ????????????viewHolder.idol_headicon?=?(ImageView)?convertView.findViewById(R.id.idol_headicon); ??
  128. ????????????viewHolder.idol_nick?=?(TextView)?convertView.findViewById(R.id.idol_nick); ??
  129. ????????????viewHolder.idol_name?=?(TextView)?convertView.findViewById(R.id.idol_name); ??
  130. ???????????? ??
  131. ????????????Drawable?cachedImage?=? null ; ??
  132. ???????????? if (data!= null ){ ??
  133. ???????????????? try ?{ ??
  134. ????????????????????convertView.setTag(data.get( "name" )); ??
  135. ????????????????????viewHolder.idol_nick.setText(data.getString( "nick" )); ??
  136. ????????????????????viewHolder.idol_name.setText( "@" +data.getString( "name" )); ??
  137. ???????????????????? //異步加載圖片 ??
  138. ????????????????????cachedImage?=?asyncImageLoader.loadDrawable(data.getString( "head" )+ "/100" ,viewHolder.idol_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.idol_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 ?IdolViewHolder?{ ??
  158. ???????? private ?ImageView?idol_headicon; ??
  159. ???????? private ?TextView?idol_nick; ??
  160. ???????? private ?TextView?idol_name; ??
  161. ????} ??
  162. ??
  163. ???? @Override ??
  164. ???? public ? void ?onItemClick(AdapterView<?>?arg0,?View?arg1,? int ?position,? long ?arg3)?{ ??
  165. ????????Intent?intent?=? new ?Intent(IdolActivity. 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. }??
      public class IdolActivity extends ListActivity implements OnItemClickListener{

	private DataHelper dataHelper;
	private UserInfo user;
	private MyWeiboSync weibo;
	private Handler handler;
	private AsyncImageLoader asyncImageLoader; 
	private IdolThread thread;
	private ProgressDialog progressDialog;
	private JSONArray array;
	private IdolAdapter adapter;
	private ListView listView;
	private String name;
	private String currentNick;//當前界面的昵稱
	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.idol);
		setUpViews();//設置view
		setUpListeners();//設置listenter
		
		asyncImageLoader = new AsyncImageLoader();
		dataHelper = new DataHelper(IdolActivity.this);
		weibo = new MyWeiboSync();
		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);
		}
		weibo.setAccessTokenKey(user.getToken());
		weibo.setAccessTokenSecrect(user.getTokenSecret());
		
		Intent intent = getIntent();
		name = intent.getStringExtra("name");//獲取從前面頁面傳遞過來的數據
		currentNick = intent.getStringExtra("nick");
		top_title.setText(currentNick+"的偶像");
		
		progressDialog = new ProgressDialog(IdolActivity.this);// 生成一個進度條
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setTitle("請稍等");
		progressDialog.setMessage("正在讀取數據中!");
		
		handler = new IdolHandler();
		thread = new IdolThread();
		thread.start();//開啟一個線程獲取數據
		progressDialog.show();
	}
	
	private void setUpViews(){
		listView = getListView();
		top_panel = (View)findViewById(R.id.idol_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);
	}
	
	class IdolThread extends Thread {
		@Override
		public void run() {
			String jsonStr = weibo.getIdols(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(), 20, 0, name);
			try {
				JSONObject dataObj = new JSONObject(jsonStr).getJSONObject("data");
				array = dataObj.getJSONArray("info");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			//通知handler處理數據
			Message msg = handler.obtainMessage();
			handler.sendMessage(msg);
		}
	}
	
	class IdolHandler extends Handler { 
		@Override
		public void handleMessage(Message msg){
			adapter = new IdolAdapter(IdolActivity.this, array);
			listView.setAdapter(adapter);
			progressDialog.dismiss();// 關閉進度條
		}
	}
	
	class IdolAdapter extends BaseAdapter {
		private Context context;
		private LayoutInflater inflater;
		private JSONArray array;
		
		public IdolAdapter(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();
			IdolViewHolder viewHolder = new IdolViewHolder();
			JSONObject data = (JSONObject)array.opt(position);
			convertView = inflater.inflate(R.layout.idol_list_item, null);
			
			viewHolder.idol_headicon = (ImageView) convertView.findViewById(R.id.idol_headicon);
			viewHolder.idol_nick = (TextView) convertView.findViewById(R.id.idol_nick);
			viewHolder.idol_name = (TextView) convertView.findViewById(R.id.idol_name);
			
			Drawable cachedImage = null;
			if(data!=null){
				try {
					convertView.setTag(data.get("name"));
					viewHolder.idol_nick.setText(data.getString("nick"));
					viewHolder.idol_name.setText("@"+data.getString("name"));
					//異步加載圖片
					cachedImage = asyncImageLoader.loadDrawable(data.getString("head")+"/100",viewHolder.idol_headicon, new ImageCallback(){
	                    @Override
	                    public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl) {
	                        imageView.setImageDrawable(imageDrawable);
	                    }
	                });
					if(cachedImage == null) {
						viewHolder.idol_headicon.setImageResource(R.drawable.icon);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return convertView;
		}
	}
	
	static class IdolViewHolder {
		private ImageView idol_headicon;
		private TextView idol_nick;
		private TextView idol_name;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		Intent intent = new Intent(IdolActivity.this,UserInfoActivity.class);
		try {
			JSONObject fansInfo = (JSONObject)array.opt(position);
			JSONArray tweets = fansInfo.getJSONArray("tweet");
			JSONObject tweet = null;
			if(tweets!=null&&tweets.length()>0){
				tweet = (JSONObject)tweets.opt(0);
				intent.putExtra("origtext", tweet.getString("text"));
				intent.putExtra("timestamp", TimeUtil.getStandardTime(tweet.getLong("timestamp")));
			}
			intent.putExtra("name", fansInfo.getString("name"));
			intent.putExtra("nick", fansInfo.getString("nick"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		startActivity(intent);//跳轉到用戶信息界面
	}
}

    


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/idol_top" ?layout= "@layout/top_panel" ?android:layout_alignParentTop= "true" /> ??
  5. ????<ListView?android:id= "@id/android:list" ?android:layout_below= "@id/idol_top" ?android:layout_width= "fill_parent" ?android:cacheColorHint= "#00000000" ??
  6. ????????android:layout_height= "fill_parent" ?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/idol_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/idol_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>??
      <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffffff"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<include android:id="@+id/idol_top" layout="@layout/top_panel" android:layout_alignParentTop="true"/>
	<ListView android:id="@id/android:list" android:layout_below="@id/idol_top" android:layout_width="fill_parent" android:cacheColorHint="#00000000"
		android:layout_height="fill_parent" android:layout_weight="1" android:divider="@drawable/list_divider"/>
	<RelativeLayout android:layout_width="fill_parent" android:layout_height="40.0dip" android:layout_alignParentBottom="true">
		<Button android:id="@+id/idol_back_btn" android:layout_width="40.0dip" android:drawableTop="@drawable/btn_back_selector" android:background="@drawable/bottom_back_bg"
			android:layout_height="40.0dip"  android:layout_alignParentLeft="true"/>
		<Button android:id="@+id/idol_tohome_btn" android:layout_width="40.0dip"
			android:layout_height="40.0dip" android:drawableTop="@drawable/btn_home_selector" android:background="@drawable/bottom_home_bg" android:layout_alignParentRight="true"/>
	</RelativeLayout>
</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/idol_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/idol_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/idol_name" ?android:layout_marginLeft= "6.0dip" ?android:layout_below= "@id/idol_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>??
  11. http://helloandroid.iteye.com/blog/1136761

(轉摘)Android騰訊微薄客戶端開發十二:博主的偶像


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 中文字幕天天躁夜夜狠狠综合 | 日韩欧美国产一区二区三区四区 | 亚洲欧美国产18 | 午夜dy888理论不卡达达兔 | 狠狠色婷婷丁香综合久久韩国 | a视频网站 | 97国产成人精品免费视频 | 国产精品久久久久无毒 | 色噜噜狠狠色综合久 | 中文精品久久久久国产不卡 | 久久综合爱 | 色综合欧美色综合七久久 | 激情综合视频 | jizz成熟丰满中国妇女 | 亚洲精品久久久久久下一站 | 四虎永久在线精品视频播放 | 久久夜夜操妹子 | 欧美成人全部免费观看1314色 | 国产精品视频一区二区三区 | 欧美人成人亚洲专区中文字幕 | 国产精选一区二区 | 亚洲国产麻豆 | 99久久精品99999久久 | 国产精品久久久久乳精品爆 | 香蕉视频禁止18 | 在线日韩国产 | 久草性视频 | 天天草夜夜草 | 波多野结衣久久精品 | 欧美激情在线播放一区二区 | 国产码欧美日韩高清综合一区 | 日韩美女中文字幕 | 奇米777me | 麻豆国产一区 | 狠狠插狠狠干 | 天天做天天做天天综合网 | 四虎永久免费观看 | 性生活免费网站 | 午夜视频在线观看网站 | 男人天堂a | 美国毛片一级e片黑人片 |