如何使用大型数据库/游标的多重自动完成文本

时间:2013-12-07 12:17:59

标签: android

我有一个包含400k记录的数据库,我想将它与MultiAutoCompleteText一起使用。

一种可能的方法是在游标中加载所有这些记录,并在我们的MultiAutoCompleteText视图中使用CursorAdapter。然而,对我来说它似乎效率很低,我想知道是否有更有效的方法,所以我不必完全加载所有记录。

例如,一种方法是考虑用户输入的第一个字母,并仅加载以该字母开头的那些记录。但是,这样我们必须更改每个新单词的光标并将新光标传递给MultiAutoCompleteText,我不知道如何。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

  

每次从数据库中检索400k记录   效率低下......你可以实现这个......

     

最好创建一个服务来加载数据   数据库.....将该服务作为singleTon

public class RecordsLoadingService extends Service {
    private ArrayList<Record> records;
    private static RecordsLoading recordsLoading;
    public static RecordsLoadingService getInstance() {
        return instance;
    }
    public void onCreate() {
        if (instance == null) {         
            instance = this;
        }
    }
    public ArrayList<Clock> getAllRecords() {
        return records;
    }
    public void setRecordsLoadingListener(RecordsLoading recordsLoading) {
        this.recordsLoading = recordsLoading;
    }
    public int onStartCommand(Intent intent, int flags, int startId) {
        LoadData loadData = new LoadData();
        loadData.execute("");
        return super.onStartCommand(intent, flags, startId);
    }
    private class LoadData extends AsyncTask<String, String, ArrayList<Clock>> {
        protected ArrayList<Records> doInBackground(String... params) {
            if(records!=null){
             //retrive records from database
            }
            return records;
        }
        protected void onPostExecute(ArrayList<Clock> clocks) {
            super.onPostExecute(clocks);
            if (recordsLoading != null) {
                recordsLoading.onLoadingCompletedListener(clocks);
            }
        };
    }
}
  

将活动中的数据检索为..

public class MainActivity extends Activity implements RecordsLoading {
    private ArrayList<Record> records=new ArrayList<Records>;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //take one progress Bar in layout and make visible
        setContentView(R.layout.id);
        if (RecordsLoadingService.getAllClocks() != null) {
                onLoadingCompletedListener(citiesLoadingService.getAllClocks());
            } else {
                if (RecordsLoadingService.getInstance() != null) {
                    RecordsLoadingService.getInstance().setCitiesLoadingListener(
                            this);
                } else {
                    Intent startLoading = new Intent(this,
                            RecordsLoadingService.class);
                    startService(startLoading);
                    isStarted = true;
                }
            }
    }
    protected void onResume() {
        if (isStarted) {
            if (RecordsLoadingService.getInstance() != null) {
                RecordsLoadingService.getInstance().setRecordsLoadingListener(
                        this);
            }
        }
        super.onResume();
    }
    public void onLoadingCompletedListener(ArrayList<Records> records) {
        this.records=records;
        initAdapter();
    }
    public void initAdapter(){
    //make that progress Bar in layout and make invisible
    //create adapter for MultiAutoCompleteText and pass these records.....
    }
}