PullToRefresh使用详解实现异步加载的下拉刷新列表文档格式.docx

上传人:b****6 文档编号:20234484 上传时间:2023-01-21 格式:DOCX 页数:19 大小:80.77KB
下载 相关 举报
PullToRefresh使用详解实现异步加载的下拉刷新列表文档格式.docx_第1页
第1页 / 共19页
PullToRefresh使用详解实现异步加载的下拉刷新列表文档格式.docx_第2页
第2页 / 共19页
PullToRefresh使用详解实现异步加载的下拉刷新列表文档格式.docx_第3页
第3页 / 共19页
PullToRefresh使用详解实现异步加载的下拉刷新列表文档格式.docx_第4页
第4页 / 共19页
PullToRefresh使用详解实现异步加载的下拉刷新列表文档格式.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

PullToRefresh使用详解实现异步加载的下拉刷新列表文档格式.docx

《PullToRefresh使用详解实现异步加载的下拉刷新列表文档格式.docx》由会员分享,可在线阅读,更多相关《PullToRefresh使用详解实现异步加载的下拉刷新列表文档格式.docx(19页珍藏版)》请在冰豆网上搜索。

PullToRefresh使用详解实现异步加载的下拉刷新列表文档格式.docx

org.apache.http.impl.client.DefaultHttpClient;

17.import 

org.apache.http.message.BasicNameValuePair;

18.import 

org.apache.http.protocol.HTTP;

19.import 

org.json.JSONArray;

20. 

21. 

22.import 

com.handmark.pulltorefresh.library.PullToRefreshBase;

23.import 

com.handmark.pulltorefresh.library.PullToRefreshListView;

24.import 

com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;

25.import 

com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;

26. 

27.import 

android.os.AsyncTask;

28.import 

android.os.Bundle;

29.import 

android.text.format.DateUtils;

30.import 

android.util.Log;

31.import 

android.widget.ListView;

32.import 

android.app.ListActivity;

33. 

34.public 

class 

MainActivity 

extends 

ListActivity{ 

35. 

36. 

private 

String 

serverIP="

http:

//222.195.151.19"

;

37. 

List<

ImageAndText>

mData;

38. 

PullToRefreshListView 

mPullRefreshListView;

39. 

ImageAndTextListAdapter 

adapter=null;

40. 

@Override 

41. 

public 

void 

onCreate(Bundle 

savedInstanceState) 

42. 

super.onCreate(savedInstanceState);

43. 

setContentView(R.layout.activity_main);

44. 

45. 

mPullRefreshListView 

(PullToRefreshListView) 

findViewById(R.id.pull_refresh_list);

46. 

47. 

//设定下拉监听函数 

48. 

mPullRefreshListView.setOnRefreshListener(new 

OnRefreshListener<

ListView>

() 

49. 

50. 

onRefresh(PullToRefreshBase<

refreshView) 

51. 

label 

DateUtils.formatDateTime(getApplicationContext(), 

System.currentTimeMillis(), 

52. 

DateUtils.FORMAT_SHOW_TIME 

DateUtils.FORMAT_SHOW_DATE 

DateUtils.FORMAT_ABBREV_ALL);

53. 

54. 

// 

Update 

the 

LastUpdatedLabel 

55. 

refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

56. 

57. 

Log.d("

msg"

"

this=="

+this);

58. 

Do 

work 

to 

refresh 

list 

here. 

59. 

new 

GetDataTask().execute();

60. 

61. 

62. 

});

63. 

64. 

mPullRefreshListView.setMode(Mode.PULL_FROM_END);

设置底部下拉刷新模式 

65. 

//传参生成适配器 

66. 

mData 

getData();

67. 

ListView 

actualListView 

mPullRefreshListView.getRefreshableView();

68. 

adapter 

ImageAndTextListAdapter(this,mData,actualListView);

69. 

70. 

设置适配器 

71. 

actualListView.setAdapter(adapter);

72. 

73. 

74. 

getData() 

75. 

//创建默认的httpClient实例. 

76. 

DefaultHttpClient 

httpclient 

DefaultHttpClient();

77. 

HttpResponse 

response 

null;

78. 

HttpEntity 

entity 

79. 

80. 

StringBuilder 

builder 

StringBuilder();

81. 

JSONArray 

jsonArray 

82. 

83. 

ArrayList<

();

84. 

85. 

try{ 

86. 

创建httpost.访问本地服务器网址 

87. 

HttpPost 

httpost 

HttpPost(serverIP+"

/try_an_server/index.php"

);

88. 

89. 

//构造POST方法的{name:

value} 

参数对 

90. 

List 

<

NameValuePair>

vps 

ArrayList 

91. 

//将参数传入post方法中 

92. 

vps.add(new 

BasicNameValuePair("

action"

 

"

insert"

));

93. 

name"

进去了"

94. 

95. 

httpost.setEntity(new 

UrlEncodedFormEntity(vps, 

HTTP.UTF_8));

96. 

httpclient.execute(httpost);

//执行 

97. 

98. 

if 

(response.getEntity() 

!

null) 

99. 

//如果服务器端JSON没写对,这句是会出异常,是执行不过去的 

100. 

BufferedReader 

reader 

BufferedReader(new 

InputStreamReader(response.getEntity().getContent()));

101. 

reader.readLine();

102. 

for 

(;

reader.readLine()) 

103. 

builder.append(s);

104. 

105. 

Log.i("

builder.toString 

builder.toString());

106. 

107. 

JSONArray(builder.toString());

108. 

(int 

0;

jsonArray.length();

i++) 

109. 

if(jsonArray.getJSONObject(i).getInt("

id"

)==1){ 

110. 

name=jsonArray.getJSONObject(i).getString("

111. 

info=jsonArray.getJSONObject(i).getString("

info"

112. 

PicName=jsonArray.getJSONObject(i).getString("

photo"

113. 

picURL=serverIP+"

/try_an_server/"

+PicName+"

.jpg"

114. 

115. 

ImageAndText 

item=new 

ImageAndText(picURL,name,info);

116. 

list.add(item);

117. 

118. 

119. 

120. 

catch 

(Exception 

e) 

121. 

e.printStackTrace();

122. 

finally 

123. 

try 

124. 

(entity 

125. 

126. 

httpclient.getConnectionManager().shutdown();

//关闭连接 

127. 

//这两种释放连接的方法都可以 

128. 

129. 

130. 

TODO 

Auto-generated 

block 

131. 

132. 

133. 

134. 

135. 

return 

list;

136. 

137. 

138. 

139. 

140. 

141. 

GetDataTask 

AsyncTask<

Void, 

142. 

143. 

//后台处理部分 

144. 

145. 

protected 

doInBackground(Void... 

params) 

146. 

Simulates 

background 

job. 

147. 

item 

148. 

149. 

ImageAndText(serverIP+"

/try_an_server/xizang.jpg"

sss"

ssss"

150. 

151. 

TODO:

handle 

exception 

152. 

setTitle("

map出错了"

153. 

154. 

155. 

item;

156. 

157. 

158. 

//这里是对刷新的响应,可以利用addFirst()和addLast()函数将新加的内容加到LISTView中 

159. 

//根据AsyncTask的原理,onPostExecute里的result的值就是doInBackground()的返回值 

160. 

161. 

onPostExecute(ImageAndText 

result) 

162. 

//在头部增加新添内容 

163. 

164. 

165. 

mData.add(result);

166. 

167. 

//通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合 

168. 

adapter.notifyDataSetChanged();

169. 

adapter.loadImage();

170. 

Call 

onRefreshComplete 

when 

has 

been 

refreshed. 

171. 

mPullRefreshListView.onRefreshComplete();

172. 

173. 

174. 

setTitle(e.getMessage());

175. 

176. 

177. 

super.onPostExecute(result);

178. 

179. 

180. 

181. 

182.} 

packagecom.example.try_simpleadapter_new;

/**

*完成与服务器通信的下拉刷新

*@authorharvic

*/

importjava.io.BufferedReader;

importjava.io.InputStreamReader;

importjava.util.ArrayList;

importjava.util.List;

importorg.apache.http.HttpEntity;

importorg.apache.http.HttpResponse;

importorg.apache.http.NameValuePair;

importorg.apache.http.client.entity.UrlEncodedFormEntity;

importorg.apache.http.client.methods.HttpPost;

importorg.apache.http.impl.client.DefaultHttpClient;

importorg.apache.http.message.BasicNameValuePair;

importorg.apache.http.protocol.HTTP;

importorg.json.JSONArray;

importcom.handmark.pulltorefresh.library.PullToRefreshBase;

importcom.handmark.pulltorefresh.library.PullToRefreshListView;

importcom.handmark.pulltorefresh.library.PullToRefreshBase.Mode;

importcom.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;

importandroid.os.AsyncTask;

importandroid.os.Bundle;

importandroid.text.format.Dat

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 自我管理与提升

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1