真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

如何在Android應(yīng)用中分批顯示ListView數(shù)據(jù)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)如何在Android應(yīng)用中分批顯示 ListView數(shù)據(jù),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括古交網(wǎng)站建設(shè)、古交網(wǎng)站制作、古交網(wǎng)頁制作以及古交網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,古交網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到古交省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

Android ListView數(shù)據(jù)的分批顯示

布局文件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;//總的條數(shù)
 private int mStartIndex = 0;//開始加載的位置
 private int maxCount = 20;//每頁加載多少數(shù)據(jù)
 @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call_safe2);
    initUi();
    initData();
 }
 //初始化數(shù)據(jù)
 private Handler handler = new Handler(){
 @Override
    public void handleMessage(Message msg) {
  //數(shù)據(jù)初始化結(jié)束,關(guān)閉家在全的展示
  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;
 //初始化數(shù)據(jù)
 private void initData() {
 //防止阻塞主線程,在子線程中初始化數(shù)據(jù)(后期數(shù)據(jù)會(huì)很多)
 new Thread(){
  @Override
  public void run(){
  dao = new BlackNumberDao(CallSafeActivity.this);
   //一共有多少條數(shù)據(jù)
     totalNumber = dao.getTotalNumber();
  //分批加載數(shù)據(jù)
     if(blackNumberInfos == null){
      //第一次加載
      blackNumberInfos = dao.findPar2(mStartIndex, maxCount);
     }else{
      //把后面的數(shù)據(jù)。追加到blackNumberInfos集合里面。防止黑名單被覆蓋
      blackNumberInfos.addAll(dao.findPar2(mStartIndex, maxCount));
     }
  handler.sendEmptyMessage(0);
  }
 }.start();
 }
 //初始化UI
 private void initUi() {
 ll_pb = (LinearLayout) findViewById(R.id.ll_pb);
    //初始化的時(shí)候展示加載的圓圈
    ll_pb.setVisibility(View.VISIBLE);
 list_view_callsafe = (ListView) findViewById(R.id.list_view_callsafe);
 //設(shè)置listView的滾動(dòng)監(jiān)聽器
 list_view_callsafe.setOnScrollListener(new OnScrollListener(){
      //狀態(tài)改變時(shí)候回調(diào)的方法
      /*
       * @param scrollState 表示滾動(dòng)的狀態(tài)
       *AbsListView.OnScrollListener.SCROLL_STATE_IDLE 閑置狀態(tài)
       *AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL 手指觸摸的時(shí)候的狀態(tài)
       *AbsListView.OnScrollListener.SCROLL_STATE_FLING 慣性
       */
  @Override
  public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
        case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
          //獲取到最后一條顯示的數(shù)據(jù)條數(shù)
          int lastVisiblePosition = list_view_callsafe.getLastVisiblePosition();
          System.out.println("lastVisiblePosition==========" + lastVisiblePosition);
            if(lastVisiblePosition == blackNumberInfos.size() - 1){//判斷最后一個(gè)數(shù)據(jù)是不是每頁顯示的數(shù)據(jù)-1
              // 加載更多的數(shù)據(jù)。 更改加載數(shù)據(jù)的開始位置
              mStartIndex += maxCount;
              if (mStartIndex >= totalNumber) {
                Toast.makeText(getApplicationContext(),
                    "沒有更多的數(shù)據(jù)了。", Toast.LENGTH_SHORT).show();
                return;
              }
              initData();
            }
          break;
      }
  }
  //listview滾動(dòng)的時(shí)候調(diào)用的方法
      //時(shí)時(shí)調(diào)用。當(dāng)我們的手指觸摸的屏幕的時(shí)候就調(diào)用
  @Override
  public void onScroll(AbsListView view, int firstVisibleItem,
   int visibleItemCount, int totalItemCount) {
  // TODO Auto-generated method stub
  }
 });
 }
 //對(duì)代碼進(jìn)行優(yōu)化,繼承自定義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) {
  //谷歌推出的優(yōu)化方案
  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);
  //監(jiān)聽刪除數(shù)據(jù)按鈕,刪除數(shù)據(jù)
  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;
  /*
  //谷歌推出的優(yōu)化方案
  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;
  */
  /*初步的優(yōu)化
  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;
  */
  /*傳統(tǒng)的方式
  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應(yīng)用中分批顯示 ListView數(shù)據(jù)了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


分享名稱:如何在Android應(yīng)用中分批顯示ListView數(shù)據(jù)
網(wǎng)頁URL:http://weahome.cn/article/pdggid.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部