如何在Android App中通过Recyclerview显示外部SQLite数据库数据?

时间:2019-04-02 19:36:32

标签: android android-recyclerview android-sqlite recycler-adapter external-data-source

最近几天,我学习了如何创建SQLite数据库并使用SQLiteOpenHelper通过RecyclerView显示它。。是的,借助Google和Stack-overflow的力量,我学得很好。但是,我现在无法通过RecyclerView显示外部数据库数据。最近两天,我进行了很多搜索,其中大多数仅用于通过使用“ Assets文件夹和SQLiteAssetsHelper将外部数据库导入应用程序。找不到任何解释如何通过已导入的RecyclerView呈现数据的内容。因此,您能给我一些指导,以通过RecyclerView显示外部SQLite数据库数据吗?对此的任何链接或任何书籍或任何参考将不胜感激。在此先感谢。

1 个答案:

答案 0 :(得分:0)

作为与数据库交互的结果,使用应获得一个数据集(一个数组或一个类似ArrayList的集合)。正如我所看到的,从您的解释中,您现在知道如何获取它,但不知道如何将其传递给recyclerView。因此,您需要一个适配器来执行此操作。 Adapter是一个类,它将从数组数据集中获取每个对象,并从其数据中创建(添加)单个recyclerView项。处理完所有数组项目后,适配器将列出项目列表,这样它将用数据填充您的回收站视图,这是一个代码段,可以帮助您了解其工作方式:

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

 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private String[] mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView textView;
        public MyViewHolder(TextView v) {
            super(v);
            textView = v;
        }
    }


    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        TextView v = (TextView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_text_view, parent, false);
        ...
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.textView.setText(mDataset[position]);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}
  

    public class MyActivity extends Activity {
    private RecyclerView recyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);
        recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        recyclerView.setHasFixedSize(true);

        // use a linear layout manager
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        // specify an adapter (see also next example)
        mAdapter = new MyAdapter(myDataset);
        recyclerView.setAdapter(mAdapter);
    }
    // ...
}