当前位置: 代码迷 >> SQL >> android listview scroll滑屏展示sqlite分页,类似聊天记录,listview动态添加sqlite分页数据
  详细解决方案

android listview scroll滑屏展示sqlite分页,类似聊天记录,listview动态添加sqlite分页数据

热度:78   发布时间:2016-05-05 10:58:32.0
android listview scroll滑屏显示sqlite分页,类似聊天记录,listview动态添加sqlite分页数据

前言:最近做基于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<String> 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<String> 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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ListView        android:id="@+id/listview"        android:layout_width="fill_parent"        android:layout_height="fill_parent" /></LinearLayout>
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<String> 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<String> titles = new ArrayList<String>();		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<String> 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<String> items = new ArrayList<String>();		int columnCount  = mCursor.getColumnCount();		while (mCursor.moveToNext()) {			String item = mCursor.getString(mCursor					.getColumnIndexOrThrow(MyDBOpenHelper.FIELD_TITLE));			items.add(item);					}		//不要关闭数据库		return items;	}}
4,效果图


5,下载地址:http://download.csdn.net/detail/shi508181017/8297955

  相关解决方案