一、简介

运行结果

二、代码
1.xml
(1)activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/btn_launch_oauth"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Launch OAuth Flow"/> <Button
android:id="@+id/btn_sendWeiBo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发送一条微博消息"
/>
<Button
android:id="@+id/btn_getWeiBoList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="得到主页时间线数据"
/>
</LinearLayout>

(2)AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.marsdroid.oauth05"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".PrepareRequestTokenActivity" android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="x-oauthflow" android:host="callback" />
</intent-filter>
</activity>
<activity android:name=".BroadcastTimelineActivity"/>
</application>
</manifest>

(3)list.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>

(4)item.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/nameId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textId"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
</LinearLayout>

2.java
(1)MainActivity.java

 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.marsdroid.model.WeiBoList; import oauth.signpost.OAuth;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; import com.google.gson.Gson; public class MainActivity extends Activity { final String TAG = getClass().getName();
private SharedPreferences prefs;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); prefs = PreferenceManager.getDefaultSharedPreferences(this);
Button launchOauth = (Button) findViewById(R.id.btn_launch_oauth);
Button sendWeiBoButton = (Button)findViewById(R.id.btn_sendWeiBo);
Button getWeiBoListButton = (Button)findViewById(R.id.btn_getWeiBoList); sendWeiBoButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
//收集需要向腾讯微博服务器端发送的数据
Map<String,String> map = new HashMap<String,String>();
map.put("content", "test");
map.put("clientip", "127.0.0.1");
map.put("format", "json");
//URL编码
List<String> decodeNames = new ArrayList<String>();
decodeNames.add("oauth_signature");
//生成WeiboClient对象需要四个参数:Consumer_key,Consumer_key_secret,Oauth_tokent,OAuth_token_secret
String OAuth_token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String OAuth_token_secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
WeiBoClient weiBoClient = new WeiBoClient(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET, OAuth_token, OAuth_token_secret);
weiBoClient.doPost("http://open.t.qq.com/api/t/add",map,decodeNames);
}
}); getWeiBoListButton.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) { Intent intent = new Intent(MainActivity.this, BroadcastTimelineActivity.class);
startActivity(intent);
} }); launchOauth.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent().setClass(v.getContext(), PrepareRequestTokenActivity.class));
}
});
} }

(2)BroadcastTimelineActivity.java

 import java.util.HashMap;
import java.util.Map; import oauth.signpost.OAuth; import org.marsdroid.model.WeiBoList; import android.app.ListActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.ListView; import com.google.gson.Gson; public class BroadcastTimelineActivity extends ListActivity{ private SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list); prefs = PreferenceManager.getDefaultSharedPreferences(this);
Map<String,String> keyValues = new HashMap<String,String>();
keyValues.put("format", "json");
keyValues.put("pageflag", "0");
keyValues.put("pagetime", "0");
keyValues.put("reqnum", "20");
String OAuth_token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String OAuth_token_secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
WeiBoClient weiBoClient = new WeiBoClient(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET, OAuth_token, OAuth_token_secret);
String result = weiBoClient.doGet(Constants.WeiBoApi.HOME_TIMELINE, keyValues);
Gson gson = new Gson();
WeiBoList weiBoList = gson.fromJson(result, WeiBoList.class);
WeiBoAdapter weiBoAdapter = new WeiBoAdapter(weiBoList, this);
ListView listView = getListView();
listView.setAdapter(weiBoAdapter);
} }

(3)WeiBoAdapter.java

 import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.marsdroid.model.WeiBoData;
import org.marsdroid.model.WeiBoList; import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView; public class WeiBoAdapter extends BaseAdapter{
//WeiBoList对象当中的数据,代表了服务器端所返回的所有数据
private WeiBoList weiBoList;
//info当中存储了一次所取回的所有微博数据
private List<WeiBoData> info = null;
//View对象的缓存,不用每次要数据都new 一个view对象
private Map<Integer,View> rowViews = new HashMap<Integer,View>();
private Context context = null; public WeiBoAdapter(WeiBoList weiBoList,Context context){
this.weiBoList = weiBoList;
info = weiBoList.getData().getInfo();
this.context = context;
}
//返回当中的Adapter当中,共包含多少个item
@Override
public int getCount() {
// TODO Auto-generated method stub
return info.size();
}
//根据位置,得到相应的item对象
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return info.get(position);
}
//根据位置,得到相应的item对象的ID
@Override
public long getItemId(int position) {
// 这例子对id没什么要求
return position;
} //ListView通过调用getView()方法,得到相应的View对象,并将其显示在Activity当中
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//convertView可以提高view的性能,涉及到数据结构的问题
View rowView = rowViews.get(position);
if(rowView == null){
//生成一个LayoutInflater对象,填充器
LayoutInflater layoutInflater = LayoutInflater.from(context);
//调用LayoutInflater对象的inflate方法,可以生成一个View对象,null是父控件
rowView = layoutInflater.inflate(R.layout.item, null);
//得到该View当中的两个控件
TextView nameView = (TextView)rowView.findViewById(R.id.nameId);
TextView textView = (TextView)rowView.findViewById(R.id.textId);
//调用getItem()方法,得到对应位置的weiBoData对象
WeiBoData weiBoData = (WeiBoData)getItem(position);
nameView.setText(weiBoData.getName());
textView.setText(weiBoData.getText());
rowViews.put(position, rowView);//缓存取过的数据
}
return rowView;
} }

ANDROID_MARS学习笔记_S04_008_用Listview、自定义adapter显示返回的微博数据的更多相关文章

  1. ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER004_同步显示歌词

    一.流程分析 1.点击播放按钮,会根据lrc名调用LrcProcessor的process()分析歌词文件,得到时间队列和歌词队列 2.new一个hander,把时间队列和歌词队列传给自定义的线程类U ...

  2. [Android] Android RecycleView和ListView 自定义Adapter封装类

    在网上查看了很多对应 Android RecycleView和ListView 自定义Adapter封装类 的文章,主要存在几个问题: 一).网上代码一大抄,复制来复制去,大部分都运行不起来,或者 格 ...

  3. Flutter学习笔记(41)--自定义Dialog实现版本更新弹窗

    如需转载,请注明出处:Flutter学习笔记(41)--自定义Dialog实现版本更新弹窗 功能点: 1.更新弹窗UI 2.强更与非强更且别控制 3.屏蔽物理返回键(因为强更的时候点击返回键,弹窗会消 ...

  4. Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据

    Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...

  5. 2.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:

    转自:https://www.cnblogs.com/ssslinppp/p/4528892.html 个人认为,使用@ResponseBody方式来实现json数据的返回比较方便,推荐使用. 摘要 ...

  6. CockroachDB学习笔记——[译]CockroachDB中的SQL:映射表中数据到键值存储

    CockroachDB学习笔记--[译]CockroachDB中的SQL:映射表中数据到键值存储 原文标题:SQL in CockroachDB: Mapping Table Data to Key- ...

  7. Android ListView 自定义 Adapter

    自定义Adapter类 public class ListViewAdapter extends BaseAdapter { private static final String TAG = Mai ...

  8. Vue学习笔记十三:Vue+Bootstrap+vue-resource从接口获取数据库数据

    目录 前言 SpringBoot提供后端接口 Entity类 JPA操作接口 配置文件 数据库表自动映射,添加数据 写提供数据的接口 跨域问题 前端修改 效果图 待续 前言 Vue学习笔记九的列表案例 ...

  9. Bash脚本编程学习笔记04:测试命令test、状态返回值、位置参数和特殊变量

    我自己接触Linux主要是大学学习的Turbolinux --> 根据<鸟哥的Linux私房菜:基础篇>(第三版) --> 马哥的就业班课程.给我的感觉是这些课程对于bash的 ...

随机推荐

  1. Java动态绑定

    1. 动态绑定 将一个方法调用同一个方法主体关联起来被称作绑定. 在运行时根据对象的类型进行绑定,叫做后期绑定或运行时绑定.Java中除了static方法和final 例如,下面定义了一个Shape类 ...

  2. zzzzw_在线考试系统③完结篇

    昨天填完原本打算写有关“学生考试部门”的总结,但是因为时间来不及,所以推迟到今天来写. 至于最后的:“老师登录”部门就没什么好说的了,只要会了“管理员部分”和“学生考试部分”的书写,剩下就只是耐心的一 ...

  3. java Spring 生命周期

    1.初始化回调 <bean name="userService" class="com.sun.service.UserService" init-met ...

  4. ASP.NET Identity 用户注册相关设定

    此部分可以在 Web项目中的App_Start目录下的 IdentityConfig.cs 文件进行设置. 1.配置密码的验证逻辑 manager.PasswordValidator = new Pa ...

  5. ACM——进制转换

    http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1012 进制转换 时间限制(普通/Jav ...

  6. iOS开发——常用字符串string相关方法和处理

    (持续更新中……) 1,四舍五入 2,剔除字符 3,拼接字符 4,字符个数和长度 5,字符串的比较 6,字符串的范围 7,字符串转Number类型

  7. OC - 2.OC基础知识介绍

    一.基础语法 1> OC语言和C语言 C语言是面向过程的语言,OC语言是面向对象的语言 OC语言继承了C语言,并增加了面向对象的思想 以下内容只介绍OC语言与C语言的不同之处 2> 关键字 ...

  8. Java中printStackTrace()、toString()、getMessage()的区别

    一.三者之间的关系图: 二.演示 1.printStackTrace()演示: public class Test {     public int div(int a, int b)     {   ...

  9. Sql server 浅谈用户定义表类型

    1.1 简介 SQL Server 中,用户定义表类型是指用户所定义的表示表结构定义的类型.您可以使用用户定义表类型为存储过程或函数声明表值参数,或者声明您要在批处理中或在存储过程或函数的主体中使用的 ...

  10. awk 查找文件长度 删除

    #在某个目录下,由于有些是缓存文件,它们的共同点就是长度大于3, 找到它们,然后用rm 命令删除#ls abc.pyabcd.py.... #ls | awk 'length($1) > 3 { ...