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 分页加载数据的更多相关文章

  1. Android中ListView分页加载数据

    public class MainActivity extends Activity { private ListView listView=null; //listview的数据填充器 privat ...

  2. 【Android进阶】Listview分页加载数据的实现

    Listview分页加载数据的实现 public class MainActivity extends Activity { protected static final int SUCCESS_GE ...

  3. Android ListView分页加载时图片显示问题

    场景:Android ListView需要分页加载,每个item中会有图片,图片又是从网络下载的. 问题:在滑动加载下一页时,上一页的图片明明已经下载完成了,但是无法显示出来. Bug重现: 1,加载 ...

  4. listView 分页加载数据

    Android应用 开发中,采用ListView组件来展示数据是很常用的功能,当一个应用要展现很多的数据时,一般情况下都不会把所有的数据一次就展示出来,而是通过分页 的形式来展示数据,个人觉得这样会有 ...

  5. Android GridView 分页加载数据

    android UI 往右滑动,滑动到最后一页就自动加载数据并显示 如图: package cn.anycall.ju; import java.util.ArrayList; import java ...

  6. Android之分页加载数据

    基本的原理和我的上一篇随笔“Android之下拉刷新ListView”差不多,代码里面有注释,这里就不废话了,直接上代码. 自定义的分页显示ListView——PagedListView.java代码 ...

  7. Android ListView异步加载数据

    1.主Activity public class MainActivity extends Activity { private ListView listView; private ArrayLis ...

  8. java攻城狮之路(Android篇)--widget_webview_metadata_popupwindow_tabhost_分页加载数据_菜单

    一.widget:桌面小控件1 写一个类extends AppWidgetProvider 2 在清单文件件中注册: <receiver android:name=".ExampleA ...

  9. 实现Android ListView 自动加载更多内容

    研究了几个小时终于实现了Android ListView 自动加载的效果. 说说我是怎样实现的.分享给大家. 1.给ListView增加一个FooterView,调用addFooterView(foo ...

随机推荐

  1. spark中saveAsTextFile的错误

    写了很简单的一段spark代码,将结果保存为windows本地文件,执行之后总是报错NullPointerException 查询之后 发现是本地缺少hadoop需要的一个文件所致 如果本地已经安装了 ...

  2. flask客户端测试使用设置cookie参数

    今天在对flask客户端进行测试,然后看到我们服务器端用请求前钩子写了这样的代码 @app.before_requestdef before_request(): session = request. ...

  3. 创建一个规范的django项目

    1. 创建项目 2. 创建static目录及配置 1.创建放css, javascript,img的目录 2.在settings.py中将static绝对路径保存到变量STATICFILES_DIRS ...

  4. 怎样让DBGrid在按住Shift点鼠标的同时能将连续范围的多行选中?

    参见例子:…privateSel : Boolean ;//判断是否处于选择状态BookMark : TBookMark ;//记录先前的位置…procedure TForm1.DBGrid1Mous ...

  5. python之使用heapq()函数计算列表中数值大小

    # heapq函数:计算列表最大几个值和最小几个值 # 语法:heapq.nlargest(n, list,[key]) # n表示最大或最小的几个: list为分析的对象: key为排序关键字,非必 ...

  6. asp.net 的三种开发模式

    一, Web Pages 是三种创建 ASP.NET 网站和 Web 应用程序的编程模式中的一种. 其他两种编程模式是 Web Forms 和 MVC(Model View Controller 模型 ...

  7. BZOJ2212 POI2011Tree Rotations(线段树合并)

    显然子树内的操作不会对子树外产生影响.于是贪心,若交换之后子树内逆序对减少就交换. 这个东西可以用权值线段树计算.操作完毕后需要对两棵权值线段树合并,这个的复杂度是两棵线段树的重复节点个数.那么总复杂 ...

  8. BZOJ3569 DZY Loves Chinese II(随机化+树上差分+线性基)

    上一题的强制在线版.对图跑出一个dfs树,给非树边赋上随机权值,树边的权值为覆盖他的非树边权值的异或.这样如果某条树边和覆盖他的非树边都被割掉(即图不连通),他们的异或值就为0.每次对询问看有没有子集 ...

  9. 网络流Dinic模板

    #include <iostream> #include <cstdio> #include <cstring> #include <queue> #d ...

  10. [luogu3834]静态区间第k小【主席树】

    传送门:https://www.luogu.org/problemnew/show/P3834 题目描述 如题,给定N个整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 分析 很多人都说是用 ...