Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

android - Displaying data in a RecyclerView

I have this working perfectly with a ListView, but decided to update my code to use RecyclerView.

I see there is no default implementation, and the responses to similar questions are quite old.

Is using a sort of cursor the best way to go, or is there a better option?

These are my code snippets for a working RecyclerView with hard-coded values:

MainActivity

    recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
    mLayoutmanager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(mLayoutmanager);

    mAdapter = new recyclerViewDataAdapter();
    recyclerView.setAdapter(mAdapter);

recyclerViewDataAdapter

public class recyclerViewDataAdapter extends RecyclerView.Adapter {

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        //lets populate our recyler view with the item created;
        //get the view from the layout inflator
        //  third parameter is set to false to prevent viewgroup to attach to root
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);

        // as this method need to return the viewHolder type
        // need to convert our view to the view holder
        RecyclerView.ViewHolder viewHolder = new RecyclerView.ViewHolder(view) {
            @Override
            public String toString() {
                return super.toString();
            }
        };
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return 3000;
    }
}

activity_main

<android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

recycler_item

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hii "
        android:textSize="30sp" />
</LinearLayout>

Here is my DatabaseAdapter which worked perfectly with a ListView:

public class DatabaseHelper extends SQLiteAssetHelper {

    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "database9.db";
    private static final String BOOKS = "books";
    private static final String AUTHORS = "authors";

    public DatabaseHelper (Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // setForcedUpgrade();
    }

    // Getting all books
    public ArrayList<Author> getAllAuthors() {

        ArrayList<Author> authorList = new ArrayList<>();

        // Select all query
        String selectQuery = "SELECT id, name FROM " + AUTHORS + " ORDER BY name_alphabetic";

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                // create new author object
                Author author = new Author();
                // set ID and name of author object
                author.setID(Integer.parseInt(cursor.getString(0)));
                author.setName(cursor.getString(1));
                // pass author object to authorList array
                authorList.add(author);
            } while (cursor.moveToNext());
        }

        // return author list
        return authorList;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your can try

  1. In your activity

    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. Your adapter is look like

     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. Your 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>
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...