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 ...
随机推荐
- scipy优化器optimizer
#optimazer优化器 from scipy.optimize import minimize def rosem(x): return sum(100.0*(x[1:]-x[:-1])**2.0 ...
- linux中tomcat修改错误日志路径
涉及文件 log4j.properties (一般开发将该文件放在项目的缺省目录即源包下,在文件系统里,就是在项目的/src/java目录下,缺省的文件名是log4j.properties,这样项 ...
- html的表格 table
創建表格: 每一個表格以table開始: 每一個表格行以tr開始: 每一個數據以td開始:td的內容可以文本.圖像.表格.表單.段落等. 表格邊框: border設置邊框的粗細,但無法設置行間距,也無 ...
- Bootstrap按钮式下拉菜单
前面的话 按钮式下拉菜单仅从外观上看,和下拉菜单效果基本上是一样的.不同的是普通的下拉菜单是block元素,而按钮式下拉菜单是inline-block元素.本文将详细介绍Bootstrap按钮式下拉菜 ...
- Bootstrap学习目录
前面的话 Bootstrap与CSS的关系,类似于javascript与jQuery的关系,原理与应用的关系.只是jQuery不再火爆,而Bootstrap依然火热,它在github有着超过100万的 ...
- 删除文件,copy文件bat
由于IntelliJ IDEA Build Artifacts速度实在是无法忍受,特整理了一个bat脚本方便maven install后更新jar文件到部署包 del /F /S /Q D:\Idea ...
- Apache访问控制
简单概述 httpd服务的访问控制 作用: 控制对网站资源的访问 为特定的网站目录添加访问授权 常用访问控制方式: 客户机地址限制 用户授权限制 1.基于客户端地址的访问控制 Order配置项,定义控 ...
- AGC005F Many Easy Problems(NTT)
先只考虑求某个f(k).考虑转换为计算每条边的贡献,也即该边被所选连通块包含的方案数.再考虑转换为计算每条边不被包含的方案数.这仅当所选点都在该边的同一侧.于是可得f(k)=C(n,k)+ΣC(n,k ...
- BZOJ4541 HNOI2016矿区(平面图转对偶图)
考虑先将平面图转化为对偶图.具体地,将无向边拆成两条有向边.每次考虑找到包围一个区域的所有边.对当前考虑的边,找到该边的反向边在该边终点的出边集中,按极角序排序的后继,这条后继边也是包围该区域的边.这 ...
- 11 Zabbix Item类型之Zabbix Calculated 计算型Item类型
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 11 Zabbix Item类型之Zabbix Calculated 计算型Item类型 计算类型 ...