ANDROID_MARS学习笔记_S04_008_用Listview自定义adapter显示返回的微博数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ANDROID_MARS学习笔记_S04_008_用Listview自定义adapter显示返回的微博数据相关的知识,希望对你有一定的参考价值。
一、简介
运行结果
二、代码
1.xml
(1)activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <Button android:id="@+id/btn_launch_oauth" 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="Launch OAuth Flow"/> 11 12 <Button 13 android:id="@+id/btn_sendWeiBo" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:text="发送一条微博消息" 17 /> 18 <Button 19 android:id="@+id/btn_getWeiBoList" 20 android:layout_width="fill_parent" 21 android:layout_height="wrap_content" 22 android:text="得到主页时间线数据" 23 /> 24 </LinearLayout>
(2)AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="org.marsdroid.oauth05" 4 android:versionCode="1" 5 android:versionName="1.0"> 6 <uses-sdk android:minSdkVersion="10" /> 7 8 <uses-permission android:name="android.permission.INTERNET" /> 9 <application android:icon="@drawable/icon" android:label="@string/app_name"> 10 <activity android:name=".MainActivity" android:label="@string/app_name"> 11 <intent-filter> 12 <action android:name="android.intent.action.MAIN" /> 13 <category android:name="android.intent.category.LAUNCHER" /> 14 </intent-filter> 15 </activity> 16 <activity android:name=".PrepareRequestTokenActivity" android:launchMode="singleTask"> 17 <intent-filter> 18 <action android:name="android.intent.action.VIEW" /> 19 <category android:name="android.intent.category.DEFAULT" /> 20 <category android:name="android.intent.category.BROWSABLE" /> 21 <data android:scheme="x-oauthflow" android:host="callback" /> 22 </intent-filter> 23 </activity> 24 <activity android:name=".BroadcastTimelineActivity"/> 25 </application> 26 </manifest>
(3)list.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <ListView 8 android:id="@android:id/list" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 ></ListView> 12 </LinearLayout>
(4)item.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:id="@+id/nameId" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 /> 12 <TextView 13 android:id="@+id/textId" 14 android:layout_height="wrap_content" 15 android:layout_width="fill_parent" 16 /> 17 </LinearLayout>
2.java
(1)MainActivity.java
1 import java.util.ArrayList; 2 import java.util.HashMap; 3 import java.util.List; 4 import java.util.Map; 5 6 import org.marsdroid.model.WeiBoList; 7 8 import oauth.signpost.OAuth; 9 import android.app.Activity; 10 import android.content.Intent; 11 import android.content.SharedPreferences; 12 import android.os.Bundle; 13 import android.preference.PreferenceManager; 14 import android.view.View; 15 import android.view.View.OnClickListener; 16 import android.widget.Button; 17 18 import com.google.gson.Gson; 19 20 public class MainActivity extends Activity { 21 22 final String TAG = getClass().getName(); 23 private SharedPreferences prefs; 24 /** Called when the activity is first created. */ 25 @Override 26 public void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.main); 29 30 prefs = PreferenceManager.getDefaultSharedPreferences(this); 31 Button launchOauth = (Button) findViewById(R.id.btn_launch_oauth); 32 Button sendWeiBoButton = (Button)findViewById(R.id.btn_sendWeiBo); 33 Button getWeiBoListButton = (Button)findViewById(R.id.btn_getWeiBoList); 34 35 sendWeiBoButton.setOnClickListener(new OnClickListener() { 36 37 @Override 38 public void onClick(View v) { 39 //收集需要向腾讯微博服务器端发送的数据 40 Map<String,String> map = new HashMap<String,String>(); 41 map.put("content", "test"); 42 map.put("clientip", "127.0.0.1"); 43 map.put("format", "json"); 44 //URL编码 45 List<String> decodeNames = new ArrayList<String>(); 46 decodeNames.add("oauth_signature"); 47 //生成WeiboClient对象需要四个参数:Consumer_key,Consumer_key_secret,Oauth_tokent,OAuth_token_secret 48 String OAuth_token = prefs.getString(OAuth.OAUTH_TOKEN, ""); 49 String OAuth_token_secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, ""); 50 WeiBoClient weiBoClient = new WeiBoClient(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET, OAuth_token, OAuth_token_secret); 51 weiBoClient.doPost("http://open.t.qq.com/api/t/add",map,decodeNames); 52 } 53 }); 54 55 getWeiBoListButton.setOnClickListener(new OnClickListener(){ 56 57 @Override 58 public void onClick(View v) { 59 60 Intent intent = new Intent(MainActivity.this, BroadcastTimelineActivity.class); 61 startActivity(intent); 62 } 63 64 }); 65 66 launchOauth.setOnClickListener(new OnClickListener() { 67 public void onClick(View v) { 68 startActivity(new Intent().setClass(v.getContext(), PrepareRequestTokenActivity.class)); 69 } 70 }); 71 } 72 73 }
(2)BroadcastTimelineActivity.java
1 import java.util.HashMap; 2 import java.util.Map; 3 4 import oauth.signpost.OAuth; 5 6 import org.marsdroid.model.WeiBoList; 7 8 import android.app.ListActivity; 9 import android.content.SharedPreferences; 10 import android.os.Bundle; 11 import android.preference.PreferenceManager; 12 import android.widget.ListView; 13 14 import com.google.gson.Gson; 15 16 public class BroadcastTimelineActivity extends ListActivity{ 17 18 private SharedPreferences prefs; 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.list); 23 24 prefs = PreferenceManager.getDefaultSharedPreferences(this); 25 Map<String,String> keyValues = new HashMap<String,String>(); 26 keyValues.put("format", "json"); 27 keyValues.put("pageflag", "0"); 28 keyValues.put("pagetime", "0"); 29 keyValues.put("reqnum", "20"); 30 String OAuth_token = prefs.getString(OAuth.OAUTH_TOKEN, ""); 31 String OAuth_token_secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, ""); 32 WeiBoClient weiBoClient = new WeiBoClient(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET, OAuth_token, OAuth_token_secret); 33 String result = weiBoClient.doGet(Constants.WeiBoApi.HOME_TIMELINE, keyValues); 34 Gson gson = new Gson(); 35 WeiBoList weiBoList = gson.fromJson(result, WeiBoList.class); 36 WeiBoAdapter weiBoAdapter = new WeiBoAdapter(weiBoList, this); 37 ListView listView = getListView(); 38 listView.setAdapter(weiBoAdapter); 39 } 40 41 }
(3)WeiBoAdapter.java
1 import java.util.HashMap; 2 import java.util.List; 3 import java.util.Map; 4 5 import org.marsdroid.model.WeiBoData; 6 import org.marsdroid.model.WeiBoList; 7 8 import android.content.Context; 9 import android.view.LayoutInflater; 10 import android.view.View; 11 import android.view.ViewGroup; 12 import android.widget.BaseAdapter; 13 import android.widget.TextView; 14 15 public class WeiBoAdapter extends BaseAdapter{ 16 //WeiBoList对象当中的数据,代表了服务器端所返回的所有数据 17 private WeiBoList weiBoList; 18 //info当中存储了一次所取回的所有微博数据 19 private List<WeiBoData> info = null; 20 //View对象的缓存,不用每次要数据都new 一个view对象 21 private Map<Integer,View> rowViews = new HashMap<Integer,View>(); 22 private Context context = null; 23 24 public WeiBoAdapter(WeiBoList weiBoList,Context context){ 25 this.weiBoList = weiBoList; 26 info = weiBoList.getData().getInfo(); 27 this.context = context; 28 } 29 //返回当中的Adapter当中,共包含多少个item 30 @Override 31 public int getCount() { 32 // TODO Auto-generated method stub 33 return info.size(); 34 } 35 //根据位置,得到相应的item对象 36 @Override 37 public Object getItem(int position) { 38 // TODO Auto-generated method stub 39 return info.get(position); 40 } 41 //根据位置,得到相应的item对象的ID 42 @Override 43 public long getItemId(int position) { 44 // 这例子对id没什么要求 45 return position; 46 } 47 48 //ListView通过调用getView()方法,得到相应的View对象,并将其显示在Activity当中 49 @Override 50 public View getView(int position, View convertView, ViewGroup parent) { 51 //convertView可以提高view的性能,涉及到数据结构的问题 52 View rowView = rowViews.get(position); 53 if(rowView == null){ 54 //生成一个LayoutInflater对象,填充器 55 LayoutInflater layoutInflater = LayoutInflater.from(context); 56 //调用LayoutInflater对象的inflate方法,可以生成一个View对象,null是父控件 57 rowView = layoutInflater.inflate(R.layout.item, null); 58 //得到该View当中的两个控件 59 TextView nameView = (TextView)rowView.findViewById(R.id.nameId); 60 TextView textView = (TextView)rowView.findViewById(R.id.textId); 61 //调用getItem()方法,得到对应位置的weiBoData对象 62 WeiBoData weiBoData = (WeiBoData)getItem(position); 63 nameView.setText(weiBoData.getName()); 64 textView.setText(weiBoData.getText()); 65 rowViews.put(position, rowView);//缓存取过的数据 66 } 67 return rowView; 68 } 69 70 }
以上是关于ANDROID_MARS学习笔记_S04_008_用Listview自定义adapter显示返回的微博数据的主要内容,如果未能解决你的问题,请参考以下文章
ANDROID_MARS学习笔记_S02_008_ANIMATION第二种使用方式:xml
ANDROID_MARS学习笔记_S04_001_OAuth简介
ANDROID_MARS学习笔记_S04_002_用AsyncTask实现异步操作
ANDROID_MARS学习笔记_S04_007_从服务器获取微博数据时间线
ANDROID_MARS学习笔记_S04_004_用HTTPCLENT发带参数的get和post请求
ANDROID_MARS学习笔记_S04_006_用获取access_token,access_token_secrect