Du kan starte med en Bean for at indeholde og modellere oplysningerne og gøre det nemmere at implementere.
public class DataBean{
protected int id;
protected String name;
protected String card;
protected String code;
//Setter, Getters and constructor
...
}
Med den oprettede DataBean kan du ændre returneringstyperne for dine metoder til DataBean eller en liste og udfyldt i hver metode i stedet for at returnere en streng med alle felterne.
public DataBean getData(String name){
...
DataBean bean = null;
if (cursor.moveToFirst()) {
int index = cursor.getColumnIndex(DataBaseHelper.UID);
int index2 = cursor.getColumnIndex(DataBaseHelper.NAME);
int index3 = cursor.getColumnIndex(DataBaseHelper.CARD);
int index4 = cursor.getColumnIndex(DataBaseHelper.CODE);
int id = cursor.getInt(index);
String personName = cursor.getString(index2);
String card = cursor.getString(index3);
String code = cursor.getString(index4);
bean = new DataBean(id, name, card, code);
}
return bean;
}
public List<DataBean> getAllData() {
List<DataBean> list = new ArrayList<>();
...
while (cursor.moveToNext()) {
int index = cursor.getColumnIndex(DataBaseHelper.UID);
int index2 = cursor.getColumnIndex(DataBaseHelper.NAME);
int index3 = cursor.getColumnIndex(DataBaseHelper.CARD);
int index4 = cursor.getColumnIndex(DataBaseHelper.CODE);
int cid = cursor.getInt(index);
String name = cursor.getString(index2);
String card = cursor.getString(index3);
String code = cursor.getString(index4);
DataBean bean = new DataBean(cid, name, card, code);
list.add(bean);
}
return list;
}
Når du nu kalder dine metoder, har du et DataBean-objekt(er), nu skal du skrive din adapter for at vise informationen i RecyclerView.
Først skal du linke og konfigurere RecyclerView i din aktivitet.
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(new DataBeanAdapter(dbAdapter.getAllData(), R.layout.item));
Når du skal oprette DataBeanAdapter og ViewHolder.
public class DataBeanAdapter extends RecyclerView.Adapter<DataBeanAdapter.ViewHolder>{
private List<DataBean> items;
private int itemLayout;
public DataBeanAdapter(List<DataBean> items, int itemLayout){
this.items = items;
this.itemLayout = itemLayout;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
DataBean item = items.get(position);
holder.name.setText(item.getName());
holder.card.setText(item.getCard());
//All the thing you gonna show in the item
}
@Override
public int getItemCount() {
return items.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView card;
public ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
card = (TextView) itemView.findViewById(R.id.card);
}
}
}
Id'er, layout og attributter for ViewHolder afhængigt af, hvem du vil vise pr. vare i RecyclerView.