我正在尝试使用游标在ListView中显示预填充数据库(超过100000行)中的所有项目.它可以工作,但是应用程序启动并显示ListView需要几分钟的时间.有更快的方法吗?我读过有关FTS3表的内容,会
我正在使用ArrayList< HashMap< String,String>>使用SimpleAdapter和自定义2行布局.
码:
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 } }); }没有更快的方法来加载100000个项目,并且您的用户也不需要同时查看所有项目,因此,使用线程逐个加载它们.
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 } }