Android腾讯微薄客户端开发十一:博主的粉丝_第1张图片

Java代码
public class FansActivity extends ListActivity implements OnItemClickListener{

private DataHelper dataHelper;
private UserInfo user;
private MyWeiboSync weibo;
private Handler handler;
private AsyncImageLoader asyncImageLoader;
private FansThread thread;
private ListView listView;
private ProgressDialog progressDialog;
private JSONArray array;
private FansAdapter 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.fans);
setUpViews();//设置view
setUpListeners();//设置listenter
asyncImageLoader = new AsyncImageLoader();
dataHelper = new DataHelper(FansActivity.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(FansActivity.this);// 生成一个进度条
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setTitle("请稍等");
progressDialog.setMessage("正在读取数据中!");

handler = new FansHandler();
thread = new FansThread();
thread.start();//开启一个线程获取数据
progressDialog.show();
}

private void setUpViews(){
listView = getListView();
top_panel = (View)findViewById(R.id.fans_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 FansThread extends Thread {
@Override
public void run() {
String jsonStr = weibo.getFans(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 FansHandler extends Handler {
@Override
public void handleMessage(Message msg){
adapter = new FansAdapter(FansActivity.this, array);
listView.setAdapter(adapter);
progressDialog.dismiss();// 关闭进度条
}
}

class FansAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private JSONArray array;

public FansAdapter(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();
FansViewHolder viewHolder = new FansViewHolder();
JSONObject data = (JSONObject)array.opt(position);
convertView = inflater.inflate(R.layout.fans_list_item, null);

viewHolder.fans_headicon = (ImageView) convertView.findViewById(R.id.fans_headicon);
viewHolder.fans_nick = (TextView) convertView.findViewById(R.id.fans_nick);
viewHolder.fans_name = (TextView) convertView.findViewById(R.id.fans_name);

Drawable cachedImage = null;
if(data!=null){
try {
convertView.setTag(data.get("name"));
viewHolder.fans_nick.setText(data.getString("nick"));
viewHolder.fans_name.setText("@"+data.getString("name"));
//异步加载图片
cachedImage = asyncImageLoader.loadDrawable(data.getString("head")+"/100",viewHolder.fans_headicon, new ImageCallback(){
@Override
public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl) {
imageView.setImageDrawable(imageDrawable);
}
});
if (cachedImage == null) {
viewHolder.fans_headicon.setImageResource(R.drawable.icon);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return convertView;
}
}

static class FansViewHolder {
private ImageView fans_headicon;
private TextView fans_nick;
private TextView fans_name;
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3){
Intent intent = new Intent(FansActivity.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);//跳转到用户信息界面
}
}


public class FansActivity extends ListActivity implements OnItemClickListener{


private DataHelper dataHelper;
private UserInfo user;
private MyWeiboSync weibo;
private Handler handler;
private AsyncImageLoader asyncImageLoader;
private FansThread thread;
private ListView listView;
private ProgressDialog progressDialog;
private JSONArray array;
private FansAdapter 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.fans);
setUpViews();//设置view
setUpListeners();//设置listenter
asyncImageLoader = new AsyncImageLoader();
dataHelper = new DataHelper(FansActivity.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(FansActivity.this);// 生成一个进度条
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setTitle("请稍等");
progressDialog.setMessage("正在读取数据中!");

handler = new FansHandler();
thread = new FansThread();
thread.start();//开启一个线程获取数据
progressDialog.show();
}

private void setUpViews(){
listView = getListView();
top_panel = (View)findViewById(R.id.fans_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 FansThread extends Thread {
@Override
public void run() {
String jsonStr = weibo.getFans(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 FansHandler extends Handler {
@Override
public void handleMessage(Message msg){
adapter = new FansAdapter(FansActivity.this, array);
listView.setAdapter(adapter);
progressDialog.dismiss();// 关闭进度条
}
}

class FansAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private JSONArray array;

public FansAdapter(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();
FansViewHolder viewHolder = new FansViewHolder();
JSONObject data = (JSONObject)array.opt(position);
convertView = inflater.inflate(R.layout.fans_list_item, null);

viewHolder.fans_headicon = (ImageView) convertView.findViewById(R.id.fans_headicon);
viewHolder.fans_nick = (TextView) convertView.findViewById(R.id.fans_nick);
viewHolder.fans_name = (TextView) convertView.findViewById(R.id.fans_name);

Drawable cachedImage = null;
if(data!=null){
try {
convertView.setTag(data.get("name"));
viewHolder.fans_nick.setText(data.getString("nick"));
viewHolder.fans_name.setText("@"+data.getString("name"));
//异步加载图片
cachedImage = asyncImageLoader.loadDrawable(data.getString("head")+"/100",viewHolder.fans_headicon, new ImageCallback(){
@Override
public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl) {
imageView.setImageDrawable(imageDrawable);
}
});
if (cachedImage == null) {
viewHolder.fans_headicon.setImageResource(R.drawable.icon);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return convertView;
}
}

static class FansViewHolder {
private ImageView fans_headicon;
private TextView fans_nick;
private TextView fans_name;
}


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3){
Intent intent = new Intent(FansActivity.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代码
<?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/fans_top" layout="@layout/top_panel" android:layout_alignParentTop="true"/>
<ListView android:id="@id/android:list" android:layout_below="@id/fans_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/fans_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/fans_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/fans_top" layout="@layout/top_panel" android:layout_alignParentTop="true"/>
<ListView android:id="@id/android:list" android:layout_below="@id/fans_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/fans_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/fans_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代码
<?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/fans_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/fans_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/fans_name" android:layout_marginLeft="6.0dip" android:layout_below="@id/fans_nick" android:textColor="#ff000000" android:layout_width="wrap_content" android:layout_height="32.0dip" android:textSize="8.0sp" android:layout_alignParentLeft="true"/>
</RelativeLayout>
</LinearLayout>


<?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/fans_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/fans_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/fans_name" android:layout_marginLeft="6.0dip" android:layout_below="@id/fans_nick" android:textColor="#ff000000" android:layout_width="wrap_content" android:layout_height="32.0dip" android:textSize="8.0sp" android:layout_alignParentLeft="true"/>
</RelativeLayout>
</LinearLayout>

更多相关文章

  1. Android 关于 如何使用外界导入的数据库文件
  2. Android连载之:第三章第二节:Android用户界面
  3. Android 打造任意层级树形控件 考验你的数据结构和设计
  4. Android - 利用Android studio + Android Killer工具在手机未ROO
  5. android 仿ios数字密码解锁界面
  6. Android 如何在自定义界面上启用输入法 (How to enable inputmet
  7. Android Studio-手把手教你做启动界面开发
  8. 曝Android机冷冻后变"傻" 加密数据随意访问
  9. Android应用开发笔记(13): Android移动应用界面的模板化设计

随机推荐

  1. sql处理数据库锁的存储过程分享
  2. SQLSERVER 本地查询更新操作远程数据库的
  3. sql 查询记录数结果集某个区间内记录
  4. sql server 表结构修改方法
  5. MSSQL数据库排序规则如何更改
  6. mssql2005,2008导出数据字典实现方法
  7. SQL Server正则表达式 替换函数应用详解
  8. SQL Server 数据库实用SQL语句
  9. SQL 中having 和where的区别分析
  10. SQL根据指定分隔符分解字符串实现步骤