189 8069 5689

如何在Android应用中分批显示ListView数据

这期内容当中小编将会给大家带来有关如何在Android应用中分批显示 ListView数据,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

创新互联服务项目包括古交网站建设、古交网站制作、古交网页制作以及古交网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,古交网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到古交省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

Android ListView数据的分批显示

布局文件activity_call_safe2.xml

<?xml version="1.0" encoding="utf-8"?>

  
    
    

CasllSafeActivity.java

package com.ldw.safe.Activity;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.ldw.safe.R;
import com.ldw.safe.adapter.MyBaseAdapter;
import com.ldw.safe.bean.BlackNumberInfo;
import com.ldw.safe.db.dao.BlackNumberDao;
public class CallSafeActivity extends Activity {
 private ListView list_view_callsafe;
 private List blackNumberInfos;
 private LinearLayout ll_pb;
 private BlackNumberDao dao;
 private CallSafeAdapter adapter;
 private int totalNumber;//总的条数
 private int mStartIndex = 0;//开始加载的位置
 private int maxCount = 20;//每页加载多少数据
 @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call_safe2);
    initUi();
    initData();
 }
 //初始化数据
 private Handler handler = new Handler(){
 @Override
    public void handleMessage(Message msg) {
  //数据初始化结束,关闭家在全的展示
  ll_pb.setVisibility(View.INVISIBLE);
  //防止适配器一直刷新
  if(adapter == null){
  adapter = new CallSafeAdapter(blackNumberInfos, CallSafeActivity.this);
  //配置适配器
  list_view_callsafe.setAdapter(adapter);
  }else{
   adapter.notifyDataSetChanged();
  }
 }
 };
 private EditText et_page_number;
 //初始化数据
 private void initData() {
 //防止阻塞主线程,在子线程中初始化数据(后期数据会很多)
 new Thread(){
  @Override
  public void run(){
  dao = new BlackNumberDao(CallSafeActivity.this);
   //一共有多少条数据
     totalNumber = dao.getTotalNumber();
  //分批加载数据
     if(blackNumberInfos == null){
      //第一次加载
      blackNumberInfos = dao.findPar2(mStartIndex, maxCount);
     }else{
      //把后面的数据。追加到blackNumberInfos集合里面。防止黑名单被覆盖
      blackNumberInfos.addAll(dao.findPar2(mStartIndex, maxCount));
     }
  handler.sendEmptyMessage(0);
  }
 }.start();
 }
 //初始化UI
 private void initUi() {
 ll_pb = (LinearLayout) findViewById(R.id.ll_pb);
    //初始化的时候展示加载的圆圈
    ll_pb.setVisibility(View.VISIBLE);
 list_view_callsafe = (ListView) findViewById(R.id.list_view_callsafe);
 //设置listView的滚动监听器
 list_view_callsafe.setOnScrollListener(new OnScrollListener(){
      //状态改变时候回调的方法
      /*
       * @param scrollState 表示滚动的状态
       *AbsListView.OnScrollListener.SCROLL_STATE_IDLE 闲置状态
       *AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL 手指触摸的时候的状态
       *AbsListView.OnScrollListener.SCROLL_STATE_FLING 惯性
       */
  @Override
  public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
        case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
          //获取到最后一条显示的数据条数
          int lastVisiblePosition = list_view_callsafe.getLastVisiblePosition();
          System.out.println("lastVisiblePosition==========" + lastVisiblePosition);
            if(lastVisiblePosition == blackNumberInfos.size() - 1){//判断最后一个数据是不是每页显示的数据-1
              // 加载更多的数据。 更改加载数据的开始位置
              mStartIndex += maxCount;
              if (mStartIndex >= totalNumber) {
                Toast.makeText(getApplicationContext(),
                    "没有更多的数据了。", Toast.LENGTH_SHORT).show();
                return;
              }
              initData();
            }
          break;
      }
  }
  //listview滚动的时候调用的方法
      //时时调用。当我们的手指触摸的屏幕的时候就调用
  @Override
  public void onScroll(AbsListView view, int firstVisibleItem,
   int visibleItemCount, int totalItemCount) {
  // TODO Auto-generated method stub
  }
 });
 }
 //对代码进行优化,继承自定义MyBaseAdapter,MyBaseAdapter继承自BaseAdapter封装一些常用的方法
 private class CallSafeAdapter extends MyBaseAdapter{
 private CallSafeAdapter(List lists, Context mContext){
  super(lists, mContext);
 }
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  //谷歌推出的优化方案
  ViewHolder holder;
  if(convertView == null){
  convertView = View.inflate(CallSafeActivity.this, R.layout.item_call_safe, null);
  holder = new ViewHolder();
  holder.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
  holder.tv_mode = (TextView) convertView.findViewById(R.id.tv_mode);
  holder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
  convertView.setTag(holder);
  }else{
  holder = (ViewHolder) convertView.getTag();
  }
  holder.tv_number.setText(lists.get(position).getNumber());
  String mode = lists.get(position).getMode();
  if(mode.equals("1")){
  holder.tv_mode.setText("电话和短信拦截");
  }else if(mode.equals("2")){
  holder.tv_mode.setText("电话拦截");
  }else if(mode.equals("3")){
  holder.tv_mode.setText("短信拦截");
  }
  final BlackNumberInfo info = lists.get(position);
  //监听删除数据按钮,删除数据
  holder.iv_delete.setOnClickListener(new OnClickListener(){
  @Override
  public void onClick(View v) {
   String number = info.getNumber();
   boolean result = dao.delete(number);
   if(result){
   Toast.makeText(CallSafeActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
   lists.remove(info);
   //更新ListView
   adapter.notifyDataSetChanged();
   }else{
   Toast.makeText(CallSafeActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
   }
  }
  });
  return convertView;
  /*
  //谷歌推出的优化方案
  ViewHolder holder;
  if(convertView == null){
  convertView = View.inflate(CallSafeActivity.this, R.layout.item_call_safe, null);
  holder = new ViewHolder();
  holder.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
  holder.tv_mode = (TextView) convertView.findViewById(R.id.tv_mode);
  convertView.setTag(holder);
  }else{
  holder = (ViewHolder) convertView.getTag();
  }
  holder.tv_number.setText(blackNumberInfos.get(position).getNumber());
  String mode = blackNumberInfos.get(position).getMode();
  if(mode.equals("1")){
  holder.tv_mode.setText("电话和短信拦截");
  }else if(mode.equals("2")){
  holder.tv_mode.setText("电话拦截");
  }else if(mode.equals("3")){
  holder.tv_mode.setText("短信拦截");
  }
  return convertView;
  */
  /*初步的优化
  convertView = View.inflate(CallSafeActivity.this, R.layout.item_call_safe, null);
  TextView tv_number = (TextView) convertView.findViewById(R.id.tv_number);
  TextView tv_mode = (TextView) convertView.findViewById(R.id.tv_mode);
  tv_number.setText(blackNumberInfos.get(position).getNumber());
  String mode = blackNumberInfos.get(position).getMode();
  if(mode.equals("1")){
  tv_mode.setText("电话和短信拦截");
  }else if(mode.equals("2")){
  tv_mode.setText("电话拦截");
  }else if(mode.equals("3")){
  tv_mode.setText("短信拦截");
  }
  return convertView;
  */
  /*传统的方式
  View view = View.inflate(CallSafeActivity.this, R.layout.item_call_safe, null);
  TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
  TextView tv_mode = (TextView) view.findViewById(R.id.tv_mode);
  tv_number.setText(blackNumberInfos.get(position).getNumber());
  String mode = blackNumberInfos.get(position).getMode();
  if(mode.equals("1")){
  tv_mode.setText("电话和短信拦截");
  }else if(mode.equals("2")){
  tv_mode.setText("电话拦截");
  }else if(mode.equals("3")){
  tv_mode.setText("短信拦截");
  }
  return view;
  */
 } 
 }
 static class ViewHolder{
 TextView tv_number;
 TextView tv_mode;
 ImageView iv_delete;
 }
}

上述就是小编为大家分享的如何在Android应用中分批显示 ListView数据了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


分享标题:如何在Android应用中分批显示ListView数据
当前网址:http://cdxtjz.com/article/pdggid.html

其他资讯