Android listview分页显示——data from php
来源:互联网 发布:淘宝无线店铺收藏链接 编辑:程序博客网 时间:2024/05/16 13:49
此demo的简单流程是:发送请求数据到php,然后php查询数据库返回json,android接收到json做动态加载更多数据和刷新listview的相关处理。
当滚动条滚动到低端时,不管有没有数据都没有做隐藏BUTTON的操作,这只是一个listview分页以及刷新的demo
php代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<div><?php
/**
* 返回JSON给客户端(安卓)
*/
//建立数据库连接
$con = mysql_connect(
"localhost"
,
"root"
,
"root"
);
if
(! $con) {
die (
'Could not connect: '
. mysql_error () );
}
//选择查询的数据库
mysql_select_db (
"test"
, $con );
//设置字符集为UTF-8
mysql_query (
"set names utf8"
);
$beginNum=$_REQUEST[
"beginNum"
];
$query =
"select id,name,sex,password from userinfo order by id desc limit $beginNum,20"
;
//执行SQL语句
$result = mysql_query ( $query );
//循环 将查询的数据存入数组
while
( $row = mysql_fetch_assoc ( $result ) ) {
$response [] = $row;
}
//使用Foreach遍历数组 同时使用urlencode处理 含有中文的字段
foreach ( $response as $key => $value ) {
$newValue[$key] = $value;
//$newData [$key] ['name'] = urlencode ( $value ['name'] );
//$newData [$key] ['sex'] = urlencode ( $value ['sex'] );
}
echo json_encode ( $newValue );
mysql_close ( $con );
?></div>
Android端代码:
布局文件:
remind_list_item.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<div><?xml version=
"1.0"
encoding=
"utf-8"
?>
<!-- 收件箱列表文字信息 -->
<RelativeLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:id=
"@+id/RelativeLayout02"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:background=
"#ffffff"
android:paddingBottom=
"4dip"
android:paddingLeft=
"12dip"
android:paddingRight=
"12dip"
><TextView
android:id=
"@+id/name_textView"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:background=
"#ffffff"
android:paddingRight=
"10dip"
android:text=
""
android:textColor=
"#000000"
android:textSize=
"12dip"
/>
<TextView
android:id=
"@+id/content_textView"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:layout_below=
"@+id/name_textView"
android:background=
"#ffffff"
android:paddingTop=
"3dip"
android:textColor=
"#000000"
android:textSize=
"14dip"
/>
<TextView
android:id=
"@+id/time_textView"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:layout_below=
"@+id/content_textView"
android:background=
"#ffffff"
android:paddingTop=
"3dip"
android:textColor=
"#000000"
android:textSize=
"12dip"
/>
</RelativeLayout>
</div>
load_more.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<div><?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:id=
"@+id/loading_line"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
android:gravity=
"center"
android:orientation=
"vertical"
><Button
android:id=
"@+id/viewmorebutton"
android:layout_width=
"fill_parent"
android:layout_height=
"38dip"
android:text=
"查看更多"
/>
<LinearLayout
android:id=
"@+id/linearlayout"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:gravity=
"center"
android:orientation=
"horizontal"
>
<ProgressBar
style=
"?android:attr/progressBarStyleSmall"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
/>
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"数据加载中..."
/>
</LinearLayout>
</LinearLayout>
</div>
remind_listview.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<!-- 收件箱列表 -->
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
android:background=
"#f0f0f0"
android:orientation=
"vertical"
>
<RelativeLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:id=
"@+id/layout_topmenu"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:layout_alignParentTop=
"true"
>
<include layout=
"@layout/top"
/>
</RelativeLayout>
<ListView
android:id=
"@+id/remind_ListView"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:cacheColorHint=
"#00000000"
>
</ListView>
</LinearLayout>
Java代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* 事件列表
*
* @author 技术作坊
* @version 1.0
*/
public
class
RemindListViewActivity
extends
Activity {
private
RemindListAdapter listItemAdapter =
new
RemindListAdapter();
// 适配器
private
String jsonResult;
// 从PHP返回的JSON
private
List<JSONObject> jsonData;
// JSON集合
private
ImageView refreshImageView;
// 刷新
private
ProgressBar progressBar;
// 圆形进度
private
ListView listView;
// 列表
private
View footView;
// 底部布局
private
Button viewMoreButton;
// 底部查看更多按钮
LinearLayout footerProgressBarLayout;
// 底部圆形进度
private
Handler handler;
private
GetDetailThread thread;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
this
.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.remind_listview);
listView = (ListView) findViewById(R.id.remind_ListView);
refreshImageView = (ImageView) findViewById(R.id.refresh_ImageView);
progressBar = (ProgressBar) findViewById(R.id.pb);
// 向列表添加一个页脚
LayoutInflater inflater = LayoutInflater.from(
this
);
footView = inflater.inflate(R.layout.load_more,
null
);
viewMoreButton = (Button) footView.findViewById(R.id.viewmorebutton);
footerProgressBarLayout = (LinearLayout) footView
.findViewById(R.id.linearlayout);
listView.addFooterView(footView);
/******当submintData()返回true时获取数据并显示查看更多数据的按钮,否则隐藏并提示程序错误******/
if
(submintData(
0
) ==
true
) {
progressBar.setVisibility(View.GONE);
footerProgressBarLayout.setVisibility(View.GONE);
handler =
new
DealHandler();
thread =
new
GetDetailThread();
thread.start();
// 开启一个线程获取数据
// refreshImageView.setOnClickListener(new
// refreshImageViewClick());// 刷新
viewMoreButton.setOnClickListener(
new
ViewMoreClickListener());
// 查看更多
}
else
{
handler =
new
DealHandler();
progressBar.setVisibility(View.GONE);
footerProgressBarLayout.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),
"程序错误"
, Toast.LENGTH_SHORT)
.show();
}
refreshImageView.setOnClickListener(
new
refreshImageViewClick());
// 刷新
}
/**
* 查看更多
*
* @author Guozhilong
*
*/
class
ViewMoreClickListener
implements
OnClickListener {
@Override
public
void
onClick(View v) {
footerProgressBarLayout.setVisibility(View.VISIBLE);
// 显示
viewMoreButton.setVisibility(View.GONE);
// 隐藏
handler.postDelayed(
new
Runnable() {
@Override
public
void
run() {
int
count = listItemAdapter.getCount();
count +=
10
;
// 在原有的基础上再加10
submintData(count);
// 当返回的JSON为空时做如下判断
if
(jsonResult.equals(
"null"
)) {
footerProgressBarLayout.setVisibility(View.GONE);
// 隐藏
viewMoreButton.setVisibility(View.VISIBLE);
// 显示
Toast.makeText(getApplicationContext(),
"数据全部加载完成,没有更多数据"
, Toast.LENGTH_SHORT).show();
}
else
{
footerProgressBarLayout.setVisibility(View.GONE);
// 隐藏
viewMoreButton.setVisibility(View.VISIBLE);
// 显示
try
{
JSONArray jsonArray =
new
JSONArray(jsonResult);
System.out.print(jsonArray.length());
for
(
int
i =
0
; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
listItemAdapter.addNewsItem(json);
}
listItemAdapter.notifyDataSetChanged();
}
catch
(JSONException e) {
e.printStackTrace();
}
}
}
},
2000
);
}
}
/**
* 提交数据
*
* @param beginNumStr
* @return
*/
public
Boolean submintData(
int
beginNumStr) {
Map<String, String> map =
new
HashMap<String, String>();
map.put(
"beginNum"
, String.valueOf(beginNumStr));
boolean
result =
false
;
try
{
result = submintDataByHttpClientDoPost(map,
"http://192.168.1.116/DataInteraction/responsefenye.php"
);
}
catch
(Exception e) {
e.printStackTrace();
}
if
(result) {
return
true
;
}
else
{
return
false
;
}
}
/**
* 以HttpClient的DoPost方式提交数据到服务器
*
* @param map
* 传递进来的数据,以map的形式进行了封装
* @param path
* 要求服务器PHP的地址 返回的boolean类型的参数
* @throws Exception
*/
public
Boolean submintDataByHttpClientDoPost(Map<String, String> map,
String path)
throws
Exception {
// 1. 获得一个相当于浏览器对象HttpClient,使用这个接口的实现类来创建对象,DefaultHttpClient
HttpClient hc =
new
DefaultHttpClient();
// DoPost方式请求的时候设置请求,关键是路径
HttpPost request =
new
HttpPost(path);
// 2. 为请求设置请求参数,也即是将要上传到web服务器上的参数
List<NameValuePair> parameters =
new
ArrayList<NameValuePair>();
for
(Map.Entry<String, String> entry : map.entrySet()) {
NameValuePair nameValuePairs =
new
BasicNameValuePair(
entry.getKey(), entry.getValue());
parameters.add(nameValuePairs);
}
// 请求实体HttpEntity也是一个接口,我们用它的实现类UrlEncodedFormEntity来创建对象,注意后面一个String类型的参数是用来指定编码的
HttpEntity entity =
new
UrlEncodedFormEntity(parameters,
"gbk"
);
request.setEntity(entity);
// 3. 执行请求
HttpResponse response = hc.execute(request);
// 4. 通过返回码来判断请求成功与否
if
(response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
jsonResult = EntityUtils.toString(response.getEntity());
// 获取PHP端的echo
return
true
;
}
return
false
;
}
/**
* 获取JSON
*
* @param result
* @return
*/
public
List<JSONObject> getData(String result) {
// String url = "http://192.168.1.116/DataInteraction/responsejson.php";
// String result = HttpConn.getResult(url);
jsonData =
new
ArrayList<JSONObject>();
try
{
JSONArray jsonArray =
new
JSONArray(result);
for
(
int
i =
0
; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
jsonData.add(json);
}
}
catch
(JSONException e) {
e.printStackTrace();
}
return
jsonData;
}
// 开启线程获取数据
class
GetDetailThread
extends
Thread {
public
void
run() {
jsonData = getData(jsonResult);
Message msg = handler.obtainMessage();
handler.sendMessage(msg);
}
}
class
DealHandler
extends
Handler {
public
void
handleMessage(Message msg) {
listItemAdapter.setContext(RemindListViewActivity.
this
);
listItemAdapter.setList(jsonData);
listView.setAdapter(listItemAdapter);
}
}
/**
* 刷新
*
* @author Guozhilong
*
*/
class
refreshImageViewClick
implements
OnClickListener {
@Override
public
void
onClick(View v) {
refreshImageView.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
footerProgressBarLayout.setVisibility(View.GONE);
handler.postDelayed(
new
Runnable() {
@Override
public
void
run() {
if
(submintData(
0
) ==
true
) {
thread =
new
GetDetailThread();
thread.start();
// 开启一个线程获取数据
refreshImageView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
footerProgressBarLayout.setVisibility(View.VISIBLE);
}
else
{
refreshImageView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
footerProgressBarLayout.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),
"程序错误"
,
Toast.LENGTH_SHORT).show();
}
}
},
2000
);
}
}
}
BaseAdapter代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<div>
/**
* 提醒列表适配器
*
* @author 技术作坊
* @version 1.0
*
*/
public
class
RemindListAdapter
extends
BaseAdapter {
private
List<JSONObject> list;
private
Context context;
public
List<JSONObject> getList() {
return
list;
}
public
void
setList(List<JSONObject> list) {
this
.list = list;
}
public
Context getContext() {
return
context;
}
public
void
setContext(Context context) {
this
.context = context;
}
@Override
public
int
getCount() {
return
list.size();
}
@Override
public
Object getItem(
int
position) {
return
list.get(position);
}
@Override
public
long
getItemId(
int
position) {
return
position;
}
// public RemindListAdapter(Context context, List<JSONObject> list) {
// this.context = context;
// this.list = list;
// }
public
View getView(
int
position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = LayoutInflater.from(context);
View view = mInflater.inflate(R.layout.remind_list_item,
null
);
JSONObject json = (JSONObject) list.get(position);
TextView nameTextView = (TextView) view.findViewById(R.id.name_textView);
TextView contentTextView = (TextView) view.findViewById(R.id.content_textView);
TextView timeTextView = (TextView) view.findViewById(R.id.time_textView);
String titles =
""
;
//标题
String sender =
""
;
//发件人
String sendTime=
""
;
//发送时间
try
{
sender = json.getString(
"name"
);
titles = json.getString(
"sex"
);
sendTime=json.getString(
"password"
);
}
catch
(JSONException e) {
e.printStackTrace();
}
nameTextView.setText(
"名字:"
+ sender);
contentTextView.setText(
"性别:"
+ titles);
timeTextView.setText(
"密码:"
+sendTime);
return
view;
}
public
void
addNewsItem(JSONObject newsitem) {
list.add(newsitem);
}
}
</div>
效果图:
转载文章请注明出处
- Android listview分页显示——data from php
- android ListView分页显示
- Android ListView分页显示
- Android ListView分页显示
- android ListView的分段显示、分页显示
- Android ListView实现分页显示数据
- Android ListView实现分页显示数据
- Android ListView 按钮点击分页显示
- Android ListView 按钮点击分页显示
- Android ListView分页显示简单模拟实现
- Android数据的分页 listview显示数据
- android ListView的分段显示、分页显示(附源码)
- ListView分页显示数据
- ListView 分页显示
- ListView的分页显示
- ListView 分页显示
- ListView分页显示
- ListView分页显示
- 如何删除ubuntu系统安装的应用软件
- 唠唠叨叨又七夕
- Web服务的组合
- Timer和TimerTask与线程的关系
- 18哥四部经典——PHP(原创哦)
- Android listview分页显示——data from php
- VS2008切换时卡死解决方法
- 念,心随君浅舞天涯
- ASP.NET 打开网页方法
- 页面跳转
- Zend Server CE忘记密码无法启动,如何重置
- 嫣然一笑苦了情
- AT&T语法
- 中国未来的大国战略-中国战略网