Android 内容提供商的更新不起任何作用

Android 内容提供商的更新不起任何作用,android,android-sqlite,android-contentprovider,Android,Android Sqlite,Android Contentprovider,我正在尝试制作一个Android应用程序来帮助那些头痛的人。我有一个sqlite数据库来存储危机,用户可以通过按下按钮来添加危机。同样的按钮也用来表示危机已经结束。换句话说,当你感到头痛时,你按下按钮;然后,当结束后,再次按下该按钮,应用程序将更新相应的条目,其中包含结束日期 但是,如果我的insert做得很好,那么我的更新根本就不会更新。以下是它的工作原理: 我首先检索数据库中id最大的最新条目,然后获取实际日期,并将其放入ContentValue中。最后我更新了条目 以下是按钮代码: pub

我正在尝试制作一个Android应用程序来帮助那些头痛的人。我有一个sqlite数据库来存储危机,用户可以通过按下按钮来添加危机。同样的按钮也用来表示危机已经结束。换句话说,当你感到头痛时,你按下按钮;然后,当结束后,再次按下该按钮,应用程序将更新相应的条目,其中包含结束日期

但是,如果我的insert做得很好,那么我的更新根本就不会更新。以下是它的工作原理:

我首先检索数据库中id最大的最新条目,然后获取实际日期,并将其放入ContentValue中。最后我更新了条目

以下是按钮代码:

public void onClickStartStop(View v){
    Log.v("andromed", "Starting/Stopping crisis");
    String d = new Date().toString();
    ContentValues cv = new ContentValues();
    String user_info = "";
    String[] projection = {CriseContract.COLUMN_NAME_CRISE_ID, CriseContract.COLUMN_NAME_CRISE_DEBUT,CriseContract.COLUMN_NAME_CRISE_FIN};
    Cursor criseCursor = getContentResolver().query(CriseContract.CONTENT_URI, projection,"SELECT MAX("+CriseContract.COLUMN_NAME_CRISE_ID+") FROM "+CriseContract.TABLE_NAME, null, null);
    Log.v("andromed",""+criseCursor.getCount());
    if(criseCursor.getCount()>=0){
        while(criseCursor.moveToNext()){
            String date_fin = criseCursor.getString(criseCursor.getColumnIndex(CriseContract.COLUMN_NAME_CRISE_FIN));
            if(!(date_fin==(null))){
                Log.v("andromed","Date exists "+date_fin);
                user_info = "Crise enregistrée";
                cv.put(CriseContract.COLUMN_NAME_CRISE_DEBUT, d);
                Uri u = getContentResolver().insert(CriseContract.CONTENT_URI, cv);
            }else{
                String date_deb = criseCursor.getString(criseCursor.getColumnIndex(CriseContract.COLUMN_NAME_CRISE_DEBUT));
                if(date_deb==null){
                    Log.v("andromed","No date in db");
                    user_info = "Crise enregistrée";
                    cv.put(CriseContract.COLUMN_NAME_CRISE_DEBUT, d);
                    Uri u = getContentResolver().insert(CriseContract.CONTENT_URI, cv);
                }else{
                    Log.v("andromed", "Need to close the crisis");
                    cv.put(CriseContract.COLUMN_NAME_CRISE_FIN, d);
                    int tmp = getMaxId();
                    String where = CriseContract.COLUMN_NAME_CRISE_ID+"="+tmp;
                    String[] st = {""+tmp};
                    int nup = getContentResolver().update(CriseContract.CONTENT_URI,cv, where, null);
                    Log.v("andromed", nup+" rows updated");
                    user_info = "Crise terminée";
                }
            }
        }
    }else{
        user_info = "Erreur lors de la lecture";
    }
    Toast t = Toast.makeText(getApplicationContext(),user_info, Toast.LENGTH_LONG);
    t.show();
}
别介意日志和吐司之类的东西,就为了我

以下是检索最大id的函数:

private int getMaxId(){
    String[] projection = {CriseContract.COLUMN_NAME_CRISE_ID};
    String selection = "SELECT MAX("+CriseContract.COLUMN_NAME_CRISE_ID+") FROM "+CriseContract.TABLE_NAME;
    Cursor c = getContentResolver().query(CriseContract.CONTENT_URI, projection, selection, null, null);
    Log.v("andromed", ""+c.getCount());
    int maxid=-1;
    if(c!=null){
        while(c.moveToNext()){
            maxid = c.getInt(c.getColumnIndex(CriseContract.COLUMN_NAME_CRISE_ID));
        }
    }
    Log.v("andromed", "Greatest id in table Crise : "+maxid);
    return maxid;
}
当然,我的合同课:

public final static class CriseContract{
    public static final String AUTHORITY = "com.piertris.andromed";
    public static final String BASE_PATH = "database";
    public static final Uri CONTENT_URI = Uri.parse("content://"+AUTHORITY+"/"+BASE_PATH);
    public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE+"/"+BASE_PATH;
    public static final String CONTENT_ITEM_TYPE= ContentResolver.CURSOR_ITEM_BASE_TYPE+"/andromed";
    public static final String TABLE_NAME = "crises";
    public static final String COLUMN_NAME_CRISE_ID = "criseid";
    public static final String COLUMN_NAME_CRISE_DEBUT = "date_debut";
    public static final String COLUMN_NAME_CRISE_FIN = "date_fin";
    public static final String COLUMN_NAME_INTENSITE = "intensite";
    public static final String COLUMN_NAME_SYMPTOM = "symptome";
    public static final String COLUMN_NAME_MED = "prise_med";
    public static final String COLUMN_NAME_MEDS = "type_med";
    public static final String COLUMN_NAME_AURA = "aura";
    public static final String COLUMN_NAME_COMMENT = "comments";

}
当我试图结束当前的危机时,我的日志告诉我0行已更新

多亏了这一点,我已经纠正了由于错误使用该函数而导致的其他问题,但这一次,我找到的唯一链接是:OP只是添加了一条评论,说他更新了他的ContentProvider,但仅此而已

我做错了什么?我把我的列名写错了吗?我是否滥用了更新功能

谢谢你的帮助

编辑

多亏了Jozua,我意识到我没有在ContentProvider文件中实现更新功能。好吧,我现在觉得非常愚蠢。一旦写了更新函数,我会告诉你它是如何工作的


再次感谢Jozua

好吧,我算是解决了这个问题,但用了一种非常糟糕的方式

考虑到我只检索了一次危机,以便添加除了开始日期和id以外的几乎所有需要的内容,我只是将我的更新请求转换为一个delete,然后是一个更新,其中我传递了一个ContentValue,其中包含我以前删除的行的值

我知道这是一个非常糟糕的编程,但至少它是有效的

我不会接受我的答案,以防有人发现我的更新功能出了什么问题,可能会帮助其他人甚至我,这样我就可以改进我的代码

就是这样:

以下是相关代码的部分:

public void onClickStartStop(View v){
    //go straight to relevant part
    String date_deb = criseCursor.getString(criseCursor.getColumnIndex(CriseContract.COLUMN_NAME_CRISE_DEBUT));
    Log.v("andromed", "Need to close the crisis");
    cv.put(CriseContract.COLUMN_NAME_CRISE_ID, criseCursor.getInt(criseCursor.getColumnIndex(CriseContract.COLUMN_NAME_CRISE_ID)));
    cv.put(CriseContract.COLUMN_NAME_CRISE_DEBUT, date_deb);
    cv.put(CriseContract.COLUMN_NAME_CRISE_FIN, d);
    int ndel = getContentResolver().delete(CriseContract.CONTENT_URI, CriseContract.COLUMN_NAME_CRISE_ID+"=?", new String[] {""+criseCursor.getInt(criseCursor.getColumnIndex(CriseContract.COLUMN_NAME_CRISE_ID))});
    Log.v("andromed", ndel+" rows deleted");
    Uri u = getContentResolver().insert(CriseContract.CONTENT_URI, cv);
    user_info = "Crise terminée";
    //End of relevant code
}

感谢那些可能已经搜索过的人。

一个头痛的应用,嗯。。。cool@hiphopdroid嘿,学校项目,你知道的…是的,我know@hiphopdroid嗯,至少它仍然很有趣,但相当困难。对那些好奇的人来说,困难的事情总是有趣的;