在显示的ListView 100000+项目从SQLite数据库 [英] Displaying 100000+ items in ListView from SQLite database

查看:157
本文介绍了在显示的ListView 100000+项目从SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示所有从pre填充数据库(超过100000行)在使用光标一个ListView的项目。它的工作原理,但它需要一两分钟为应用程序启动并显示在ListView。是否有一个更快的方法?我读过一些有关FTS3表,将能够帮助?

我用的ArrayList<&HashMap的LT;字符串,字符串>> 与SimpleAdapter和自定义2线布局。
code:

 光标光标= sDictionary.query(FTSgesla,新的String [] {PodatkovnaBaza.KEY_WORD,PodatkovnaBaza.KEY_DEFINITION},NULL,NULL,NULL,NULL,PodatkovnaBaza.KEY_WORD);    如果(光标== NULL)
    {            //没有结果
            mTextView.setText(空);
    }
    其他
    {        HashMap的<字符串,字符串>项目;        如果(cursor.moveToFirst())
        {
            做
            {
                项目=新的HashMap<字符串,字符串>();
                item.put(第1行,cursor.getString(0));
                item.put(2号线,cursor.getString(1));
                list.add(项目);
            }
            而(cursor.moveToNext());
        }            SA =新SimpleAdapter(GlavniActivity.this,列表
                    R.layout.result,
                    新的String [] {行1,2号线},
                    新的INT [] {R.id.word,R.id.definition});            mListView.setAdapter(SA);
            //定义上单击侦听器列表项
            mListView.setOnItemClickListener(新OnItemClickListener(){                @覆盖
                公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
                   //做一点事
                }
            });
        }


解决方案

有没有加载100000物品更快的方式,你的用户也不会需要看到一次全部,因此,加载它们一个接一个用主题。

 私有类SQLTask扩展的AsyncTask<整数,字符串[],整数GT; {    @覆盖
    保护整数doInBackground(整数... PARAMS){
        db.open();
        光标C = db.getAllDataCursor();
        为(c.moveToFirst();!c.isAfterLast(); c.moveToNext()){
            的String [] TEMP =新的String [c.getColumnCount()];
            的for(int i = 0; I< temp.length;我++){
                温度[I] = c.getString(ⅰ);
            }
            publishProgress(thisTask,温度);
        }
        c.close();
        db.close();
    }    @覆盖
    保护无效onProgressUpdate(字符串[] ...值){
        super.onProgressUpdate(值);
        // LOAD一行
    }
}

I'm trying to display all the items from a pre-filled database (with over 100000 rows) in a ListView using cursors. It works, but it takes a couple of minutes for the app to start and show the ListView. Is there a faster way? I've read something about FTS3 tables, would that help?

I'm using ArrayList<HashMap<String, String>> with a SimpleAdapter and custom 2-line layout. Code:

Cursor cursor = sDictionary.query("FTSgesla", new String[] {PodatkovnaBaza.KEY_WORD, PodatkovnaBaza.KEY_DEFINITION}, null, null, null, null, PodatkovnaBaza.KEY_WORD);

    if (cursor == null) 
    {

            // There are no results
            mTextView.setText("Empty");
    } 
    else 
    {

        HashMap<String, String> item;

        if(cursor.moveToFirst())
        {
            do
            {
                item = new HashMap<String, String>();
                item.put("line1", cursor.getString(0));
                item.put("line2", cursor.getString(1));
                list.add(item);
            }
            while(cursor.moveToNext());
        }

            sa = new SimpleAdapter(GlavniActivity.this, list,
                    R.layout.result,
                    new String[] { "line1","line2" },
                    new int[] {R.id.word, R.id.definition});

            mListView.setAdapter(sa);
            // Define the on-click listener for the list items
            mListView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                   //do something
                }
            });
        }

解决方案

There are no faster way to load 100000 items, and your user will also not need to see all at once, hence, load them one-by-one with thread.

    private class SQLTask extends AsyncTask<Integer, String[], Integer> {

    @Override
    protected Integer doInBackground(Integer... params) {
        db.open();
        Cursor c = db.getAllDataCursor();
        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            String[] temp = new String[c.getColumnCount()];
            for (int i = 0; i < temp.length; i++) {
                temp[i] = c.getString(i);
            }
            publishProgress(thisTask, temp);
        }
        c.close();
        db.close();
    }

    @Override
    protected void onProgressUpdate(String[]... values) {
        super.onProgressUpdate(values);
        // LOAD ONE ROW
    }
}

这篇关于在显示的ListView 100000+项目从SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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