一、简介

运行结果

二、代码
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. StyleCop安装及配置

    How to modify the default new class template for C# in Visual Studio 2008 or 2010? 可参考博客: http://www ...

  2. 程序员带你学习安卓开发-XML文档的创建与解析

    这是程序员带你学习安卓开发系列教程.本文章致力于面向对象程序员可以快速学习开发安卓技术. 上篇文章:程序员带你学习安卓开发系列-Android文件存储 因知识连贯性推荐关注头条号:做全栈攻城狮.从头开 ...

  3. JQ判断按钮,复选框是否选中

    var oRdoValue=$("#Radio1").is (":checked")?"男":"女"; //获取单选框按 ...

  4. JQ 如何设置单选按钮问题

    <input type="radio" name="db_12" value="2" checked="checked/&g ...

  5. File类最基础知识

    package File; /** * 创建一个文件: * 判断是否存在,若存在,则创建,若不存在,则删除,最后输出文件是否存在. */ import java.io.File; import jav ...

  6. XMLHttpRequest cannot load的问题解决方法

      在chrome中可以用--allow-file-access-from-files 命令来解决这个问题.右键点击chrome的快捷方式选择属性.在目标一栏中添加--allow-file-acces ...

  7. iOS 查找字符串 相同 子字符串的位置 range

    问题:解决替换同一个字符串的多个相同的字符eg. xxx这个超级大土豪白送xxx一个!赶快来抢把! 将第一个xxx换成名字 将第二个xxx换成物品 两种办法    第二种办法更灵活一点 //第一种办法 ...

  8. js使用正则表达式去空格

    写成类的方法格式如下:(str.trim();) <script language="javascript"> String.prototype.trim=functi ...

  9. switch case实现两个数的算术运算

    方法一: package com.liaojianya.chapter1; import java.util.Scanner; public class SwitchDemo1 { public st ...

  10. Andriod 中常见错误

    1.Open quote is expected for attribute "android:name" associated with an element type &quo ...