- 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();
- }
- }
- }
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(); } } }
- <?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>
<?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>
- <?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/refer_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/refer_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/refer_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/refer_hasimage" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_toLeftOf= "@id/refer_timestamp" />
- <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" />
- <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" />
- </RelativeLayout>
- </LinearLayout>
- http://helloandroid.iteye.com/blog/1136767