Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ANDROID:使用滑动刷新从SQLite数据库获取n行_Java_Android_Sqlite_Android Sqlite_Swiperefreshlayout - Fatal编程技术网

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

Java ANDROID:使用滑动刷新从SQLite数据库获取n行,java,android,sqlite,android-sqlite,swiperefreshlayout,Java,Android,Sqlite,Android Sqlite,Swiperefreshlayout,我是android编程新手。我还在练习和制作这个简单的应用程序。 我希望每次刷新时从SQLite数据库中获得2行,并显示在listview上 但我现在得到的是前两行,一次又一次 我的数据库中已有6行数据 我不知道的是如何将偏移量传递给数据库操作,以及如何获取接下来的两行 非常感谢您抽出时间。我希望有人能帮助我 请看下面我的代码: DisplayItem.java public class DisplayItem extends AppCompatActivity implements Swipe

我是android编程新手。我还在练习和制作这个简单的应用程序。 我希望每次刷新时从SQLite数据库中获得2行,并显示在listview上

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

我的数据库中已有6行数据

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

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

请看下面我的代码:

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);
}

}
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


您好,非常感谢您的回复:)我仍然不知道如何将DisplayItem.java的偏移量传递给DatabaseOperation.javaoncreate期间可能首先计算数据库中的条目总数。将该值发送到您的fetchItems。对于每个刷新增量,都有一个计数器,它会记住条目的总数,并根据计数器设置偏移量。将该偏移量值作为参数传递给displaySwipeInfo方法,并在queryHi中使用它,再次感谢您的回复。我真的很难过,因为我不知道如何将偏移量值传递给displaySwipeInfo方法。你能帮我查一下密码吗?非常感谢你!公共游标显示swipeinfo(SQLiteDatabase db,int offset){…},然后在fetchItems(){..computed offset..Cursor display\u Cursor\u swipe=db\u op\u sw.displaySwipeInfo(db,offset);..}中检查计算的偏移量是否大于总计数。如果将偏移量重置为0
 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.