如何用Android实现listview分页

来源:爱站网时间:2020-08-13编辑:网友分享
Listview提供了丰富的接口和实现,因此我们可以定制它的显示,如果有大量的数据,我们需要进行分页显示,下面爱站技术频道小编给大家分享如何用Android实现listview分页,需要的朋友可以参考下文的介绍。

Listview提供了丰富的接口和实现,因此我们可以定制它的显示,如果有大量的数据,我们需要进行分页显示,下面爱站技术频道小编给大家分享如何用Android实现listview分页,需要的朋友可以参考下文的介绍。

最近做了下listview的分页,跟WEB上的分页是一个意思,需要那几个分页参数,不同的是sqlite中分页的查询语句,简便的方法需要用Limit,Offset关键字,前者是查询每页展示的记录数,后者是越过多少记录数,说得明白点就是忽略前面多少行记录之后,取多少行记录

我分页采用了一个重要的类Page,通过封装Page类,做为参数传递进来,返回出去也是个Page对象

import java.util.Collections; 
import java.util.List; 
/** 
 * 注意所有序号从1开始. 
 * 
 * @param  Page中记录的类型. 
 * 
 */ 
public class Page { 
  //-- 公共变量 --// 
  public static final String ASC = "asc"; 
  public static final String DESC = "desc"; 
  //-- 分页参数 --// 
  protected int pageNo = 0;// 当前页号 
  protected int pageSize = 1;// 每页显示的记录数 
  protected String orderBy = null; 
  protected String order = null; 
  protected boolean autoCount = true; 
  //-- 返回结果 --// 
  protected List result = Collections.emptyList(); 
  protected long totalCount = -1;// 总记录数 
  //-- 构造函数 --// 
  public Page() { 
  } 
  public Page(final int pageSize) { 
    setPageSize(pageSize); 
  } 
  public Page(final int pageSize, final boolean autoCount) { 
    setPageSize(pageSize); 
    setAutoCount(autoCount); 
  } 
  //-- 访问查询参数函数 --// 
  /** 
   * 获得当前页的页号,序号从0开始,默认为0. 
   */ 
  public int getPageNo() { 
    return pageNo; 
  } 
  /** 
   * 设置当前页的页号,序号从0开始,低于0时自动调整为0. 
   */ 
  public void setPageNo(final int pageNo) { 
    this.pageNo = pageNo; 
    if (pageNo  getResult() { 
    return result; 
  } 
  /** 
   * 设置页内的记录列表. 
   */ 
  public void setResult(final List result) { 
    this.result = result; 
  } 
  /** 
   * 取得总记录数, 默认值为-1. 
   */ 
  public long getTotalCount() { 
    return totalCount; 
  } 
  /** 
   * 设置总记录数. 
   */ 
  public void setTotalCount(final long totalCount) { 
    this.totalCount = totalCount; 
  } 
  /** 
   * 根据pageSize与totalCount计算总页数, 默认值为-1. 
   */ 
  public long getTotalPages() { 
    if (totalCount  0) { 
      count++; 
    } 
    return count; 
  } 
  /** 
   * 是否还有下一页. 
   */ 
  public boolean isHasNext() { 
    return (pageNo + 1 = 0); 
  } 
  /** 
   * 取得上页的页号, 序号从1开始. 
   * 当前页为首页时返回首页序号. 
   */ 
  public int getPrePage() { 
    if (isHasPre()) 
      return pageNo - 1; 
    else 
      return pageNo; 
  } 
}

下面是封装的POJO对象,界面listview的item展示就是靠它

public class Record { 
  private Integer rId; 
  private String fromUser; 
  private String toUser; 
  private String content; 
  private String rTime; 
  private Integer isReaded; 
  public Integer getrId() { 
    return rId; 
  } 
  public void setrId(Integer rId) { 
    this.rId = rId; 
  } 
  public String getFromUser() { 
    return fromUser; 
  } 
  public void setFromUser(String fromUser) { 
    this.fromUser = fromUser; 
  } 
  public String getToUser() { 
    return toUser; 
  } 
  public void setToUser(String toUser) { 
    this.toUser = toUser; 
  } 
  public String getContent() { 
    return content; 
  } 
  public void setContent(String content) { 
    this.content = content; 
  } 
  public String getrTime() { 
    return rTime; 
  } 
  public void setrTime(String rTime) { 
    this.rTime = rTime; 
  } 
  public Integer getIsReaded() { 
    return isReaded; 
  } 
  public void setIsReaded(Integer isReaded) { 
    this.isReaded = isReaded; 
  }
}

好吧,来看下DAO类里的重要方法同样是Page对象,在DAO类里做的操作就是对总记录数和结果list进行赋值

public Page getRecordPage(Page page, String loginName, 
      String contactName) { 
    int pageSize = page.getPageSize(); 
    int pageNo = page.getPageNo(); 
    String sql = "select * from " 
        + ContactsManagerDbAdapter.TABLENAME_RECORD 
        + " where (fromUser = ? and toUser = ?) or (fromUser = ? and toUser = ?)" 
        + " Limit " + String.valueOf(pageSize) + " Offset " 
        + String.valueOf(pageNo * pageSize); 
    String[] selectionArgs = { loginName, contactName, contactName, 
        loginName }; 
    Cursor cursor = mSQLiteDatabaseWritable.rawQuery(sql, selectionArgs); 
    List result = new ArrayList(); 
    for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){ 
      Record record = new Record(); 
      record.setrId(cursor.getInt(0)); 
      record.setFromUser(cursor.getString(1)); 
      record.setToUser(cursor.getString(2)); 
      record.setContent(cursor.getString(3)); 
      record.setrTime(cursor.getString(4)); 
      record.setIsReaded(cursor.getInt(5)); 
      result.add(record); 
    } 
    page.setTotalCount(getRowCount(loginName, contactName)); 
    page.setResult(result); 
    cursor.close(); 
    return page; 
} 
/** 
* 总记录数 
*/ 
public int getRowCount(String loginName, String contactName){ 
    String sql = "select * from " 
      + ContactsManagerDbAdapter.TABLENAME_RECORD 
      + " where (fromUser = ? and toUser = ?) or (fromUser = ? and toUser = ?)"; 
    String[] selectionArgs = { loginName, contactName, contactName,
        loginName }; 
    Cursor cursor = mSQLiteDatabaseWritable.rawQuery(sql, selectionArgs); 
    int count = cursor.getCount(); 
    cursor.close(); 
    return count; 
} 

关于listview的展示和自定义适配器,在此不提起,总之界面上上一页和下一页按钮的点击事件需要刷新listview

public class ChatHistory extends Activity { 
  ListView historyView; 
  Button prevPageBtn; 
  Button nextPageBtn; 
  TextView historyPageTv; 
  Button backChatsHistoryBtn; 
  Button deleteHistoryBtn; 
  List historyRecordList;  
  Page page; 
  String loginName; 
  String contactName; 
  ChatHistoryService chatHistoryService; 
  ContactsManagerDbAdapter dbAdapter; 
  private BaseAdapter adapter; 
  private static final int STATUS_CHANGE = 0; 
  private Handler mHandler; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.chats_history); 
    historyView = (ListView) findViewById(android.R.id.list); 
    prevPageBtn = (Button) findViewById(R.id.chat_prev_page); 
    nextPageBtn = (Button) findViewById(R.id.chat_next_page); 
    historyPageTv = (TextView) findViewById(R.id.history_page); 
    backChatsHistoryBtn = (Button) findViewById(R.id.back_chats_history); 
    deleteHistoryBtn = (Button) findViewById(R.id.delete_history); 
    SharedPreferences sharedata = ChatHistory.this.getSharedPreferences("data", 0); 
    loginName = sharedata.getString("loginName", ""); 
    contactName = getIntent().getStringExtra("contactName"); 
    dbAdapter = new ContactsManagerDbAdapter(getApplicationContext()); 
    dbAdapter.open(); 
    chatHistoryService = new ChatHistoryService(ChatHistory.this);
    page = new Page(); 
    page.setPageSize(8); 
    page = chatHistoryService.getHistoryPage(page, loginName, contactName, dbAdapter); 
    historyRecordList = page.getResult(); 
    updateTextView(); 
    prevPageBtn.setOnClickListener(prevPageButtonListener); 
    nextPageBtn.setOnClickListener(nextPageButtonListener); 
    backChatsHistoryBtn.setOnClickListener(backButtonListener); 
    deleteHistoryBtn.setOnClickListener(deleteHistoryButtonListener); 
    adapter = new HistoryListAdapter(ChatHistory.this); 
    historyView.setAdapter(adapter); 
    mHandler = new Handler(){  
       public void handleMessage(Message msg) { 
          switch(msg.what){ 
          case STATUS_CHANGE: 
            // 处理UI更新等操作 
            updateUI(); 
          } 
       }; 
    }; 
  } 
  private void updateUI() { 
    //详细的更新 
    adapter.notifyDataSetChanged();// 更新ListView 
    updateTextView(); 
  } 
  /** 
   * 更新页码 
   */ 
  private void updateTextView(){    
    if(historyRecordList.size() == 0){ 
      historyPageTv.setText(0 + "/" + 0); 
    } else{ 
      historyPageTv.setText(page.getPageNo() + 1 + "/" + page.getTotalPages()); 
    } 
  } 
  public class HistoryListAdapter extends BaseAdapter{ 
    private class RecentViewHolder { 
      TextView sender_context; 
      TextView rTime; 
    } 
    Context context; 
    LayoutInflater mInflater; 
    public HistoryListAdapter(Context context){ 
      this.context = context; 
      mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 
    @Override 
    public int getCount() { 
      // TODO Auto-generated method stub 
      return historyRecordList.size(); 
    } 
    @Override 
    public Object getItem(int position) { 
      // TODO Auto-generated method stub 
      return historyRecordList.get(position); 
    } 
    @Override 
    public long getItemId(int position) { 
      // TODO Auto-generated method stub 
      return position; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 
      RecentViewHolder holder; 
      if (convertView == null) { 
        convertView = mInflater.inflate(R.layout.message_layout, null); 
        holder = new RecentViewHolder(); 
        holder.sender_context = (TextView) convertView 
            .findViewById(R.id.message_view_sender_content);
        holder.rTime = (TextView) convertView 
            .findViewById(R.id.message_view_timestamp); 
        convertView.setTag(holder); 
      } else { 
        holder = (RecentViewHolder) convertView.getTag(); 
      } 
      Record record = historyRecordList.get(position); 
      if (record != null) { 
        holder.sender_context.setText(record.getFromUser() + ":" + record.getContent()); 
        holder.rTime.setText(record.getrTime()); 
      } 
      return convertView; 
    } 
  } 
  /** 
   * 上一页按钮的监听事件 
   */ 
  OnClickListener prevPageButtonListener = new OnClickListener(){ 
    @Override 
    public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(page.isHasPre()){ 
        page.setPageNo(page.getPageNo() - 1); 
        historyRecordList = chatHistoryService.getHistoryPage(page, loginName, contactName, dbAdapter).getResult(); 
        Message msg = new Message(); 
        msg.what = STATUS_CHANGE; 
        mHandler.sendMessage(msg);// 向Handler发送消息,更新UI 
      } 
    } 
  }; 
  /** 
   * 下一页按钮的监听事件 
   */ 
  OnClickListener nextPageButtonListener = new OnClickListener(){ 
    @Override 
    public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(page.isHasNext()){ 
        page.setPageNo(page.getPageNo() + 1); 
        historyRecordList = chatHistoryService.getHistoryPage(page, loginName, contactName, dbAdapter).getResult(); 
        Message msg = new Message(); 
        msg.what = STATUS_CHANGE; 
        mHandler.sendMessage(msg);// 向Handler发送消息,更新UI 
      } 
    } 
  }; 
  /** 
   * 删除历史记录按钮监听器 
   */ 
  OnClickListener deleteHistoryButtonListener = new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
      // TODO Auto-generated method stub      
    } 
  }; 
  /** 退出聊天界面监听器 */ 
  OnClickListener backButtonListener = new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
      // TODO Auto-generated method stub 
      // 返回到聊天界面 
      finish(); 
      dbAdapter.close(); 
    } 
  }; 
  @Override 
  public boolean onKeyDown(int keyCode, KeyEvent event) { 
    // 按下键盘上返回按钮 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
      finish(); 
      dbAdapter.close(); 
      return true; 
    } else { 
      return super.onKeyDown(keyCode, event); 
    }
  }
}

上一页,下一页所做的操作也就是在判断是否有上一页和下一页的情况下对页号pageNo的加减操作
最后写上ChatHistoryService.java总觉得是多余的

public class ChatHistoryService { 
  Context context; 
  public ChatHistoryService(Context context) { 
    this.context = context; 
  } 
  public Page getHistoryPage(Page page, String loginName, 
      String contactName, ContactsManagerDbAdapter dbAdapter) { 
    RecordDao recordDao = new RecordDao(dbAdapter); 
    return recordDao.getRecordPage(page, loginName, contactName); 
  } 
  public int deleteHistory(String loginName, 
      String contactName, ContactsManagerDbAdapter dbAdapter){ 
    RecordDao recordDao = new RecordDao(dbAdapter); 
    return recordDao.deleteHistoryRecord(loginName, contactName); 
  } 
} 

顺带放上XML布局文件吧
chats_history.xml

 
     
     
     
     
  

 message_layout.xml

最后来看看效果吧,别欺负我没贴上数据库建表语句,看下我的POJO类就知道我数据库表怎么建的

以上是爱站技术频道小编分享给大家的如何用Android实现listview分页,对于新手朋友来说,应该掌握更多更全面的专业技巧,不要急于求成哦。

上一篇:Android开发中MVC设计模式分析

下一篇:Android实现长按键退出应用程序

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载