从 SQLite 数据库制作列表视图

发布于 2024-10-27 03:55:36 字数 842 浏览 1 评论 0原文

我正在尝试从我的 SQLite 数据库填充 listview...这就是我从数据库获取数据的方式:

    Cursor c = database.rawQuery("SELECT * FROM " + TableName, null);
    int Column1 = c.getColumnIndex("uri");
    int Column2 = c.getColumnIndex("file");
    int Column3 = c.getColumnIndex("id");
    c.moveToFirst();
    if (c != null) {
        do {
            String uri = c.getString(Column1);
            String file = c.getString(Column2);
            int id = c.getInt(Column3);
        } while (c.moveToNext());
    }

我通常会像这样向 listview 添加一个数组:

    ListView my_listview2 = (ListView) findViewById(R.id.listView1);
    String my_array[] = {"Android", "iPhone"};
    my_listview2.setAdapter(new ArrayAdapter<String>(this, R.layout.row, R.id.my_custom_row, my_array));

如何从我的 sql 查询中创建一个用于 setadapter 的数组?

I'm trying to populate listview from my SQLite database... this is how I get my data from database:

    Cursor c = database.rawQuery("SELECT * FROM " + TableName, null);
    int Column1 = c.getColumnIndex("uri");
    int Column2 = c.getColumnIndex("file");
    int Column3 = c.getColumnIndex("id");
    c.moveToFirst();
    if (c != null) {
        do {
            String uri = c.getString(Column1);
            String file = c.getString(Column2);
            int id = c.getInt(Column3);
        } while (c.moveToNext());
    }

I would normally add an array to listview like that:

    ListView my_listview2 = (ListView) findViewById(R.id.listView1);
    String my_array[] = {"Android", "iPhone"};
    my_listview2.setAdapter(new ArrayAdapter<String>(this, R.layout.row, R.id.my_custom_row, my_array));

How can I make an array to setadapter from my sql query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微凉 2024-11-03 03:55:36

执行此操作的最佳方法是使用 CursorAdapter 或 SimpleCursorAdapter。这将为您提供最佳性能,一旦您弄清楚了,您就会发现这是使用 SQLite 数据库时最简单的方法。

http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html

下面是我经常使用的一个简单的 CustomCursorAdapter。只需将 CustomCursorAdapter 类添加为内部类即可。

    protected class CustomCursorAdapter extends SimpleCursorAdapter  {
        private int layout; 
        private LayoutInflater inflater;
        private Context context;

        public CustomCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
            super(context, layout, c, from, to);
            this.layout = layout;
            this.context = context;
            inflater = LayoutInflater.from(context);

        }


        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            Log.i("NewView", newViewCount.toString());

            View v = inflater.inflate(R.layout.list_cell, parent, false);

            return v;
        }

        @Override
        public void bindView(View v, Context context, Cursor c) {
                    //1 is the column where you're getting your data from
            String name = c.getString(1);
            /**
             * Next set the name of the entry.
             */
            TextView name_text = (TextView) v.findViewById(R.id.textView);
            if (name_text != null) {
                name_text.setText(name);
            }   
        }

像这样创建 CustomCursorAdapter 的实例...
您需要像您已经做的那样创建光标。

protected String[] from;
protected int[] to;

    //From is the column name in your cursor where you're getting the data
    //to is the id of the view it will map to
    from = new String[]{"name"};
    to = new int[]{R.id.textView};

CustomCursorAdapter adapter = new CustomCursorAdapter(this, R.layout.list, cursor, from, to);
listView.setAdapter(adapter);

The best way to do this is to use a CursorAdapter or a SimpleCursorAdapter. This will give you the best performance and once you figure it out you'll find it's the simplest approach when using a SQLite db.

http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html

Below is a simple CustomCursorAdapter that I use frequently. Just add the CustomCursorAdapter class as an inner class.

    protected class CustomCursorAdapter extends SimpleCursorAdapter  {
        private int layout; 
        private LayoutInflater inflater;
        private Context context;

        public CustomCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
            super(context, layout, c, from, to);
            this.layout = layout;
            this.context = context;
            inflater = LayoutInflater.from(context);

        }


        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            Log.i("NewView", newViewCount.toString());

            View v = inflater.inflate(R.layout.list_cell, parent, false);

            return v;
        }

        @Override
        public void bindView(View v, Context context, Cursor c) {
                    //1 is the column where you're getting your data from
            String name = c.getString(1);
            /**
             * Next set the name of the entry.
             */
            TextView name_text = (TextView) v.findViewById(R.id.textView);
            if (name_text != null) {
                name_text.setText(name);
            }   
        }

Create an instance of the CustomCursorAdapter like so...
You'll need to create your cursor just like you're already doing.

protected String[] from;
protected int[] to;

    //From is the column name in your cursor where you're getting the data
    //to is the id of the view it will map to
    from = new String[]{"name"};
    to = new int[]{R.id.textView};

CustomCursorAdapter adapter = new CustomCursorAdapter(this, R.layout.list, cursor, from, to);
listView.setAdapter(adapter);
荆棘i 2024-11-03 03:55:36

我发现使用记事本教程对于了解这一点非常有用。
它向您展示了如何通过非常简单的步骤使用 sqlite 数据库实现列表视图。

http://developer.android.com/resources/tutorials/notepad/index.html

I found working with the notepad tutorial very useful for learning about this.
It shows you how to implement the listview using the sqlite database in very easy steps.

http://developer.android.com/resources/tutorials/notepad/index.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文