listview分页

来源:互联网 发布:mysql 5.7数据库迁移 编辑:程序博客网 时间:2024/05/16 17:27

package com.ishow.kinmet.ui;

import java.util.List;
import android.content.Intent;
import android.os.AsyncTask.Status;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.ishow.kinmet.bean.AgencyApproval;
import com.ishow.kinmet.comm.BaseResult;
import com.ishow.kinmet.data.DataCenter;
import com.ishow.kinmet.task.AgencyApprovalDetailTask;
import com.ishow.kinmet.task.SimpleDialogProgress;
import com.ishow.kinmet.task.TaskHost;
import com.ishow.kinmet.ui.adapter.SimpleListViewAdapter;
import com.ishow.kinmet.ui.adapter.viewholder.AgencyApprovalReportTitleViewHolder;
import com.ishow.util.ui.BaseActivity;

public class HomeActivity extends BaseActivity {

 private TextView currentUserTextView;
 private Button newReportButton;
 private Button agencyTransactionButton;
 private Button messageButton;
 private Button officialDocumentButton;
 private Button prePageBtn;
 private Button nextPageBtn;
 private AgencyApprovalDetailTask approvalTask;
 private ListView listView;
 private SimpleListViewAdapter<AgencyApproval, AgencyApprovalReportTitleViewHolder> adapter;
 private int viewCount = 10; //10 record a page
 private int pageIndex=0;
 @Override
 protected void initUI() {
  super.initUI();
  setContentView(R.layout.main);
     initListView();
  currentUserTextView = (TextView) findViewById(R.id.wellcom_user);
  if (DataCenter.getCurrentUser() != null) {
   currentUserTextView
     .setText(DataCenter.getCurrentUser().getEmName());
  }
  
  newReportButton = (Button) findViewById(R.id.new_report);
  newReportButton.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
    //TODO sth here executeLoginTask();
   }

  });
  
  agencyTransactionButton = (Button) findViewById(R.id.my_agency_transaction);
  agencyTransactionButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
   }
  });
  
  messageButton = (Button) findViewById(R.id.my_message);
  messageButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
             //TODO sth here
   }
  });
  
  officialDocumentButton = (Button) findViewById(R.id.my_official_document);
  officialDocumentButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
    Intent intent = new Intent();
    intent.setClass(HomeActivity.this, ReportCenterActivity.class);
    startActivity(intent);
    
   }
  });
  
  prePageBtn=(Button)findViewById(R.id.pre_page_btn);
  prePageBtn.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
     to_Forward();
   }
  });
  
    nextPageBtn=(Button)findViewById(R.id.next_page_btn);
       nextPageBtn.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    to_Backward();}
  });
      
     
 }

      
 /**
   * 上一页
   */
  private void to_Forward() {
   pageIndex-- ;
   updataPage();
  }
 
  /**
   * 下一页
   */
  private void to_Backward() {
   pageIndex ++ ;
   updataPage();
  }
   
 private void initListView() {
        listView =(ListView) findViewById(R.id.agency_approval_title_listview);
        adapter = new SimpleListViewAdapter<AgencyApproval, AgencyApprovalReportTitleViewHolder>(
    this, R.layout.viewholder_agency_approval_report_title,
    AgencyApprovalReportTitleViewHolder.class);
         
  listView.setAdapter(adapter);
  executeTask();
 }

 private void executeTask() {
  if(approvalTask!=null && approvalTask.getStatus() != Status.RUNNING){
   return ;
  }
  approvalTask = new AgencyApprovalDetailTask(DataCenter.getCurrentUser().getEmId(),0,30,0);
  approvalTask.setTaskHost(approvalTaskHost);
  approvalTask.setProgress(new SimpleDialogProgress(this, "",
    getString(R.string.get_data)));
  approvalTask.execute();

 }

 private TaskHost approvalTaskHost = new TaskHost() {
  public void onPostExecute(int requestCode, BaseResult result) {
          adapter.setList(DataCenter.getCurrentApproval());

  }
 };


 private void checkButton(int size){
    if(pageIndex<= 0){
       prePageBtn.setEnabled(false);
   }else if(size-pageIndex*viewCount<=viewCount){
      nextPageBtn.setEnabled(false);
      }else{
       prePageBtn.setEnabled(true);
      nextPageBtn.setEnabled(true);
     }
 }
 
 private void updataPage()
    {
      List<AgencyApproval> list=DataCenter.getCurrentApproval();
     int size=list.size();
     adapter.setList(list.subList(pageIndex*viewCount,pageIndex*viewCount+viewCount));
     adapter.notifyDataSetChanged();
     checkButton(size);
    }
 
 
}

 

package com.ishow.kinmet.ui.adapter;

import java.util.List;

public abstract class SimpleListAdapter<T> extends BaseAdapter {

 protected List<T> list;

 public SimpleListAdapter() {
  super();
 }

 public SimpleListAdapter(List<T> list) {
  super();
  this.list = list;
 }

 @Override
 public int getCount() {
  if (list == null) {
   return 0;
  }
    return 10;//就是一页要显示的记录条数
 }

 @Override
 public T getItem(int position) {
  if (list == null || position < 0 || position >= list.size()) {
   return null;
  }
  return list.get(position);
 }

 public int getPosition(T item) {
  return list.indexOf(item);
 }

 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return position;
 }

 public List<T> getList() {
  return list;
 }

 public void remove(T obj){
  list.remove(obj);
  notifyDataSetChanged();
 }
 
 public void setList(List<T> list) {
  this.list = list;
  notifyDataSetChanged();
 }

}

 

下面是main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/default_width_height_fill_white_background"
    android:orientation="vertical" >

    <include
        android:id="@+id/user_info_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/include_user_info_top" >
    </include>

    <LinearLayout style="@style/default_layout_orientation" >
      
        <Button
            android:id="@+id/new_report"
            style="@style/index_button"
            android:text="@string/new_report" />

        <Button
            android:id="@+id/my_agency_transaction"
            style="@style/index_button"
            android:text="@string/my_agency_transaction" />

        <Button
            android:id="@+id/my_message"
            style="@style/index_button"
            android:text="@string/my_message" />

        <Button
            android:id="@+id/my_official_document"
            style="@style/index_button"
            android:text="@string/my_official_document" />
    </LinearLayout>

    <ListView
        android:id="@+id/agency_approval_title_listview"
        style="@style/default_width_height_fill_white_background"
        android:layout_width="match_parent"
        android:cacheColorHint="@android:color/transparent"
        android:descendantFocusability="blocksDescendants"
        android:divider="@drawable/divider_listview"
        android:dividerHeight="1.0dip"
        android:layout_height="300dp">
    </ListView>
   <LinearLayout style="@style/default_layout_orientation" >
      
    <Button android:id="@+id/pre_page_btn"
        style="@style/default_button"
        android:text="@string/pre_page" />
     <Button android:id="@+id/next_page_btn" style="@style/default_button" android:text="@string/next_page" />
      <TextView android:id="@+id/about_show_page"
          style="@style/default_textview"
          android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="标题页数" />
  </LinearLayout>

   
       

原创粉丝点击