在RecyclerView中显示数据

时间:2017-11-22 10:52:17

标签: android sqlite android-recyclerview android-sqlite

我使用ListView完美地工作,但决定更新我的代码以使用RecyclerView。

我看到没有默认实现,对类似问题的回答很老。

使用某种光标是最好的方法,还是有更好的选择?

这些是我的代码片段,用于处理具有硬编码值的RecyclerView:

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>

这是我的DatabaseAdapter,它与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;
    }
}

1 个答案:

答案 0 :(得分:0)

你可以尝试

  1. 在您的活动中

    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. 您的适配器看起来像

     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. 您的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>