ANDROID:使用滑动刷新从 SQLite 数据库中获取 n 行

ANDROID: Get n rows from SQLite Database using Swipe to Refresh

我是 android 编程新手。我仍在练习并制作这个简单的应用程序。 我想从 SQLite 数据库每次刷新获取 2 行并显示在列表视图上。

但我现在得到的是前两行一次又一次。

我的数据库已经有 6 行数据。

我不知道如何将偏移量传递给数据库操作以及如何获取接下来的 2 行。

非常感谢您抽出宝贵时间。我希望有人能帮助我。

请看下面我的代码:

DisplayItem.java

public class DisplayItem extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

private String TAG = DisplayItem.class.getSimpleName();
private SwipeRefreshLayout swipeRefreshLayout;

private ListView list_view;
private SwipeAdapter adapter;
private List<Item> itemList;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display_item);

    list_view = (ListView) findViewById(R.id.lvDisplay);

    itemList = new ArrayList<>();
    adapter = new SwipeAdapter(this, itemList);
    list_view.setAdapter(adapter);

        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                fetchItems();
            }
    });

}

@Override
public void onRefresh() {
    fetchItems();
}

public void fetchItems() {
    swipeRefreshLayout.setRefreshing(true);

    DatabaseOperations db_op_sw = new DatabaseOperations(this);
    SQLiteDatabase db = db_op_sw.getReadableDatabase();
    Cursor display_cursor_swipe = db_op_sw.displaySwipeInfo(db);

    String name;
    int id, qty, price;
    int offSet = 0;

    if (display_cursor_swipe.moveToFirst()) {

        do {
            id = display_cursor_swipe.getInt(display_cursor_swipe.getColumnIndex(ItemContract.ItemEntry.ID));
            name = display_cursor_swipe.getString(display_cursor_swipe.getColumnIndex(ItemContract.ItemEntry.NAME));
            qty = display_cursor_swipe.getInt(display_cursor_swipe.getColumnIndex(ItemContract.ItemEntry.QTY));
            price = display_cursor_swipe.getInt(display_cursor_swipe.getColumnIndex(ItemContract.ItemEntry.PRICE));
            Item item = new Item(id, name, qty, price);
            itemList.add(item);
            offSet = offSet + id;
        } while (display_cursor_swipe.moveToNext());

        adapter.notifyDataSetChanged();
    }

    swipeRefreshLayout.setRefreshing(false);
}

}

DatabaseOperations.java

public class DatabaseOperations extends SQLiteOpenHelper {

private static final int DB_VERSION = 1;
private static final String DB_NAME = "item_info.db";
private static final String CREATE_QUERY = "create table " + ItemContract.ItemEntry.TABLE_NAME +
                                            "(" + ItemContract.ItemEntry.ID + " text,"
                                            + ItemContract.ItemEntry.NAME + " text,"
                                            + ItemContract.ItemEntry.QTY + " integer,"
                                            + ItemContract.ItemEntry.PRICE + " integer);";

public DatabaseOperations(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    Log.d("Database Operations", "Database successfully created");
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_QUERY);
    Log.d("Database Operations", "Table successfully created");
}

 public Cursor displaySwipeInfo(SQLiteDatabase db) {

    String[] projections = {ItemContract.ItemEntry.ID, ItemContract.ItemEntry.NAME,
            ItemContract.ItemEntry.QTY, ItemContract.ItemEntry.PRICE};

    Cursor display_cursor_swipe = db.query(ItemContract.ItemEntry.TABLE_NAME, projections, null, null, null, null, null, null, "2");
    Log.d("Database Operations", "Viewed row");
    return display_cursor_swipe;
}

 @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

你走在正确的轨道上:)

所以 2 是在查询中设置的限制。你也可以在那个地方设置偏移量,查询。您可以为刷新计数保留一个计数器并使用它来创建偏移量。

 Cursor display_cursor_swipe = db.query(ItemContract.ItemEntry.TABLE_NAME, projections, null, null, null, null, null, null, offset+",2"); //offset,limit

 Cursor display_cursor_swipe = db.query(ItemContract.ItemEntry.TABLE_NAME, projections, null, null, null, null, null, null, "limit "+2+",offset "+offset);// limit 2 offset 3 

you could resort to db.rawQuery(SQLSTATEMENT); // SQLSTATEMENT is select statement in string which has LIMIT,OFFSET set on it.