如何将来自本地数据库的循环数据提供给android中的recycleler视图。
我有三个班级,
ItemData.java
public class NatureItem {
private String title;
private int imageUrl;
public NatureItem(String title,int imageUrl){
this.title = title;
this.imageUrl = imageUrl;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getImageUrl() {
return imageUrl;
}
public void setImageUrl(int imageUrl) {
this.imageUrl = imageUrl;
}
}
CardAdapter.java
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
private NatureItem[] itemsData;
public CardAdapter(NatureItem[] itemsData) {
this.itemsData = itemsData;
}
// Create new views (invoked by the layout manager)
@Override
public CardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_cardview_activity, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData
viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
}
// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView txtViewTitle;
public ImageView imgViewIcon;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.blog_content);
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.imageView);
}
}
// Return the size of your itemsData (invoked by the layout manager)
@Override
public int getItemCount() {
return itemsData.length;
}
}
MainActivity.java
NatureItem itemsData[] ={
new NatureItem("imageName",R.drawable.image2),
new NatureItem("imageName2",R.drawable.image2)
};
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new CardAdapter(itemsData);
recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
使用上面的代码,我可以使用上面数组中的信息(itemsData
)填充我的recycleview。
现在,我希望能够使用下面的数据库处理程序使用本地sql数据库中的数据填充我的recycleview;
public class DatabaseHandler extends SQLiteOpenHelper {
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_FIRSTNAME + " TEXT,"
+ KEY_LASTNAME + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE,"
+ KEY_UID + " TEXT,"
+ KEY_CREATED_AT + " TEXT" + ")";
db.execSQL(CREATE_LOGIN_TABLE);
}
/**
* Getting user data from database
* */
public Cursor getUserDetails(){
HashMap<String,String> user = new HashMap<String,String>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor!=null){
user.put("fname", cursor.getString(1));
user.put("lname", cursor.getString(2));
user.put("email", cursor.getString(3));
user.put("uid", cursor.getString(4));
user.put("created_at", cursor.getString(5));
cursor.moveToNext();
}
return cursor;
}
/**
* Getting user status
* return true if rows are there in table
* */
public int getRowCount() {
String countQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int rowCount = cursor.getCount();
db.close();
cursor.close();
// return row count
return rowCount;
}
}
请问如何使用数据库处理程序中的数据填充我的recyclerview。如果有人可以提供帮助,我将不胜感激。提前致谢
答案 0 :(得分:1)
我建议使用Android Loader Manager,它非常适用于此目的,也适用于基于Android的。
可以在这里找到一个很好的例子:
编辑:在此POST 中,您可以找到所需内容。 将它与您的活动/片段中的加载程序管理器结合起来就可以了。 记得使用OnLoadFinished
中的新光标调用adapter.switchCursor()希望这会对你有所帮助。