Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android listview scroll滑屏顯示sqlite分頁,類似聊天記錄,listview動態添加sqlite分頁數據

android listview scroll滑屏顯示sqlite分頁,類似聊天記錄,listview動態添加sqlite分頁數據

編輯:關於Android編程

前言:最近做基於openfire聊天(仿QQ、微信)翻頁查看聊天記錄,為此做了根據時間倒序查看聊天記錄,先聲明這demo是根據id來倒序(原理和時間倒序一樣)

1,主界面MainActivity.class
public class MainActivity extends Activity implements OnScrollListener{
	private ProgressBar loadInfo;
	private ListView listView;
	private LinearLayout loadLayout;
	private ArrayList items;
	private DatabaseService service;
	private int currentPage = 1; //默認在第一頁
	private static final int lineSize =10;    //每次顯示數 
	private int allRecorders = 0;  //全部記錄數
	private int pageSize = 1;  //默認共一頁
	private Aleph0 baseAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView = (ListView) findViewById(R.id.listview);
		service = new DatabaseService(this);
		//創建一個角標線性布局用來顯示正在加載
		loadLayout = new LinearLayout(this);
		loadLayout.setGravity(Gravity.CENTER);
		//定義一個ProgressBar表示“正在加載”
		loadInfo = new ProgressBar(this,null,android.R.attr.progressBarStyleSmall);
		//增加組件 
		loadLayout.addView(loadInfo, new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		//增加到listView頭部
		listView.addHeaderView(loadLayout);
		listView.setOnScrollListener(this);
		
		showAllData();
		
		
	}
	
	/**
	 * 讀取全部數據
	 */
	public void showAllData(){
		
		allRecorders = service.getCount();
		//計算總頁數
		pageSize = (allRecorders + lineSize -1) / lineSize;  
		items = service.getAllItems(currentPage, lineSize);
		Collections.reverse(items);  //倒序
		baseAdapter = new Aleph0();
		listView.setAdapter(baseAdapter);
		listView.setSelection(items.size());//直接定位到最底部
	}
	int firstItem=-1;

	@Override
	public void onScroll(AbsListView absView, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		firstItem=firstVisibleItem;
	}
	@Override
	public void onScrollStateChanged(AbsListView view, int scorllState) {
		if (firstItem==0&& currentPage < pageSize&& scorllState == OnScrollListener.SCROLL_STATE_IDLE) {// 不再滾動
			Log.e(log, 滑到第一位);
			currentPage++;
			// 增加數據
			appendDate();
		}
	}
	
	/**
	 * 增加數據
	 */
	private void appendDate(){
		final ArrayList additems = service.getAllItems(currentPage, lineSize);
		Collections.reverse(additems);
		baseAdapter.setCount(baseAdapter.getCount() + additems.size());
		//判斷,如果到了最末尾則去掉“正在加載”
		if(allRecorders == baseAdapter.getCount()){
			listView.removeHeaderView(loadLayout);
		}
		items.addAll(0, additems);
		
		baseAdapter.notifyDataSetChanged();
		listView.setSelection(additems.size());
		
	}
	
	class Aleph0 extends BaseAdapter {
		int count = lineSize; 

		public int getCount() {
			return count;
		} 
		
		public void setCount(int count){
			this.count = count;
		}

		public Object getItem(int pos) {
			return pos;
		}

		public long getItemId(int pos) {
			return pos;
		}

		public View getView(int pos, View v, ViewGroup p) {
			TextView view = new TextView(MainActivity.this);
			view.setTextSize(60);
			if(items != null){
				view.setText(items.get(pos));
			}else{
				view.setText(pos);
			}
			return view;
		}
	}
	
	

}
2,主界面布局文件activity_main.xml


    


3,數據庫操作類
public class DatabaseService { 

	private Context mContext;
	private MyDBOpenHelper dbHelper;
	

	public DatabaseService(Context context) {
		mContext = context;
		dbHelper = new MyDBOpenHelper(mContext);
	}

	// 添加
	public void insert(String title) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		String sql = insert into database(title) values(?);
		db.execSQL(sql, new String[] { title });
	}

	// 刪除
	public void delete(String title) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		String sql = delete from database where title = ?;
		db.execSQL(sql, new String[] { title });
	}

	// 查找
	public ArrayList find(int id) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		String sql = select * from database where _id = ? ;
		Cursor c = db.rawQuery(sql, new String[] { String.valueOf(id) });
		ArrayList titles = new ArrayList();
		if (c.moveToNext()) {
			String title = c.getString(c
					.getColumnIndexOrThrow(MyDBOpenHelper.FIELD_TITLE));
			titles.add(title);
			return titles;
		}
		// 不用忘記關閉Cursor。
		c.close();
		return null;
	}

	// 更新
	public void upDate(int id, String title) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		String sql = update database set title =? where _id = ?;
		db.execSQL(sql,
				new String[] { String.valueOf(title), String.valueOf(id) });
	}

	// 查詢記錄的總數
	public int getCount() {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		String sql = select count(*) from database;
		Cursor c = db.rawQuery(sql, null);
		c.moveToFirst();
		int length = c.getInt(0);
		c.close();
		return length;
	}

	/**
	 * 分頁查詢
	 * 
	 * @param currentPage 當前頁
	 * @param pageSize 每頁顯示的記錄
	 * @return 當前頁的記錄
	 */
	public ArrayList getAllItems(int currentPage, int pageSize) {
		int firstResult = (currentPage - 1) * pageSize;
		int maxResult = currentPage * pageSize;
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		String sql = select * from database order by  _id desc limit ?,? ;
		Cursor mCursor = db.rawQuery(
				sql,
				new String[] { String.valueOf(firstResult),
						String.valueOf(pageSize) });
		ArrayList items = new ArrayList();
		int columnCount  = mCursor.getColumnCount();
		while (mCursor.moveToNext()) {
			String item = mCursor.getString(mCursor
					.getColumnIndexOrThrow(MyDBOpenHelper.FIELD_TITLE));
			items.add(item);
			
		}
		//不要關閉數據庫
		return items;
	}
}
4,效果圖 \\\

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved