请从SQLite数据库列表视图 [英] Make listview from SQLite database

查看:91
本文介绍了请从SQLite数据库列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的SQLite数据库填充列表视图......这是我从数据库中获取我的数据:

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());
    }

我通常会添加一个数组的ListView这样的:

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));

我怎样才能让一个数组从我的sql查询setadapter?

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

推荐答案

要做到这一点,最好的方法是使用的CursorAdapter或SimpleCursorAdapter。这会给你最好的表现,一旦你看着办吧,你会发现这是最简单的方法使用的SQLite数据库的时候。

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.

<一个href="http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html">http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html

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

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);
            }   
        }

创建CustomCursorAdapter的一个实例,像这样... 你需要创建你的光标,就像你已经在做的事情。

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);

这篇关于请从SQLite数据库列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆