SQLite
 sql >> डेटाबेस >  >> RDS >> SQLite

RecyclerView में SQLite डेटा प्रदर्शित करें

आप जानकारी को शामिल और मॉडल करने के लिए बीन से शुरू कर सकते हैं और इसे लागू करना अधिक आसान बना सकते हैं।

public class DataBean{
    protected int id;
    protected String name;
    protected String card;
    protected String code;
    //Setter, Getters and constructor
    ...
}

बनाए गए डेटाबीन के साथ, आप अपने तरीकों के रिटर्न प्रकारों को डेटाबीन या सूची में बदल सकते हैं और सभी फ़ील्ड के साथ एक स्ट्रिंग वापस करने के बजाय प्रत्येक विधि के अंदर भर सकते हैं।

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;
}

अब जब आप अपने तरीकों को कॉल करते हैं तो आपके पास डेटाबीन ऑब्जेक्ट होता है, अब आपको रीसाइक्लर व्यू में जानकारी दिखाने के लिए अपना एडाप्टर लिखना होगा।

सबसे पहले अपनी गतिविधि में RecyclerView को लिंक और सेटअप करने की आवश्यकता है।

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));

आपको DataBeanAdapter और 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);
        }
    }
}

आईडी, लेआउट और व्यूहोल्डर की विशेषताएं निर्भर करती हैं कि आप रिसाइक्लर व्यू में प्रति आइटम किसे दिखाएंगे।




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. SQLiteDatabase एंड्रॉइड IllegalStateException

  2. SQLite - CSV फ़ाइल से डेटा आयात करें

  3. Android कक्ष डेटाबेस सभी डेटा निर्यात नहीं करेगा

  4. SQLite में एक निर्दिष्ट सीमा के भीतर एक यादृच्छिक संख्या कैसे उत्पन्न करें?

  5. SQLite में डेटटाइम () फ़ंक्शन कैसे काम करता है