SQL data load page

The last two days saw sql paging load the data, and then hands himself knocked again, today, finishing the next thoughts, then write a blog to deepen under the impression.
The entire project code review it again. The process is divided into roughly:

  1. Create a new class that inherits SQLiteOpenHelper. Here we can design table of field names, in general, we will use another class to hold these constant field names, probably seemed more layered and organized. First execution onCreate () method after creating database objects, where we can watch the design come out, then execSQL () method is executed. Here say something, to operate the database, we can either use the API tools provided by Android, but also can use SQL syntax. To me, prefer to use SQL statements, SQL statements appear wrong for a long time if the did not solve it, then try to use the API to operate. (I had really encountered this situation). Further, in addition to the establishment of a class to hold constant field, we can also be based on a database managed by the operation, we generally do not directly SQLiteOpenHelper class inherits instance, but to instantiate objects managed by singleton class, so ensures that only one instance of an object, not a waste of memory.
  2. Inside the main movable operation. We inserting data into the database in the main event there, we simply use a for loop to and from dozens of sets of data inserted into the table and then query the data to show the phone to the inside pages. Here we have the data by clicking on "Query Data" to insert the data show, the first page is displayed on the first page of the page data is loaded inside, note that this is part two unrelated, I'm here achieve first is to click on a button to display data, and then another page to load the data, but not too simple page
  3. 123
  4. 点击“查询按钮”显示全部数据。通过SQL语句来查询表中的全部数据,然后在管理类里面封装一个方法可以执行SQL查询语句,并且返回Cursor结果集,要想展示出来,我们还需要一个实体类和数据库的字段一一对应,所以再新建一个实体类,我们这里打算用ListView来展示数据。所以我们还需要一个适配器。总结下来,用listView控件展示数据的步骤就是:实体类–>适配器—>操作的类(在哪一个类里面使用ListView控件)。适配器里面我们具体要写的就是getView()方法。最后在操作的类里面实例化适配器对象,然后通过ListView控件的setAdapter()方法将数据与控件联系起来。这里我们也可以用API来查询数据表,这样我们得到的直接就是Cursor结果集。
  5. 分页加载数据的操作。分页加载数据,我们首先要确定一共有多少条数据,设置每页需要展示多少条数据,还要一空需要多少页。比如这里我设置一页展示两条数据,所以,就出现了图片的那样,如果你滚动下数据就可以发现特点了。确定多少条数据的方法我封装在了管理类里面,实际上就是,把实现方法集中在一个类里面,这样的话,整体结构显的清晰。实际上此方法就是查询数据库里面的所有数据返回Cursor对象,然后调用size()方法得到数据总数。得到数据总数之后就可以得到总页数。我们还需要定义默认为第一页,然后就是查询第一页的数据。然后在管理类里面封装一个方法来返回第一页的数据。
  6. 滚动加载数据为ListView控件设置监听事件,我们需要在两个方法里面添加逻辑,简单来说就是本页数据加载完毕后继续加载第二页直到加载完全部数据。

项目整体的过程已经分析了一遍,在这里面管理类实际上是核心部分。这里我把项目demo贡献一下。

Published 37 original articles · won praise 10 · views 10000 +

Guess you like

Origin blog.csdn.net/OneLinee/article/details/78882103