简体   繁体   中英

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

I'm new to android programming. I'm still practicing and making this simple app. I want to get 2 rows per refresh from SQLite Database and display on a listview.

But what I am getting now is the first 2 rows again and again.

My database already has data with 6 rows.

What I don't know is how to pass offset to the Database Operation and how to get the next 2 rows.

Thank you so much for your time. I hope someone can help me.

Pls see my code below:

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) {

}

}

You are on the right track :)

So the 2 is the limit which is set on the query. You can also set offset,query in that place. You can keep a counter for refresh count and use it to create the offset.

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

or

 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 

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM