Вы можете буквально перезаписать существующий курсор.
Например, рассмотрим следующий помощник по базам данных (с 3 методами, возвращающими курсор): -
public class DBHelper01 extends SQLiteOpenHelper {
public static final String DBNAME = "db01";
public static final int DBVERSION = 1;
public static final String TABLE_SPENDING = "spending";
public static final String TABLE_TRANSACTIONS = "transactions";
public static final String SPENDING_COL_ID = BaseColumns._ID;
public static final String SPENDING_COL_AMOUNT = "amount";
public static final String SPENDING_COL_CATEGORY = "catetgory";
public static final String TRANSACTIONS_COL_ID = BaseColumns._ID;
public static final String TRANSACTIONS_COl_DATE = "date";
public static final String TRANSACTION_COL_AMOUNT = "amount";
public static final String TRANSACTION_TYPE = "type";
public static final int TRANSACTION_TYPE_ADD = 0;
public static final int TRANSACTION_TYPE_DELETE = 1;
public static final int TRANSACTION_TYPE_UPDATE = 2;
String crt_spending_tbl = "CREATE TABLE IF NOT EXISTS " + TABLE_SPENDING + "(" +
SPENDING_COL_ID + " INTEGER PRIMARY KEY, " +
SPENDING_COL_AMOUNT + " REAL DEFAULT 0.0," +
SPENDING_COL_CATEGORY + " TEXT" +
")";
String crt_transactions_tbl = "CREATE TABLE IF NOT EXISTS " + TABLE_TRANSACTIONS + "(" +
TRANSACTIONS_COL_ID + " INTEGER PRIMARY KEY," +
TRANSACTIONS_COl_DATE + " TEXT DEFAULT CURRENT_TIMESTAMP," +
TRANSACTION_COL_AMOUNT + " REAL," +
TRANSACTION_TYPE + " INTEGER" +
")";
SQLiteDatabase mDB;
public DBHelper01(Context context) {
super(context, DBNAME, null, DBVERSION);
mDB = this.getWritableDatabase();
}
@Override
public synchronized void close() {
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(crt_spending_tbl);
db.execSQL(crt_transactions_tbl);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
public long addSpending(String category, Double amount) {
ContentValues cv = new ContentValues();
cv.put(SPENDING_COL_CATEGORY,category);
cv.put(SPENDING_COL_AMOUNT,amount);
return mDB.insert(TABLE_SPENDING,null,cv);
}
public long addTransactions(Double amount, int transaction_type ) {
ContentValues cv = new ContentValues();
cv.put(TRANSACTION_COL_AMOUNT,amount);
cv.put(TRANSACTION_TYPE,transaction_type);
return mDB.insert(TABLE_TRANSACTIONS,null,cv);
}
public Cursor getLatestTransaction() {
return mDB.query(TABLE_TRANSACTIONS,null,null,null,null,null,TRANSACTIONS_COl_DATE + " DESC","1");
}
public Cursor getTotalTransactionAmount() {
String[] columns = new String[]{"sum(" + TRANSACTION_COL_AMOUNT + ") AS summed_amounts" };
return mDB.query(TABLE_TRANSACTIONS,columns,null,null,null,null,null);
}
public Cursor getAllTransactions() {
return mDB.query(TABLE_TRANSACTIONS,null,null,null,null,null,TRANSACTIONS_COl_DATE + " ASC");
}
}
и следующее действие, которое вызывает 3 метода (после первоначального добавления некоторых данных), а затем выдает курсор после каждого использования: -
public class MainActivity extends AppCompatActivity {
DBHelper01 mDBHlpr;
Cursor mCsr; //<<<<<<<<<< THE CURSOR THAT WILL BE OVERIDDEN
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDBHlpr = new DBHelper01(this);
mDBHlpr.addSpending("TEST",123.45);
mDBHlpr.addTransactions(133.42,DBHelper01.TRANSACTION_TYPE_ADD);
mDBHlpr.addTransactions(127.45,DBHelper01.TRANSACTION_TYPE_DELETE);
mDBHlpr.addTransactions(111.11,DBHelper01.TRANSACTION_TYPE_UPDATE);
mDBHlpr.addTransactions(133.42,DBHelper01.TRANSACTION_TYPE_ADD);
mDBHlpr.addTransactions(127.45,DBHelper01.TRANSACTION_TYPE_DELETE);
mDBHlpr.addTransactions(111.11,DBHelper01.TRANSACTION_TYPE_UPDATE);
mCsr = mDBHlpr.getAllTransactions(); //<<<<<<<<<< First write to cursor
DatabaseUtils.dumpCursor(mCsr);
mCsr = mDBHlpr.getLatestTransaction(); //<<<<<<<<<< Overwrites the Cursor
DatabaseUtils.dumpCursor(mCsr);
mCsr = mDBHlpr.getTotalTransactionAmount(); // Overwrites the Cursor again
DatabaseUtils.dumpCursor(mCsr);
mCsr.close();
}
}
Результирующий вывод (подтверждающий, что каждый Курсор отличается): -
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@5345bd2c
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: 0 {
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: _id=1
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: date=2018-10-30 11:17:31
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: amount=133.42
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: type=0
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: }
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: 1 {
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: _id=2
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: date=2018-10-30 11:17:31
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: amount=127.45
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: type=1
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: }
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: 2 {
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: _id=3
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: date=2018-10-30 11:17:31
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: amount=111.11
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: type=2
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: }
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: 3 {
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: _id=4
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: date=2018-10-30 11:17:31
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: amount=133.42
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: type=0
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: }
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: 4 {
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: _id=5
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: date=2018-10-30 11:17:31
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: amount=127.45
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: type=1
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: }
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: 5 {
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: _id=6
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: date=2018-10-30 11:17:31
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: amount=111.11
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: type=2
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: }
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: <<<<<
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@5342f0e4
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: 0 {
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: _id=1
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: date=2018-10-30 11:17:31
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: amount=133.42
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: type=0
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: }
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: <<<<<
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@5342f6e8
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: 0 {
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: summed_amounts=743.96
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: }
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: <<<<<