android ListView 分页加载数据
1.mainActivity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context="mydemo.mycom.demo2.Home"> <LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_home_to_login"
android:text="@string/btn_to_register"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_exit"
android:text="@string/btn_to_exit"/>
</LinearLayout> <ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="match_parent"></ListView> </LinearLayout>
2.ListView的itemActivity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:orientation="horizontal"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context="mydemo.mycom.demo2.Home"> <TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:textColor="#FF172366"
android:padding="5dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:padding="5dp"
android:textColor="#FF870918"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:padding="5dp"
android:layout_height="wrap_content" /> </LinearLayout>
3.ListView footerActivity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="mydemo.mycom.demo2.Footer"> <ProgressBar
android:layout_width="50dp"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyle"
/>
<TextView android:text="@string/load_text"
android:textSize="20sp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
4.MainActivity.java
package mydemo.mycom.demo2; import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; import java.util.List; import mydemo.mycom.demo2.dao.UserDao;
import mydemo.mycom.demo2.entity.UserInfo;
import mydemo.mycom.demo2.service.LoginService; public class Home extends ActionBarActivity implements View.OnClickListener { private ListView lv;
private Button btn_home_to_login;
private Button btn_exit;
private List<UserInfo> list;
// 设置一个最大的数据条数,超过即不再加载
private int pageSize=10; //每次获取多少条数据
// 最后可见条目的索引
private int pages;//总共有多少页
private boolean loadfinished = true;
private MyAdapter myAdapter;
private View footer; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home); btn_home_to_login = (Button)findViewById(R.id.btn_home_to_login);
btn_exit = (Button)findViewById(R.id.btn_exit);
lv = (ListView)findViewById(R.id.lv);
btn_home_to_login.setOnClickListener(this);
btn_exit.setOnClickListener(this); footer = getLayoutInflater().inflate(R.layout.activity_footer,null); UserDao dao = new UserDao(this);
list = dao.getUserInfo(1,10); int total = dao.getTotal(); System.out.println(total+"==================================="); pages = total%pageSize==0 ? total/pageSize : total/pageSize+1; lv.setOnScrollListener(new MyOnScrollListener()); myAdapter = new MyAdapter();
//添加页脚(放在ListView最后)
lv.addFooterView(footer);
lv.setAdapter(myAdapter);
lv.removeFooterView(footer); lv.setOnItemClickListener(new MyItemClickListener()); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} @Override
public void onClick(View view) {
switch(view.getId())
{
case R.id.btn_home_to_login:
Intent intent = new Intent();
intent.setClass(Home.this,Register.class);
startActivity(intent);
break;
case R.id.btn_exit:
//清除登录数据
LoginService.removeLoginInfo(this);
Toast.makeText(this,"退出成功",Toast.LENGTH_SHORT).show(); Intent toLogin = new Intent();
toLogin.setClass(Home.this,MainActivity.class);
startActivity(toLogin);
break;
}
} public class MyAdapter extends BaseAdapter
{
public int getCount() {
return list.size();
} @Override
public Object getItem(int i) {
return null;
} @Override
public long getItemId(int i) {
return 0;
} @Override
public View getView(int i, View view, ViewGroup viewGroup) { UserInfo user = list.get(i); View itemview = View.inflate(Home.this,R.layout.activity_item,null); TextView tv_id = (TextView)itemview.findViewById(R.id.tv_id);
TextView tv_username = (TextView)itemview.findViewById(R.id.tv_username);
TextView tv_password = (TextView)itemview.findViewById(R.id.tv_password); tv_id.setText(user.getId()+"");
tv_username.setText(user.getUsername());
tv_password.setText(user.getPassword());
return itemview;
}
} public class MyItemClickListener implements AdapterView.OnItemClickListener
{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ListView lView = (ListView)lv;
TextView tv_id = (TextView)view.findViewById(R.id.tv_id);
TextView tv_username = (TextView)view.findViewById(R.id.tv_username);
TextView tv_password = (TextView)view.findViewById(R.id.tv_password);
String id = tv_id.getText().toString();
String username = tv_username.getText().toString();
String password = tv_password.getText().toString();
Toast.makeText(getApplicationContext(), id+","+username, Toast.LENGTH_SHORT).show();
//不同的activity之间的传值
Intent intent = new Intent();
intent.putExtra("username",username);
intent.putExtra("password",password);
intent.putExtra("id",id);
intent.setClass(Home.this,Edit.class);
startActivity(intent);
}
} public class MyOnScrollListener implements AbsListView.OnScrollListener
{
@Override
public void onScrollStateChanged(AbsListView absListView, int scrollState) { } @Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int lastItemIndex = lv.getLastVisiblePosition(); //获取当前屏幕最后的Item的id
System.out.println("====lastItemIndex:"+lastItemIndex);
System.out.println("====totalItemCount:"+totalItemCount);
if(lastItemIndex>0&&(lastItemIndex+1)==totalItemCount) //达到数据的最后一条记录
{
//当前页
int currentpage = totalItemCount%pageSize==0 ? totalItemCount /pageSize :totalItemCount /pageSize+1;
final int nextpage = currentpage +1; //下一页
System.out.println("================currentpage:"+currentpage);
System.out.println("================nextpage:"+nextpage);
if(nextpage<=pages && loadfinished)
{
loadfinished = false;
lv.addFooterView(footer);
new Thread(new Runnable() {
@Override
public void run() {
try{
Thread.sleep(3000);
}
catch (Exception e)
{ }
UserDao dao = new UserDao(Home.this); List<UserInfo> result = dao.getUserInfo(nextpage,pageSize);
handler.sendMessage(handler.obtainMessage(100,result));
}
}).start();
}
}
} Handler handler = new Handler() { public void handleMessage(Message msg)
{
list.addAll((List<UserInfo>)msg.obj);
//告诉ListView数据已经发生改变,要求ListView更新界面显示
myAdapter.notifyDataSetChanged();
if(lv.getFooterViewsCount()>0)
{
lv.removeFooterView(footer);
}
loadfinished = true;
; } };
} }
5.DAO.java
public List<UserInfo> getUserInfo(int pageIndex,int pageSize)
{
SQLiteDatabase db = helper.getWritableDatabase();
List<UserInfo> list = new ArrayList<UserInfo>(); String sql = "select * from user order by id asc limit "+(pageIndex-1)*pageSize+","+pageSize;
System.out.println("sql=======:"+sql+",pageIndex:"+pageIndex); Cursor cursor = db.rawQuery("select * from user order by id asc limit ?,?",new String[]{((pageIndex-1)*pageSize)+"",pageSize+""});
while(cursor.moveToNext())
{
int id = cursor.getInt(cursor.getColumnIndex("id"));
String username = cursor.getString(cursor.getColumnIndex("username"));
String password = cursor.getString(cursor.getColumnIndex("password")); UserInfo userInfo = new UserInfo(id,username,password);
list.add(userInfo);
}
cursor.close();
db.close();
return list; }
5.Entity.java
package mydemo.mycom.demo2.entity; public class UserInfo {
private int id;
private String username;
private String password; public UserInfo()
{ } public UserInfo(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}
android ListView 分页加载数据的更多相关文章
- Android中ListView分页加载数据
public class MainActivity extends Activity { private ListView listView=null; //listview的数据填充器 privat ...
- 【Android进阶】Listview分页加载数据的实现
Listview分页加载数据的实现 public class MainActivity extends Activity { protected static final int SUCCESS_GE ...
- Android ListView分页加载时图片显示问题
场景:Android ListView需要分页加载,每个item中会有图片,图片又是从网络下载的. 问题:在滑动加载下一页时,上一页的图片明明已经下载完成了,但是无法显示出来. Bug重现: 1,加载 ...
- listView 分页加载数据
Android应用 开发中,采用ListView组件来展示数据是很常用的功能,当一个应用要展现很多的数据时,一般情况下都不会把所有的数据一次就展示出来,而是通过分页 的形式来展示数据,个人觉得这样会有 ...
- Android GridView 分页加载数据
android UI 往右滑动,滑动到最后一页就自动加载数据并显示 如图: package cn.anycall.ju; import java.util.ArrayList; import java ...
- Android之分页加载数据
基本的原理和我的上一篇随笔“Android之下拉刷新ListView”差不多,代码里面有注释,这里就不废话了,直接上代码. 自定义的分页显示ListView——PagedListView.java代码 ...
- Android ListView异步加载数据
1.主Activity public class MainActivity extends Activity { private ListView listView; private ArrayLis ...
- java攻城狮之路(Android篇)--widget_webview_metadata_popupwindow_tabhost_分页加载数据_菜单
一.widget:桌面小控件1 写一个类extends AppWidgetProvider 2 在清单文件件中注册: <receiver android:name=".ExampleA ...
- 实现Android ListView 自动加载更多内容
研究了几个小时终于实现了Android ListView 自动加载的效果. 说说我是怎样实现的.分享给大家. 1.给ListView增加一个FooterView,调用addFooterView(foo ...
随机推荐
- elk认证模块x-pack安装
一.elasticsearch安装x-pack elasticsearch/bin/elasticsearch-plugin install x-pack ######## -> Downloa ...
- AT2364 Colorful Balls
AT2364 Colorful Balls 题意翻译 N个球排成一排,第i个球有颜色ci和重量wi. Snuke每次可以选择两个颜色相同,且重量之和不超过X的球,交换他们的位置. Snuke每次可以选 ...
- Ubuntu 16.04安装idea
此篇为http://www.cnblogs.com/EasonJim/p/7139275.html的分支页. 前提:必须正确安装JDK和Tomcat. 下载: https://www.jetbrain ...
- BZOJ4530 BJOI2014大融合(线段树合并+并查集+dfs序)
易知所求的是两棵子树大小的乘积.先建出最后所得到的树,求出dfs序和子树大小.之后考虑如何在动态加边过程中维护子树大小.这个可以用树剖比较简单的实现,但还有一种更快更优美的做法就是线段树合并.对每个点 ...
- ajax 调用 java webapi 多个参数(一)
最近开发 java webapi. 遇到一个问题,如果是多个参数(其中包含对象类型),我应该怎么传递? 一 先看解决方案: ajax <script> var data={ " ...
- Food HDU - 4292 (结点容量 拆点) Dinic
You, a part-time dining service worker in your college’s dining hall, are now confused with a new pr ...
- java监控工具VisualVM
java监控工具VisualVM https://visualvm.github.io/ https://visualvm.github.io/documentation.html https://h ...
- python3网络爬虫(2.1):爬取堆糖美女
额,明明记得昨晚存了草稿箱,一觉醒来没了,那就简写点(其实是具体怎么解释我也不太懂/xk,纯属个人理解,有错误还望指正) 环境: 版本:python3 IDE:pycharm2017.3.3 浏览器: ...
- 自学Linux Shell16.3-函数递归+创建库
点击返回 自学Linux命令行与Shell脚本之路 16.3-函数递归+创建库 1. 函数递归 递归调用函数是指函数调用自身进行求解. 通常,递归函数有基值,函数最终递推到达该值. 许多高级数学算法使 ...
- Shell基础知识(一)
教程链接:shell从入门到入门 这个网站还有其他教程,可以尝试下看看. 普及类文章:bash/cmd/dos之间有什么区别与联系 >> bash是Linux下的一个shell应用程序 ...