
點擊廣播,顯示此博主所發表的所有微博

- public ? class ?TweetsActivity? extends ?ListActivity? implements ?OnItemClickListener{ ??
- ???? private ?DataHelper?dataHelper; ??
- ???? private ?UserInfo?user; ??
- ???? private ?MyWeiboSync?weibo; ??
- ???? private ?Handler?handler; ??
- ???? private ?AsyncImageLoader?asyncImageLoader;? ??
- ???? private ?TweetsThread?thread; ??
- ???? private ?ListView?listView; ??
- ???? private ?ProgressDialog?progressDialog; ??
- ???? private ?JSONArray?array; ??
- ???? private ?TweetsAdapter?adapter; ??
- ???? 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.tweets); ??
- ????????setUpViews(); //設置view ??
- ????????setUpListeners(); //設置listenter ??
- ???????? ??
- ????????asyncImageLoader?=? new ?AsyncImageLoader(); ??
- ????????dataHelper?=? new ?DataHelper(TweetsActivity. 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(TweetsActivity. this ); //?生成一個進度條 ??
- ????????progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); ??
- ????????progressDialog.setTitle( "請稍等" ); ??
- ????????progressDialog.setMessage( "正在讀取數據中!" ); ??
- ???????? ??
- ????????handler?=? new ?TweetsHandler(); ??
- ????????thread?=? new ?TweetsThread(); ??
- ????????thread.start(); //開啟一個線程獲取數據 ??
- ????????progressDialog.show(); ??
- ???????? ??
- ????} ??
- ???? ??
- ???? private ? void ?setUpViews(){ ??
- ????????listView?=?getListView(); ??
- ????????top_panel?=?(View)findViewById(R.id.tweets_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 ?TweetsThread? extends ?Thread?{ ??
- ???????? @Override ??
- ???????? public ? void ?run()?{ ??
- ????????????String?jsonStr?=?weibo.getTweets(weibo.getAccessTokenKey(),?weibo.getAccessTokenSecrect(),PageFlag.PageFlag_First,? 0 ,? 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 ?TweetsHandler? extends ?Handler?{? ??
- ???????? @Override ??
- ???????? public ? void ?handleMessage(Message?msg){ ??
- ????????????adapter?=? new ?TweetsAdapter(TweetsActivity. this ,?array); ??
- ????????????listView.setAdapter(adapter); ??
- ????????????progressDialog.dismiss(); //?關閉進度條 ??
- ????????} ??
- ????} ??
- ???? ??
- ???? class ?TweetsAdapter? extends ?BaseAdapter?{ ??
- ???????? private ?Context?context; ??
- ???????? private ?LayoutInflater?inflater; ??
- ???????? private ?JSONArray?array; ??
- ???????? ??
- ???????? public ?TweetsAdapter(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(); ??
- ????????????TweetsViewHolder?viewHolder?=? new ?TweetsViewHolder(); ??
- ????????????JSONObject?data?=?(JSONObject)array.opt(position);? ??
- ????????????JSONObject?source?=? null ; ??
- ????????????convertView?=?inflater.inflate(R.layout.tweets_list_item,? null ); ??
- ???????????? try ?{ ??
- ????????????????source?=?data.getJSONObject( "source" ); ??
- ????????????}? catch ?(JSONException?e1)?{ ??
- ????????????????e1.printStackTrace();? ??
- ????????????} ??
- ????????????viewHolder.tweets_headicon?=?(ImageView)?convertView.findViewById(R.id.tweets_headicon); ??
- ????????????viewHolder.tweets_nick?=?(TextView)?convertView.findViewById(R.id.tweets_nick); ??
- ????????????viewHolder.tweets_hasimage?=?(ImageView)?convertView.findViewById(R.id.tweets_hasimage); ??
- ????????????viewHolder.tweets_timestamp?=?(TextView)?convertView.findViewById(R.id.tweets_timestamp); ??
- ????????????viewHolder.tweets_origtext?=?(TextView)?convertView.findViewById(R.id.tweets_origtext); ??
- ????????????viewHolder.tweets_source?=?(TextView)?convertView.findViewById(R.id.tweets_source); ??
- ???????????? ??
- ???????????? if (data!= null ){ ??
- ???????????????? try ?{ ??
- ????????????????????convertView.setTag(data.get( "id" )); ??
- ????????????????????viewHolder.tweets_nick.setText(data.getString( "nick" )); ??
- ????????????????????viewHolder.tweets_timestamp.setText(TimeUtil.converTime(Long.parseLong(data.getString( "timestamp" )))); ??
- ????????????????????viewHolder.tweets_origtext.setText(data.getString( "origtext" ),?TextView.BufferType.SPANNABLE); ??
- ???????????????????? ??
- ???????????????????? if (source!= null ){ ??
- ????????????????????????viewHolder.tweets_source.setText(source.getString( "origtext" )); ??
- ????????????????????????viewHolder.tweets_source.setBackgroundResource(R.drawable.source_bg); ??
- ????????????????????} ??
- ???????????????????? //異步加載圖片 ??
- ????????????????????Drawable?cachedImage?=?asyncImageLoader.loadDrawable(data.getString( "head" )+ "/100" ,viewHolder.tweets_headicon,? new ?ImageCallback(){ ??
- ???????????????????????? @Override ??
- ???????????????????????? public ? void ?imageLoaded(Drawable?imageDrawable,ImageView?imageView,?String?imageUrl)?{ ??
- ????????????????????????????imageView.setImageDrawable(imageDrawable); ??
- ????????????????????????} ??
- ????????????????????}); ??
- ???????????????????? if ?(cachedImage?==? null )?{ ??
- ????????????????????????viewHolder.tweets_headicon.setImageResource(R.drawable.icon); ??
- ????????????????????}? else ?{ ??
- ????????????????????????viewHolder.tweets_headicon.setImageDrawable(cachedImage); ??
- ????????????????????} ??
- ???????????????????? if (data.getJSONArray( "image" )!= null ){ ??
- ????????????????????????viewHolder.tweets_hasimage.setImageResource(R.drawable.hasimage); ??
- ????????????????????} ??
- ????????????????}? catch ?(JSONException?e)?{ ??
- ????????????????????e.printStackTrace(); ??
- ????????????????}? catch ?(Exception?e)?{ ??
- ????????????????????e.printStackTrace(); ??
- ????????????????} ??
- ????????????} ??
- ???????????? return ?convertView; ??
- ????????} ??
- ????} ??
- ???? ??
- ???? static ? class ?TweetsViewHolder?{ ??
- ???????? private ?ImageView?tweets_headicon; ??
- ???????? private ?TextView?tweets_nick; ??
- ???????? private ?TextView?tweets_timestamp; ??
- ???????? private ?TextView?tweets_origtext; ??
- ???????? private ?TextView?tweets_source; ??
- ???????? private ?ImageView?tweets_hasimage; ??
- ????} ??
- ???? ??
- ???? @Override ??
- ???? public ? void ?onItemClick(AdapterView<?>?arg0,?View?arg1,? int ?position,? long ?arg3)?{ ??
- ????????JSONObject?weiboInfo?=?(JSONObject)array.opt(position); ??
- ????????Intent?intent?=? new ?Intent(TweetsActivity. this ,?WeiboDetailActivity. class ); ??
- ???????? try ?{ ??
- ????????????intent.putExtra( "weiboid" ,?weiboInfo.getString( "id" )); ??
- ????????????startActivity(intent); ??
- ????????}? catch ?(JSONException?e)?{ ??
- ????????????e.printStackTrace(); ??
- ????????} ??
- ????} ??
- }??
public class TweetsActivity extends ListActivity implements OnItemClickListener{ private DataHelper dataHelper; private UserInfo user; private MyWeiboSync weibo; private Handler handler; private AsyncImageLoader asyncImageLoader; private TweetsThread thread; private ListView listView; private ProgressDialog progressDialog; private JSONArray array; private TweetsAdapter adapter; 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.tweets); setUpViews();//設置view setUpListeners();//設置listenter asyncImageLoader = new AsyncImageLoader(); dataHelper = new DataHelper(TweetsActivity.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(TweetsActivity.this);// 生成一個進度條 progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.setTitle("請稍等"); progressDialog.setMessage("正在讀取數據中!"); handler = new TweetsHandler(); thread = new TweetsThread(); thread.start();//開啟一個線程獲取數據 progressDialog.show(); } private void setUpViews(){ listView = getListView(); top_panel = (View)findViewById(R.id.tweets_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 TweetsThread extends Thread { @Override public void run() { String jsonStr = weibo.getTweets(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(),PageFlag.PageFlag_First, 0, 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 TweetsHandler extends Handler { @Override public void handleMessage(Message msg){ adapter = new TweetsAdapter(TweetsActivity.this, array); listView.setAdapter(adapter); progressDialog.dismiss();// 關閉進度條 } } class TweetsAdapter extends BaseAdapter { private Context context; private LayoutInflater inflater; private JSONArray array; public TweetsAdapter(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(); TweetsViewHolder viewHolder = new TweetsViewHolder(); JSONObject data = (JSONObject)array.opt(position); JSONObject source = null; convertView = inflater.inflate(R.layout.tweets_list_item, null); try { source = data.getJSONObject("source"); } catch (JSONException e1) { e1.printStackTrace(); } viewHolder.tweets_headicon = (ImageView) convertView.findViewById(R.id.tweets_headicon); viewHolder.tweets_nick = (TextView) convertView.findViewById(R.id.tweets_nick); viewHolder.tweets_hasimage = (ImageView) convertView.findViewById(R.id.tweets_hasimage); viewHolder.tweets_timestamp = (TextView) convertView.findViewById(R.id.tweets_timestamp); viewHolder.tweets_origtext = (TextView) convertView.findViewById(R.id.tweets_origtext); viewHolder.tweets_source = (TextView) convertView.findViewById(R.id.tweets_source); if(data!=null){ try { convertView.setTag(data.get("id")); viewHolder.tweets_nick.setText(data.getString("nick")); viewHolder.tweets_timestamp.setText(TimeUtil.converTime(Long.parseLong(data.getString("timestamp")))); viewHolder.tweets_origtext.setText(data.getString("origtext"), TextView.BufferType.SPANNABLE); if(source!=null){ viewHolder.tweets_source.setText(source.getString("origtext")); viewHolder.tweets_source.setBackgroundResource(R.drawable.source_bg); } //異步加載圖片 Drawable cachedImage = asyncImageLoader.loadDrawable(data.getString("head")+"/100",viewHolder.tweets_headicon, new ImageCallback(){ @Override public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl) { imageView.setImageDrawable(imageDrawable); } }); if (cachedImage == null) { viewHolder.tweets_headicon.setImageResource(R.drawable.icon); } else { viewHolder.tweets_headicon.setImageDrawable(cachedImage); } if(data.getJSONArray("image")!=null){ viewHolder.tweets_hasimage.setImageResource(R.drawable.hasimage); } } catch (JSONException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } return convertView; } } static class TweetsViewHolder { private ImageView tweets_headicon; private TextView tweets_nick; private TextView tweets_timestamp; private TextView tweets_origtext; private TextView tweets_source; private ImageView tweets_hasimage; } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { JSONObject weiboInfo = (JSONObject)array.opt(position); Intent intent = new Intent(TweetsActivity.this, WeiboDetailActivity.class); try { intent.putExtra("weiboid", weiboInfo.getString("id")); startActivity(intent); } catch (JSONException e) { e.printStackTrace(); } } }
- <?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/tweets_top" ?layout= "@layout/top_panel" ?android:layout_alignParentTop= "true" /> ??
- ????<ListView?android:id= "@id/android:list" ?android:layout_below= "@id/tweets_top" ?android:layout_width= "fill_parent" ?android:cacheColorHint= "#00000000" ??
- ????????android:layout_height= "wrap_content" ?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/mytweet_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/mytweet_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>??
<?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/tweets_top" layout="@layout/top_panel" android:layout_alignParentTop="true"/> <ListView android:id="@id/android:list" android:layout_below="@id/tweets_top" android:layout_width="fill_parent" android:cacheColorHint="#00000000" android:layout_height="wrap_content" 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/mytweet_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/mytweet_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>
- <?xml?version= "1.0" ?encoding= "utf-8" ?> ??
- <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" > ??
- ????<RelativeLayout?android:layout_width= "50.0dip" ?android:layout_height= "50.0dip" ?android:layout_weight= "0.0" > ??
- ????????<ImageView?android:id= "@+id/tweets_headicon" ?android:layout_width= "45.0dip" ?android:layout_height= "45.0dip" ?android:scaleType= "fitCenter" ?android:layout_centerInParent= "true" ?/> ??
- ????</RelativeLayout> ??
- ????<RelativeLayout?android:orientation= "vertical" ?android:layout_width= "wrap_content" ?android:layout_height= "wrap_content" ?android:padding= "4.0dip" ?android:layout_weight= "1.0" > ??
- ????????<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" /> ??
- ????????<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" /> ??
- ????????<ImageView?android:id= "@+id/tweets_hasimage" ?android:layout_width= "wrap_content" ?android:layout_height= "wrap_content" ?android:layout_toLeftOf= "@id/tweets_timestamp" /> ??
- ????????<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" /> ??
- ????????<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" /> ??
- ????</RelativeLayout> ??
- </LinearLayout>??
- http://helloandroid.iteye.com/blog/1136748
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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