gpt4 book ai didi

android - 如何将 SQLite 数据库中的数据显示到 RecyclerView 中

转载 作者:行者123 更新时间:2023-11-29 23:16:54 24 4
gpt4 key购买 nike

我想将数据从我的 SQLite 数据库显示到我的 RecyclerView(使用 LinearLayoutManager)。我为列表创建了 RecyclerView 布局,为单个项目创建了 CardView 布局。我用食谱表创建了数据库,我可以保存一个新的食谱。我的目标是在 CardView 中显示 Recipe 的标题。

配方类

public class Recipe {

private int id;
private String title;
private String photo;
private String instructions;
private int targetPeople;
private int time;



public Recipe() {

this.id = id;
this.title = title;
this.photo = photo;
this.instructions = instructions;
this.targetPeople = targetPeople;
this.time = time;
}

(加上 setter/getter 方法)

在 DatabaseHelper 中,我创建了一个方法来将所有食谱添加到列表中:

public List<Recipe> getAllRecipes() {

// sorting orders
String sortOrder =
RECIPE_TITLE + " ASC";
List<Recipe> recipeList = new ArrayList<Recipe>();

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.rawQuery("SELECT * FROM " + TBL_RECIPE, null);

// Traversing through all rows and adding to list
if (cursor.moveToFirst()) {

do {

Recipe recipe = new Recipe();
recipe.setTitle(cursor.getString(cursor.getColumnIndex("TITLE")));

// Adding user record to list
recipeList.add(recipe);
} while (cursor.moveToNext());
}

cursor.close();
db.close();

// return user list
return recipeList;
}

这是我的适配器类,带有 RecyclerView 的 ViewHolder 类:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private Context mContext;
private List<Recipe> recipeList;

//constructor of adapter
public MyAdapter(Context mContext, List<Recipe> recipeList) {
this.mContext = mContext;
this.recipeList = recipeList;

}

//ViewHolder Class
public class ViewHolder extends RecyclerView.ViewHolder {

public final TextView textViewTitle;

//constructor for ViewHolder
public ViewHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.descriptionView);
}

}

//return an instance of ViewHolder Class
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {

View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_card, parent, false);

return new ViewHolder(itemView);
}

//binding data to our ViewHolder
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {

// retrieve UI elements inside viewHolder object

viewHolder.textViewTitle.setText(recipeList.get(position).getTitle());

}

//return the size of the list
@Override
public int getItemCount() {
return recipeList.size();
}
}

最后,我在MainActivity中声明了RecyclerView、List、Adapter等,我在其中创建了一个方法来显示所有食谱:

    @SuppressLint("StaticFieldLeak")
private void displayAllRecipes() {
// AsyncTask is used that SQLite operation does not block the UI Thread.
new AsyncTask<Void, Void, ArrayList<Recipe>>() {
@Override
protected ArrayList<Recipe> doInBackground(Void... params) {

recipeList.clear();
recipeList.addAll(dbHelper. getAllRecipes());
return recipeList;
}

@Override
protected void onPostExecute(ArrayList<Recipe> aVoid) {
super.onPostExecute(aVoid);
resultAdapter.notifyDataSetChanged();
}
}.execute();
}

没有错误,但它不起作用。

最佳答案

确保您已将 arrayList 传递给适配器并将适配器设置为 recyclerView。

关于android - 如何将 SQLite 数据库中的数据显示到 RecyclerView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55159923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com