Android 我是否使用正确的方法从数据库中读取数据

Android 我是否使用正确的方法从数据库中读取数据,android,gridview,android-sqlite,sqliteopenhelper,Android,Gridview,Android Sqlite,Sqliteopenhelper,我有一个新闻应用程序,该应用程序有一个自定义gridView,其中包括appTitle、AppNewNewCount 在我的数据库表中,我有一个列,它的名称是new,每当向表中添加一个新字段时,它的值就变成1,每当用户单击一条新闻时,它的值就变成0 在具有gridView的活动中,我使用了此函数,该函数指向我的SQLiteOpenHelper数据库类: 我使用以下代码调用函数: Toast.makeText(getApplicationContext(), String.valueOf(getN

我有一个新闻应用程序,该应用程序有一个自定义gridView,其中包括appTitle、AppNewNewCount

在我的数据库表中,我有一个列,它的名称是new,每当向表中添加一个新字段时,它的值就变成1,每当用户单击一条新闻时,它的值就变成0

在具有gridView的活动中,我使用了此函数,该函数指向我的SQLiteOpenHelper数据库类:

我使用以下代码调用函数:

Toast.makeText(getApplicationContext(), String.valueOf(getNumberofNews("Political")), Toast.LENGTH_LONG).show();
函数的代码为:

private int getNumberofNews(String categoryRequested) {
        DB_New_Handler obj = null;
        try{
            obj = new DB_News_Handler(getApplicationContext());
            int newNews = obj.getNumberOfNewNews(categoryRequested);
            if (newNews > 0) {
                return newRings;
            }
        }catch (Exception ex){
            Log.d("getNumberofNews: ", ex.toString());
        }finally{
            if (obj != null){
                obj.close();
            }
        }
        return 0;
    }
这是我在SQLiteOpenHelper中用来收集新新闻数量的函数:

public int getNumberOfNewNews(String categoryName) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try{
            db = this.getReadableDatabase();

            cursor = db.query(TABLE_NEWS, new String[] { KEY_ID,
                KEY_NEWS_ID, KEY_NAME, KEY_CATEGORY, KEY_Package_Id,
                KEY_FAVORITE, KEY_DATE, KEY_NEW_ADDED }, KEY_CATEGORY + "=?",
                new String[] { categoryName }, null, null, null, null);

            if (cursor != null)
                cursor.moveToFirst();

            Log.d(categoryName + " has ", cursor.getCount() + " new News");
            return cursor.getCount();
        }catch (Exception ex){
            Log.d("getNumberOfNewNews: ", ex.toString());
        }finally{
            if (cursor != null ){
                cursor.close();
            }
            if (db != null){
                db.close();
            }
        }
        return 0;
    }

,现在的问题是getNumberOfNewNews函数。当假定它返回0时,它总是返回1。

在查询新闻计数后编写数据库更新语句。您的更新应将“密钥添加”字段更改为0。我在您的方法中添加了一条注释,您应该在其中编写更新查询

public int getNumberOfNewNews(String categoryName) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try{
            db = this.getReadableDatabase();

            cursor = db.query(TABLE_NEWS, new String[] { KEY_ID,
                KEY_NEWS_ID, KEY_NAME, KEY_CATEGORY, KEY_Package_Id,
                KEY_FAVORITE, KEY_DATE, KEY_NEW_ADDED }, KEY_CATEGORY + "=?",
                new String[] { categoryName }, null, null, null, null);

            if (cursor != null)
                cursor.moveToFirst();

//Write the update method here


            Log.d(categoryName + " has ", cursor.getCount() + " new News");
            return cursor.getCount();
        }catch (Exception ex){
            Log.d("getNumberOfNewNews: ", ex.toString());
        }finally{
            if (cursor != null ){
                cursor.close();
            }
            if (db != null){
                db.close();
            }
        }
        return 0;
    }

在查询新闻计数后,编写数据库更新语句。您的更新应将“密钥添加”字段更改为0。我在您的方法中添加了一条注释,您应该在其中编写更新查询

public int getNumberOfNewNews(String categoryName) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try{
            db = this.getReadableDatabase();

            cursor = db.query(TABLE_NEWS, new String[] { KEY_ID,
                KEY_NEWS_ID, KEY_NAME, KEY_CATEGORY, KEY_Package_Id,
                KEY_FAVORITE, KEY_DATE, KEY_NEW_ADDED }, KEY_CATEGORY + "=?",
                new String[] { categoryName }, null, null, null, null);

            if (cursor != null)
                cursor.moveToFirst();

//Write the update method here


            Log.d(categoryName + " has ", cursor.getCount() + " new News");
            return cursor.getCount();
        }catch (Exception ex){
            Log.d("getNumberOfNewNews: ", ex.toString());
        }finally{
            if (cursor != null ){
                cursor.close();
            }
            if (db != null){
                db.close();
            }
        }
        return 0;
    }

在查询新闻计数后,编写数据库更新语句。您的更新应将“密钥添加”字段更改为0。我在您的方法中添加了一条注释,您应该在其中编写更新查询

public int getNumberOfNewNews(String categoryName) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try{
            db = this.getReadableDatabase();

            cursor = db.query(TABLE_NEWS, new String[] { KEY_ID,
                KEY_NEWS_ID, KEY_NAME, KEY_CATEGORY, KEY_Package_Id,
                KEY_FAVORITE, KEY_DATE, KEY_NEW_ADDED }, KEY_CATEGORY + "=?",
                new String[] { categoryName }, null, null, null, null);

            if (cursor != null)
                cursor.moveToFirst();

//Write the update method here


            Log.d(categoryName + " has ", cursor.getCount() + " new News");
            return cursor.getCount();
        }catch (Exception ex){
            Log.d("getNumberOfNewNews: ", ex.toString());
        }finally{
            if (cursor != null ){
                cursor.close();
            }
            if (db != null){
                db.close();
            }
        }
        return 0;
    }

在查询新闻计数后,编写数据库更新语句。您的更新应将“密钥添加”字段更改为0。我在您的方法中添加了一条注释,您应该在其中编写更新查询

public int getNumberOfNewNews(String categoryName) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try{
            db = this.getReadableDatabase();

            cursor = db.query(TABLE_NEWS, new String[] { KEY_ID,
                KEY_NEWS_ID, KEY_NAME, KEY_CATEGORY, KEY_Package_Id,
                KEY_FAVORITE, KEY_DATE, KEY_NEW_ADDED }, KEY_CATEGORY + "=?",
                new String[] { categoryName }, null, null, null, null);

            if (cursor != null)
                cursor.moveToFirst();

//Write the update method here


            Log.d(categoryName + " has ", cursor.getCount() + " new News");
            return cursor.getCount();
        }catch (Exception ex){
            Log.d("getNumberOfNewNews: ", ex.toString());
        }finally{
            if (cursor != null ){
                cursor.close();
            }
            if (db != null){
                db.close();
            }
        }
        return 0;
    }
阿拉法特是对的 另外,你必须用两个选择参数来查询你的数据库…例如

public int getNumberOfNewNews(String categoryName) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try{
            db = this.getReadableDatabase();

            cursor = db.query(TABLE_NEWS, new String[] { KEY_ID,
                KEY_NEWS_ID, KEY_NAME, KEY_CATEGORY, KEY_Package_Id,
                KEY_FAVORITE, KEY_DATE, KEY_NEW_ADDED }, KEY_CATEGORY + "=? AND ",
                KEY_NEW_ADDED + "=? ",new String[] { categoryName, "1" }, null, null, null, null);

            if (cursor != null)
                cursor.moveToFirst();

//Write the update method here


            Log.d(categoryName + " has ", cursor.getCount() + " new News");
            return cursor.getCount();
        }catch (Exception ex){
            Log.d("getNumberOfNewNews: ", ex.toString());
        }finally{
            if (cursor != null ){
                cursor.close();
            }
            if (db != null){
                db.close();
            }
        }
阿拉法特是对的 另外,你必须用两个选择参数来查询你的数据库…例如

public int getNumberOfNewNews(String categoryName) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try{
            db = this.getReadableDatabase();

            cursor = db.query(TABLE_NEWS, new String[] { KEY_ID,
                KEY_NEWS_ID, KEY_NAME, KEY_CATEGORY, KEY_Package_Id,
                KEY_FAVORITE, KEY_DATE, KEY_NEW_ADDED }, KEY_CATEGORY + "=? AND ",
                KEY_NEW_ADDED + "=? ",new String[] { categoryName, "1" }, null, null, null, null);

            if (cursor != null)
                cursor.moveToFirst();

//Write the update method here


            Log.d(categoryName + " has ", cursor.getCount() + " new News");
            return cursor.getCount();
        }catch (Exception ex){
            Log.d("getNumberOfNewNews: ", ex.toString());
        }finally{
            if (cursor != null ){
                cursor.close();
            }
            if (db != null){
                db.close();
            }
        }
阿拉法特是对的 另外,你必须用两个选择参数来查询你的数据库…例如

public int getNumberOfNewNews(String categoryName) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try{
            db = this.getReadableDatabase();

            cursor = db.query(TABLE_NEWS, new String[] { KEY_ID,
                KEY_NEWS_ID, KEY_NAME, KEY_CATEGORY, KEY_Package_Id,
                KEY_FAVORITE, KEY_DATE, KEY_NEW_ADDED }, KEY_CATEGORY + "=? AND ",
                KEY_NEW_ADDED + "=? ",new String[] { categoryName, "1" }, null, null, null, null);

            if (cursor != null)
                cursor.moveToFirst();

//Write the update method here


            Log.d(categoryName + " has ", cursor.getCount() + " new News");
            return cursor.getCount();
        }catch (Exception ex){
            Log.d("getNumberOfNewNews: ", ex.toString());
        }finally{
            if (cursor != null ){
                cursor.close();
            }
            if (db != null){
                db.close();
            }
        }
阿拉法特是对的 另外,你必须用两个选择参数来查询你的数据库…例如

public int getNumberOfNewNews(String categoryName) {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try{
            db = this.getReadableDatabase();

            cursor = db.query(TABLE_NEWS, new String[] { KEY_ID,
                KEY_NEWS_ID, KEY_NAME, KEY_CATEGORY, KEY_Package_Id,
                KEY_FAVORITE, KEY_DATE, KEY_NEW_ADDED }, KEY_CATEGORY + "=? AND ",
                KEY_NEW_ADDED + "=? ",new String[] { categoryName, "1" }, null, null, null, null);

            if (cursor != null)
                cursor.moveToFirst();

//Write the update method here


            Log.d(categoryName + " has ", cursor.getCount() + " new News");
            return cursor.getCount();
        }catch (Exception ex){
            Log.d("getNumberOfNewNews: ", ex.toString());
        }finally{
            if (cursor != null ){
                cursor.close();
            }
            if (db != null){
                db.close();
            }
        }

这里我给出了4种类型存储数据库的示例,其中我给出了sqlite存储示例。你可以查一查你的那顶帽子帮不了我。对不起,这里我给出了4种类型存储数据库的示例,因为我给出了sqlite存储示例。你可以查一查你的那顶帽子帮不了我。对不起,这里我给出了4种类型存储数据库的示例,因为我给出了sqlite存储示例。你可以查一查你的那顶帽子帮不了我。对不起,这里我给出了4种类型存储数据库的示例,因为我给出了sqlite存储示例。你可以查一查你的那顶帽子帮不了我。对不起,谢谢,这对我帮助很大!很高兴知道它有帮助:)谢谢,它帮了我很多!很高兴知道它有帮助:)谢谢,它帮了我很多!很高兴知道它有帮助:)谢谢,它帮了我很多!很高兴知道这有帮助:)