【问题标题】:Update on Content Provider does nothing更新内容提供者什么都不做
【发布时间】:2014-04-07 21:26:32
【问题描述】:

我正在尝试制作一款 Android 应用来帮助患有头痛的人。我有一个 sqlite 数据库来存储危机,用户可以通过按下按钮来添加危机。相同的按钮用于指示危机已经结束。换句话说,当您感到头痛时,您按下按钮;然后,当它结束时,再次按下它,应用程序会更新相应的“结束日期”条目。

但如果我的插入做得很好,我的更新根本不会更新。这是它应该如何工作的:

我首先检索数据库中的最新条目(即具有最大 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";

}

当我试图结束当前的危机时,我的 Logcat 告诉我更新了 0 行。

感谢 SO,由于错误使用该功能,我已经纠正了其他问题,但这次,我找到的唯一链接是这个:Android content provider not updating database 并且 OP 刚刚添加了一条评论,说他更新了他的 ContentProvider,但仅此而已。

我做错了什么?我是否“错误命名”了我的列名?我是否误用了更新功能?

感谢您的帮助。

编辑

感谢 Jozua,我意识到我没有在我的 ContentProvider 文件中实现更新功能。好吧,我现在感觉非常愚蠢。编写 update() 函数后,我会及时通知您它是如何工作的。

再次感谢 Jozua。

【问题讨论】:

  • 一款让人头疼的应用哈...酷
  • @hiphopdroid 嘿,学校项目,你知道...
  • 是的@vrashnak,我知道
  • @hiphopdroid 好吧,至少它仍然很有趣,但相当困难。
  • 困难的事情总是让好奇的人感兴趣;)

标签: android android-sqlite android-contentprovider


【解决方案1】:

好吧,我有点解决了这个问题,但方法很糟糕。

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

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

我不会接受我的回答,以防有人发现我的 update() 函数出了什么问题并可能帮助其他人(甚至是我,以便我可以改进我的代码)。

就是这样:)

以下是相关代码部分:

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
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多