如何处理与SQLCipher相关的CurpsorAdapter

时间:2014-10-25 08:25:04

标签: android sqlite encryption sqlcipher

我试图在Adroid中设置一个encypted sqlite3数据库。

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

所以当我尝试实现一个类BesucheAdapter扩展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);
}   

1 个答案:

答案 0 :(得分:0)

哟必须使用Android SQL Cursor(android.database.Cursor)实现CursorAdapter方法。它就像不使用sqlcipher一样。

public void bindView(View view, Context context, Cursor cursor) {


    //do something with the cursor
 }

我们在项目中使用sqlcipher,它的工作原理如下。无需实现另一个Adapter类或其他东西。

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

注意:您无法转换为sqlcipher.Cursor,因为contentresolver会在CursorWrapperInner实例中内部包装Cursor,该实例具有Closeguard,如果您没有正确关闭Cursor,则会通知您。但你不需要施放。您所要做的就是使用它,如果您的COntentProvider实现正确使用net.sqlcipher.Cursor,一切都很好。