使用SQLite数据库

时间:2018-01-13 00:08:33

标签: android sqlite android-recyclerview adapter android-context

我有一个有效的回收视图:

public class MtbListAdapter extends RecyclerView.Adapter<MtbListAdapter.RecyclerViewHolder> implements Filterable {
private ArrayList<ApiObject> arrayList;
private ArrayList<ApiObject> arrayListFiltered;
private DatabaseHelper databaseHelper;

public MtbListAdapter(ArrayList<ApiObject> arrayList) {
    this.arrayList =arrayList;
    this.arrayListFiltered =arrayList;
}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_mtb, viewGroup, false);
    return new RecyclerViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerViewHolder recyclerViewHolder, int position){

    //load texts to card
    recyclerViewHolder.tv_name.setText(arrayListFiltered.get(position).getName());
    recyclerViewHolder.tv_length.setText(arrayListFiltered.get(position).getLength());

    //is card already saved?
    String s_identifier = arrayListFiltered.get(position).getId();
    databaseHelper = new DatabaseHelper(recyclerViewHolder.tv_name.getContext());
    boolean isSaved = databaseHelper.isSaved(s_identifier);

    if (isSaved) recyclerViewHolder.iv_isSaved.setVisibility(View.VISIBLE);
    else recyclerViewHolder.iv_isSaved.setVisibility(View.INVISIBLE);
}

onBindViewHolder我检查项目是否已保存,如果是,请放置&#34;已保存&#34;卡上的图像。我这样做是通过在我的SQLite数据库中为已保存的项目获取ID值。我关心的一件事是databaseHelper = new DatabaseHelper(recyclerViewHolder.tv_name.getContext());被召唤了很多。问题1.这是一个问题吗?

为了缓解我的顾虑,我尝试通过构造函数传递上下文并使databaseHelper成为一个Object类,但是我传递的任何上下文都导致我的SQLite数据库方法在databaseHelper类中崩溃。我尝试传递Activity.this,应用程序上下文,基本上下文等。我只是成功传递了viewHolder项目上下文,如图所示。就像我说代码作为书面作品,但我担心这是昂贵的。

这是我尝试过的代码:

 public class MtbListAdapter extends RecyclerView.Adapter<MtbListAdapter.RecyclerViewHolder> implements Filterable {
    private Context context;
    private ArrayList<ApiObject> arrayList;
    private ArrayList<ApiObject> arrayListFiltered;
    private DatabaseHelper databaseHelper = new DatabaseHelper(context);

public MtbListAdapter(ArrayList<ApiObject> arrayList, Context context) {
    this.arrayList =arrayList;
    this.arrayListFiltered =arrayList;
    this.context =context;
}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_mtb, viewGroup, false);
    return new RecyclerViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerViewHolder recyclerViewHolder, int position){

    //load texts to card
    recyclerViewHolder.tv_name.setText(arrayListFiltered.get(position).getName());
    recyclerViewHolder.tv_length.setText(arrayListFiltered.get(position).getLength());

    //is card already saved?
    String s_identifier = arrayListFiltered.get(position).getId();
    boolean isSaved = databaseHelper.isSaved(s_identifier);

    if (isSaved) recyclerViewHolder.iv_isSaved.setVisibility(View.VISIBLE);
    else recyclerViewHolder.iv_isSaved.setVisibility(View.INVISIBLE);
}

这是我的SQLite方法:

 //returns true if is already saved
public boolean isSaved(String s) {
    SQLiteDatabase db = getReadableDatabase();
    String[] columns = new String[] {KEY_IDENTIFIER};
    String where = KEY_IDENTIFIER + " = ?";
    String[] whereArgs = new String[] {s};

    // select column_word from table where column_word = 's' limit 1;
    try (Cursor c = db.query(TABLE_NAME, columns, where, whereArgs, null, null, null, "1")){
        return c.moveToFirst();
    }
}

这是我的错误(我只发布了通过构造函数传递上下文的代码)。否则一切正常。

  

java.lang.NullPointerException:尝试调用虚方法&#39; android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String,int,android.database.sqlite.SQLiteDatabase $ CursorFactory ,android.database.DatabaseErrorHandler)&#39;在空对象引用上

1 个答案:

答案 0 :(得分:1)

您可以通过constrcutor包含 context ,然后在构造函数中创建一个帮助器实例。

e.g。而不是: -

public MtbListAdapter(ArrayList<ApiObject> arrayList) {
    this.arrayList =arrayList;
    this.arrayListFiltered =arrayList;
}

使用: -

public MtbListAdapter(Context context, ArrayList<ApiObject> arrayList) {
    this.arrayList =arrayList;
    this.arrayListFiltered =arrayList;
    databaseHelper = new DatabaseHelper(context);
}

并删除该行: -

    databaseHelper = new DatabaseHelper(recyclerViewHolder.tv_name.getContext());
  

我关注的一件事是databaseHelper = new   DatabaseHelper(recyclerViewHolder.tv_name.getContext());被称为   许多。问题1.这是一个问题吗?

这不是最好的方法,因为你正在重新构建实例。关于打开数据库,实际上这不是在构造时,而是在调用getReableDatabasegetWriteableDatabase时,这与isSaved时完成的相同方法被调用。 get ???? ableDatabase旨在处理多个调用。所以,我相信没有什么值得关注的。

但是,您不能在isSaved方法中关闭Cursor。这应该是值得关注的,好像你有太多的游标打开然后会发生异常。

您可能想要更改: -

//returns true if is already saved
public boolean isSaved(String s) {
    SQLiteDatabase db = getReadableDatabase();
    String[] columns = new String[] {KEY_IDENTIFIER};
    String where = KEY_IDENTIFIER + " = ?";
    String[] whereArgs = new String[] {s};

    // select column_word from table where column_word = 's' limit 1;
    try (Cursor c = db.query(TABLE_NAME, columns, where, whereArgs, null, null, null, "1")){
        return c.moveToFirst();
    }
}

类似于: -

//returns true if is already saved
public boolean isSaved(String s) {
    boolean rv = false;
    SQLiteDatabase db = getReadableDatabase();
    String[] columns = new String[] {KEY_IDENTIFIER};
    String where = KEY_IDENTIFIER + " = ?";
    String[] whereArgs = new String[] {s};

    // select column_word from table where column_word = 's' limit 1;
    (Cursor c = db.query(TABLE_NAME, columns, where, whereArgs, null, null, null, "1"));
    if(c.moveToFirst) {
            rv = true;
    }
    c.close()
    return rv;
}