SQLite
 sql >> Datenbank >  >> RDS >> SQLite

Anzeigen von Daten in einer RecyclerView

Sie können es versuchen

  1. In Ihrer Aktivität

    ArrayList<Author> authors = dbHandler. getAllAuthors();
    recyclerView.setHasFixedSize(true);
    
    // ListView
    recyclerView.setLayoutManager(new LinearLayoutManager(youractivity.this));
    
    
    // create an Object for Adapter
    CardViewDataAdapter1  mAdapter = new CardViewDataAdapter1(authors);
    
    // set the adapter object to the Recyclerview
    recyclerView.setAdapter(mAdapter);
    
  2. Ihr Adapter sieht so aus

     class CardViewDataAdapter1 extends RecyclerView.Adapter<CardViewDataAdapter1.ViewHolder> {
       private ArrayList<Author> dataSet;
    
    
    public CardViewDataAdapter1(ArrayList<Author> os_versions) {
    
        dataSet = os_versions;
    }
    
    
    @Override
    public CardViewDataAdapter1.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
      // create a new view
        View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
                R.layout.recycler_item, null);
    
        itemLayoutView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
    
        // create ViewHolder
    
        CardViewDataAdapter1.ViewHolder viewHolder = new CardViewDataAdapter1.ViewHolder(itemLayoutView);
    
        return viewHolder;
    }
    
    @Override
    public void onBindViewHolder(CardViewDataAdapter1.ViewHolder viewHolder, int i) {
    
        Author fp = dataSet.get(i);
    
        viewHolder.tv_name.setText(fp.getName());
    
    
    
    
        viewHolder.menu = fp;
    }
    
    @Override
    public int getItemCount() {
    
        return dataSet.size();
    }
    
    public void updateList(List<Author> temp) {
        dataSet = (ArrayList<Author>) temp;
        notifyDataSetChanged();
    }
    
    // inner class to hold a reference to each item of RecyclerView
    public  class ViewHolder extends RecyclerView.ViewHolder {
    
        public TextView tv_name;
    
    
        public Author menu;
        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
    
            tv_name = (TextView) itemLayoutView  .findViewById(R.id.tv_name);
    
    
           }
    
       }
     }
    
  3. Ihre recycler_item.xml

    <?xml version="1.0" encoding="utf-8"?>
       <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:card_view="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      card_view:cardCornerRadius="5dp"
     card_view:cardBackgroundColor="#FFFFFF"
        card_view:cardUseCompatPadding="true"
       >
    <LinearLayout
      android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical">
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
             android:layout_height="wrap_content"
            android:text="Enamul"/>
    
     </LinearLayout>
    
    </android.support.v7.widget.CardView>