【问题标题】:How to deal with CurpsorAdapter in connection with SQLCipher如何处理与 SQLCipher 相关的 CurpsorAdapter
【发布时间】:2014-10-25 08:25:03
【问题描述】:

我正在尝试在 Adroid 中建立一个加密的 sqlite3 数据库。

我尝试了 encrypt sqlite database Android: 中建议的 6 个步骤 CommonsWare。其中大部分工作正常。问题是 android.widget.CursorAdapter 似乎没有对应的类。

所以当我尝试实现一个类 BesucheAdapter extends CursorAdapter 时,我收到以下消息:

The method bindView(View, Context, Cursor) of type BesucheAdapter must override or implement a supertype method.

在我项目的许多其他地方,当我尝试通过

从适配器获取光标时

final Cursor cursor = adapter.getCursor();

我明白了:

Type mismatch: cannot convert from android.database.Cursor to net.sqlcipher.Cursor.

任何想法我能做什么?

package net.krankenhauspfarrer.besuche;


import net.sqlcipher.Cursor;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;



public class BesucheAdapter extends CursorAdapter {

//für die anzeige der aktuellen Patienten


private LayoutInflater inflator;
private int ciP, ciSt, ciRaum, ciSex, ciName, ciGeb, ciKat, ciLB, ciHint;


@SuppressWarnings("deprecation")
public BesucheAdapter(Context context, Cursor c) {
    super(context, c);
    Log.d("Besuche","BesucheAdapter vor Constructor");
    //date = new Date(); // 
    inflator = LayoutInflater.from(context);
    ciP = c.getColumnIndex(DBHandler.MAIN_P);
    ciSt = c.getColumnIndex(DBHandler.MAIN_ST);
    ciRaum = c.getColumnIndex(DBHandler.MAIN_RAUM);
    ciSex = c.getColumnIndex(DBHandler.MAIN_SEX);
    ciName = c.getColumnIndex(DBHandler.MAIN_NAME);
    ciGeb = c.getColumnIndex(DBHandler.MAIN_GEB);
    ciKat = c.getColumnIndex(DBHandler.MAIN_KAT);
    ciLB = c.getColumnIndex(DBHandler.MAIN_LB);
    ciHint = c.getColumnIndex(DBHandler.MAIN_HINT);


}
@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView tvP = (TextView) view.findViewById(R.id.listP);
    TextView tvSt = (TextView) view.findViewById(R.id.listSt);
    TextView tvRaum = (TextView) view.findViewById(R.id.listRaum);
    TextView tvSex = (TextView) view.findViewById(R.id.listSex);
    TextView tvName = (TextView) view.findViewById(R.id.listName);
    TextView tvGeb = (TextView) view.findViewById(R.id.listGeb);
    TextView tvKat = (TextView) view.findViewById(R.id.listKat);
    TextView tvLB = (TextView) view.findViewById(R.id.listLB);
    TextView tvHint = (TextView) view.findViewById(R.id.listHint);

    int p = cursor.getInt(ciP);
    tvP.setText (String.valueOf(p));

    String st = cursor.getString(ciSt);
    tvSt.setText(st);

    String raum = cursor.getString(ciRaum);
    tvRaum.setText(raum);

    String sex = cursor.getString(ciSex);
    tvSex.setText(sex);

    String name = cursor.getString(ciName);
    tvName.setText(name);

    long gebMS = cursor.getLong(ciGeb);
    //date.setTime(timeMillis);
    tvGeb.setText(BHelper.resolveBesucheDate2String(gebMS, BHelper.RD_SHORT));

    String kat = cursor.getString(ciKat);
    tvKat.setText(kat);

    long lbMS = cursor.getLong(ciLB);
    //date.setTime(timeMillis);
    tvLB.setText(BHelper.resolveBesucheDate2String(lbMS, BHelper.RD_SHORT));

    String hint = cursor.getString(ciHint);
    tvHint.setText(hint);




}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    Log.d("Besuche","BesucheAdapter vor TextViewNew");
    return inflator.inflate(R.layout.besuche_zeile, null);
}   

【问题讨论】:

    标签: android sqlite encryption sqlcipher


    【解决方案1】:

    你必须使用 Android SQL 游标 (android.database.Cursor) 来实现 CursorAdapter 方法。它就像不使用 sqlcipher 一样工作。

    public void bindView(View view, Context context, Cursor cursor) {
    
    
        //do something with the cursor
     }
    

    我们在我们的项目中使用 sqlcipher,它的工作原理是这样的。无需实现另一个适配器类或其他东西。

    net.sqlcipher.Cursor 只是 android.database.Cursor 的扩展类,提供了一个 sqlcipher 内部使用的 getType 方法:net.sqlcipher.Cursor on Github

    注意:您不能强制转换为 sqlcipher.Cursor,因为 contentresolver 在内部将游标包装在 CursorWrapperInner 实例中,该实例具有 Closeguard 通知您是否未正确关闭游标。但是你不需要投射。您所要做的就是使用它,如果您的 CContentProvider 实现正确使用 net.sqlcipher.Cursor 一切都很好。

    【讨论】:

    • 非常感谢。这拯救了我的一天。在我按照您的建议进行操作并另外添加了 SQLiteDatabase.loadLibs(this); 行之后,一切正常。并将库集成为 [link] zetetic.net/sqlcipher/sqlcipher-for-android
    • np。我建议您已经正确包含了 sqlcipher。你可以接受我的回答来解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-07
    • 2012-02-11
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    相关资源
    最近更新 更多