SQLite Update不适用于列表

时间:2017-12-23 18:46:45

标签: android sqlite android-sqlite

我编写这段代码,逻辑应该像列表视图上的项目单击一个int数据库更新为1,如果再次单击再次更新为0但它只更新为0

listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView expandableListView,  
        View view, 
        int groupPosition, 
        int childPosition, 
        long id) {
            int num = dbHelper.getActive(child.getTourId(),child.getSiteName());
            if (num == 1){
                Log.i("database",num+"");
                dbHelper.setDeAvtive(child.getSiteName(),child.getTourId());

            } else if (num == 0){
                Log.i("database",num+"");
                dbHelper.setActive(child.getSiteName(),child.getTourId());                   
            }
            return true;
            }
    });
}

这是我的数据库类

活跃课程

 public void setActive(String name,int id){
    SQLiteDatabase db = getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(ChildTour.ACTIVE,1);
    db.update(name,values,ChildTour.ACTIVE+"="+id,null);
    Log.i("database",id+" Activated");

deActive Class

public void setDeAvtive(String name,int id){
    SQLiteDatabase db = getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(ChildTour.ACTIVE,0);
    db.update(name,values,ChildTour.ACTIVE+"="+id,null);
    Log.i("database",id+" DeActivated");

getActive class

 public int getActive(int tourId,String name) {
    SQLiteDatabase db = getReadableDatabase();
    int activeCode = 0;
    Cursor cursor = null;
    try {
        cursor = db.rawQuery("SELECT " + ChildTour.ACTIVE + " FROM "
                + name + " WHERE " + ChildTour.TOUR_ID + " = " + tourId, null);
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            activeCode= cursor.getInt(cursor.getColumnIndex(ChildTour.ACTIVE));
            Log.i("databse", "UpdateCode is: " + updateCode);

        }
        return activeCode;
    } finally {
        cursor.close();
    }
}

这是我点击5次的日志

12-24 09:32:02.091 5032-5032/app.mma.introsliderproject I/database: 3 Activated
12-24 09:32:02.331 5032-5032/app.mma.introsliderproject I/database: 0
12-24 09:32:02.331 5032-5032/app.mma.introsliderproject I/database: 3 Activated
12-24 09:32:02.571 5032-5032/app.mma.introsliderproject I/database: 0
12-24 09:32:02.571 5032-5032/app.mma.introsliderproject I/database: 3 Activated
12-24 09:32:02.761 5032-5032/app.mma.introsliderproject I/database: 0
12-24 09:32:02.761 5032-5032/app.mma.introsliderproject I/database: 3 Activated
12-24 09:32:02.991 5032-5032/app.mma.introsliderproject I/database: 0
12-24 09:32:02.991 5032-5032/app.mma.introsliderproject I/database: 3 Activated
12-24 09:32:03.171 5032-5032/app.mma.introsliderproject I/database: 0
12-24 09:32:03.171 5032-5032/app.mma.introsliderproject I/database: 3 Activated

1 个答案:

答案 0 :(得分:2)

我很抱歉我们在setActive和setDeActive上的新内容更新代码应该像这样db.update(name,values,ChildTour.TOUR_ID+"="+id,null); 再次对不起