Display Sqlite Data In Recyclerview
I search a lot, but I didn't find any step-by-step guides on how to display SQLite data in RecyclerView. Can anyone explain to me how can I do this? My DataBaseAdapter : public cla
Solution 1:
You can start with a Bean to contain and model the information and make it more easy to implement.
publicclassDataBean{
protectedint id;
protectedString name;
protectedString card;
protectedString code;
//Setter, Getters and constructor
...
}
With the DataBean created, you can change the return types of your methods to DataBean or a List and filled inside each method instead of return a String with all the fields.
public DataBean getData(String name){
...
DataBeanbean=null;
if (cursor.moveToFirst()) {
intindex= cursor.getColumnIndex(DataBaseHelper.UID);
intindex2= cursor.getColumnIndex(DataBaseHelper.NAME);
intindex3= cursor.getColumnIndex(DataBaseHelper.CARD);
intindex4= cursor.getColumnIndex(DataBaseHelper.CODE);
intid= cursor.getInt(index);
StringpersonName= cursor.getString(index2);
Stringcard= cursor.getString(index3);
Stringcode= cursor.getString(index4);
bean = newDataBean(id, name, card, code);
}
return bean;
}
public List<DataBean> getAllData() {
List<DataBean> list = newArrayList<>();
...
while (cursor.moveToNext()) {
intindex= cursor.getColumnIndex(DataBaseHelper.UID);
intindex2= cursor.getColumnIndex(DataBaseHelper.NAME);
intindex3= cursor.getColumnIndex(DataBaseHelper.CARD);
intindex4= cursor.getColumnIndex(DataBaseHelper.CODE);
intcid= cursor.getInt(index);
Stringname= cursor.getString(index2);
Stringcard= cursor.getString(index3);
Stringcode= cursor.getString(index4);
DataBeanbean=newDataBean(cid, name, card, code);
list.add(bean);
}
return list;
}
Now when you call your methods you have a DataBean object(s), now you need write your Adapter to show the information in the RecyclerView.
First need link and setup the RecyclerView in your Activity.
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(newLinearLayoutManager(this));
mRecyclerView.setItemAnimator(newDefaultItemAnimator());
mRecyclerView.setAdapter(newDataBeanAdapter(dbAdapter.getAllData(), R.layout.item));
After you need create the DataBeanAdapter and the ViewHolder.
publicclassDataBeanAdapterextendsRecyclerView.Adapter<DataBeanAdapter.ViewHolder>{
private List<DataBean> items;
privateint itemLayout;
publicDataBeanAdapter(List<DataBean> items, int itemLayout){
this.items = items;
this.itemLayout = itemLayout;
}
@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Viewv= LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
returnnewViewHolder(v);
}
@OverridepublicvoidonBindViewHolder(ViewHolder holder, int position) {
DataBeanitem= items.get(position);
holder.name.setText(item.getName());
holder.card.setText(item.getCard());
//All the thing you gonna show in the item
}
@OverridepublicintgetItemCount() {
return items.size();
}
publicstaticclassViewHolderextendsRecyclerView.ViewHolder {
public TextView name;
public TextView card;
publicViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
card = (TextView) itemView.findViewById(R.id.card);
}
}
}
The id's, layout and the attributes of the ViewHolder depending who you gonna show per item in the RecyclerView.
Post a Comment for "Display Sqlite Data In Recyclerview"