我想从 sqlite 检索数据并使用 cardview 显示它。我已经阅读并寻找可能的解决方案来解决我的问题,但问题仍然存在。当我试图检索数据时,没有显示输出,尽管数据肯定存在于数据库中。这是我获取所有文件的数据库代码:
public List<Research> getAllResearch() {
List<Research> researchList = new ArrayList<Research>();
// Select All Query
String selectQuery = "SELECT * FROM " + FILE_TABLE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Research research = new Research();
research.setId(Integer.parseInt(cursor.getString(0)));
research.setTitle(cursor.getString(1));
research.setAuthor(cursor.getString(2));
research.setYear(cursor.getString(3));
research.setDesc(cursor.getString(4));
// Adding research to list
researchList.add(research);
} while (cursor.moveToNext());
}
Log.d(TAG,"student data"+ researchList.toString());
// return research list
return researchList;
}
自定义适配器.java;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
static Context context;
CustomAdapter(Context context){
this.context=context;
}
static List<Research> my_data;
public CustomAdapter(Context context, List<Research> my_data) {
this.context = context;
this.my_data = my_data;
this.my_data = new ArrayList<Research>();
}
@Override
public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.card, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) {
holder.title.setText(my_data.get(position).getTitle());
holder.author.setText(my_data.get(position).getAuthor());
holder.year.setText(my_data.get(position).getYear());
}
@Override
public int getItemCount() {
return my_data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView title, author, year;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
author = (TextView) itemView.findViewById(R.id.author);
year = (TextView) itemView.findViewById(R.id.year);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ViewFiles.class);
Bundle extras = new Bundle();
extras.putInt("position", getAdapterPosition());
intent.putExtras(extras);
/*
int i=getAdapterPosition();
intent.putExtra("position", getAdapterPosition());*/
context.startActivity(intent);
Toast.makeText(CustomAdapter.context, "you have clicked Row " + getAdapterPosition(), Toast.LENGTH_LONG).show();
}
}
}
查看文件.java
public class ViewFiles extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
RecyclerView.Adapter adapter;
List<Research> data_list;
DbHelper db;
Session session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_files);
session = new Session(this);
if (!session.loggedin()) {
session.setLoggedin(false);
startActivity(new Intent(ViewFiles.this, Login.class));
db = new DbHelper(this);
data_list = new ArrayList<Research>();
data_list = db.getAllResearch();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view1);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new CustomAdapter(this, data_list);
recyclerView.setAdapter(adapter);
}
}
}
这是 logcat 错误。
E/RecyclerView: No adapter attached; skipping layout
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xee934ca0
希望你能帮我?谢谢:) 这是 logcat 错误图像