ANDROID_MARS学习笔记_S04_008_用Listview、自定义adapter显示返回的微博数据

时间:2023-12-22 23:08:14

一、简介

ANDROID_MARS学习笔记_S04_008_用Listview、自定义adapter显示返回的微博数据

运行结果

ANDROID_MARS学习笔记_S04_008_用Listview、自定义adapter显示返回的微博数据

二、代码
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;
} }