Android实现简单的分批加载ListView
每次滑动至底端,从数据库中获取10条数据,并加载于ListView中
数据库
packagecom.example.listviewbatchloading; importandroid.content.Context; importandroid.database.sqlite.SQLiteDatabase; importandroid.database.sqlite.SQLiteDatabase.CursorFactory; importandroid.database.sqlite.SQLiteOpenHelper; publicclassPersonDbextendsSQLiteOpenHelper{ publicPersonDb(Contextcontext){ super(context,"creature",null,1); //TODOAuto-generatedconstructorstub } @Override publicvoidonCreate(SQLiteDatabasedb){ //TODOAuto-generatedmethodstub db.execSQL("createtablepeople(_idintegerprimarykeyautoincrement,namechar,numberchar)"); } @Override publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){ //TODOAuto-generatedmethodstub db.execSQL("droptablepeople"); onCreate(db); } }
数据库的业务封装,其中获取更多数据的是核心代码
packagecom.example.listviewbatchloading; /** *数据库的业务封装类 */ importjava.util.ArrayList; importjava.util.List; importandroid.content.ContentValues; importandroid.content.Context; importandroid.database.Cursor; importandroid.database.sqlite.SQLiteDatabase; publicclassPersonList{ PersonDbpersonDb; publicPersonList(Contextcontext){ this.personDb=newPersonDb(context); } /** *获取一定条目的数据 *@paramstartIndex *开始取数据的位置 *@paramnum *取多少条数据 */ publicList<Person>getMoreDatas(intstartIndex,intnum){ List<Person>list=newArrayList<Person>(); SQLiteDatabasedb=personDb.getWritableDatabase(); Cursorcursor=db.rawQuery("selectname,numberfrompeopleorderby_iddesclimit?,?", newString[]{startIndex+"",num+""}); while(cursor.moveToNext()){ Personperson=newPerson(); person.setName(cursor.getString(cursor.getColumnIndex("name"))); person.setNumber(cursor.getString(cursor.getColumnIndex("number"))); list.add(person); } cursor.close(); db.close(); returnlist; } /** *添加数据库条目 *@paramname *@paramnumber */ publicvoidadd(Stringname,Stringnumber){ SQLiteDatabasedb=personDb.getWritableDatabase(); ContentValuescv=newContentValues(); cv.put("name",name); cv.put("number",number); db.insert("people",null,cv); db.close(); } }
功能实现
packagecom.example.listviewbatchloading; importjava.util.ArrayList; importjava.util.List; importandroid.app.Activity; importandroid.os.Bundle; importandroid.os.Handler; importandroid.os.Message; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.AbsListView; importandroid.widget.AbsListView.OnScrollListener; importandroid.widget.BaseAdapter; importandroid.widget.ListView; importandroid.widget.TextView; importandroid.widget.Toast; publicclassMainActivityextendsActivity{ privateListViewlv; privateList<Person>datas=newArrayList<Person>(); privatestaticintPERPAGE=10;//每页加载数目 privatestaticfinalintFINISH=0;//数据加载完成 privateList<Person>moreDatas;//每次加载的数据 privateMyAdapteradapter; privatePersonListpersonList; //若数据较多,耗时较长,数据加载完成时,发送FINISH至handler,并通知ListView更新数据 privateHandlerhandler=newHandler(){ publicvoidhandleMessage(android.os.Messagemsg){ switch(msg.what){ caseFINISH: if(moreDatas.size()!=0){ System.out.println(moreDatas.toString()); adapter.notifyDataSetChanged(); }else{ Toast.makeText(MainActivity.this,"没有更多数据",Toast.LENGTH_SHORT).show(); } break; default: break; } }; }; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); initEvent(); } /** *为ListView添加滚动监听事件,但滚动至最后一行时,加载更多数据 */ privatevoidinitEvent(){ lv.setOnScrollListener(newOnScrollListener(){ @Override publicvoidonScrollStateChanged(AbsListViewview,intscrollState){ //TODOAuto-generatedmethodstub if(scrollState==OnScrollListener.SCROLL_STATE_IDLE){ intlastVisiblePosition=lv.getLastVisiblePosition(); if(lastVisiblePosition==datas.size()-1){ initData(); System.out.println("加载更多数据"); } } } @Override publicvoidonScroll(AbsListViewview,intfirstVisibleItem,intvisibleItemCount,inttotalItemCount){ //TODOAuto-generatedmethodstub } }); } privatevoidinitView(){ setContentView(R.layout.activity_main); lv=(ListView)findViewById(R.id.lv); personList=newPersonList(getApplicationContext()); adapter=newMyAdapter(); lv.setAdapter(adapter); } /** *在子线程中加载数据,避免主线程阻塞 */ privatevoidinitData(){ newThread(){ publicvoidrun(){ //加载更多数据 moreDatas=personList.getMoreDatas(datas.size(),PERPAGE); datas.addAll(moreDatas);//把一个容器的所有数据加进来 //取数据完成,发消息通知取数据完成 handler.obtainMessage(FINISH).sendToTarget(); }; }.start(); } privateclassItemView{ privateTextViewtv_name; privateTextViewtv_num; } /** *ListView的适配器 *@authorlian * */ privateclassMyAdapterextendsBaseAdapter{ @Override publicintgetCount(){ //TODOAuto-generatedmethodstub returndatas.size(); } @Override publicObjectgetItem(intposition){ //TODOAuto-generatedmethodstub returnnull; } @Override publiclonggetItemId(intposition){ //TODOAuto-generatedmethodstub return0; } @Override publicViewgetView(intposition,ViewconvertView,ViewGroupparent){ ItemViewitemView=null; if(convertView==null){ itemView=newItemView(); convertView=View.inflate(getApplicationContext(),R.layout.item_lv,null); itemView.tv_name=(TextView)convertView.findViewById(R.id.tv_name); itemView.tv_num=(TextView)convertView.findViewById(R.id.tv_num); convertView.setTag(itemView); }else{ itemView=(ItemView)convertView.getTag(); } Personperson=datas.get(position); itemView.tv_name.setText(person.getName()); itemView.tv_num.setText(person.getNumber()); returnconvertView; } } }
其他Person的JavaBean,以及布局文件,不在赘述
以上就是本文的全部内容,希望对大家的学习有所帮助。