ListView index not matching the Sqlite Database Index


#1

Hi all, I am facing problems in my deleting function for my app. When I swipe-delete the List View item, the item will disappear but when I refresh the app, the item is back there again. I checked my database id and list id, apparently they do not match at all. I “sort of” found out the solution, which is I have to look at the primary key value in the object for the particular list index while deleting the item. Problem is, how do I find the primary key value?
Any help will be appreciated greatly.

(ViewAll.js )

var swipeDelete = require("../../shared/utils/ios-swipe-delete");
var frameModule = require("ui/frame");
var dialogsModule = require("ui/dialogs");
var Sqlite = require("nativescript-sqlite");
var observableModule = require("data/observable")
var ObservableArray = require("data/observable-array").ObservableArray;
var view = require("ui/core/view");
var page;
var opage;
var oa = ObservableArray;
var db;
var sqlData;

var sqlRemove = function(idx) {
    console.log("sqlRemove:"+idx);

    new Sqlite("foo6.db", function(err, db) {
        console.log("Sqlite idx:", idx);

        db.execSQL("delete from quotes where id = ?", [idx], function(err, idx) {
            console.log("DELETE id:", idx);
        });
    });
}

exports.loaded = function(args) {
    page = args.object;
    opage = args.object;
    if (page.ios) {

    function Record(idx, author, quote) {
        this.idx = idx;
        this.author = author;
        this.quote = quote;
    }

    var listView = page.getViewById("quoteList");
    swipeDelete.enable(listView, function(index) {
        console.log("SwipeDelete: index="+index);
        console.dump(page.bindingContext);
        sqlData.quoteList.splice(index,1);
        sqlRemove(index);
    });

    new Sqlite("foo6.db", function(err, db) {
        if (err) {
            console.error("We failed to open database: ", err);
        } else {
            var ary = [];

            db.all("SELECT * FROM quotes", function(err1, rows) {

 if (err1) {
                    console.error("SELECT ERROR:", err1);

                } else {
                    console.log("SELECT SUCCESS:", err1);
                    for(var row in rows) {      //displaying the array
                        var idx = rows[row][0]; // index
                        var ath = rows[row][1]; // author
                        var qot = rows[row][2]; // quote
                        //var r = new Record(idx , ath , qot);
                        var r = new Record(idx , ath , qot);

                        //console.log(idx+". "+author);
                        //console.log(JSON.stringify(r));

                        ary.push(r);
                        //ary.push(rows[row]);
                    }

                    sqlData = new observableModule.fromObject({
                        "quoteList": new ObservableArray(ary)
                    });
                }
            });
        }
    });

    console.log("1. "+sqlData);
//  console.log("2. "+JSON.stringify(sqlData));

    page.bindingContext = sqlData;
    opage.bindingContext = sqlData;
    //page.bindingContext = pageData;
}};
Error shown in console

CONSOLE LOG file:///app/views/viewAll/viewAll.js:16:13: sqlRemove:13
CONSOLE LOG file:///app/views/viewAll/viewAll.js:19:14: Sqlite idx: 13
CONSOLE LOG file:///app/views/viewAll/viewAll.js:22:15: DELETE id: 0


#2

In the sqlRemove(idx) you need to use the idx which is the index in the listviews items - in this case you are using an ObservableArray so you can use the following: https://docs.nativescript.org/cookbook/data/observable-array#get-item-at-specified-index-using-getitemindex-method

Once you use the ObservableArray getItem() method you have the actual item in the array. So you can access it’s properties using simple dot notation.

var theActualItem = sqlData.quoteList.getItem(idx);
var author = theActualItem.author; // this would be the items author property value