需要一个快速的方法来使用建议的文本(MatrixCursor?)执行SearchView

时间:2014-03-15 21:51:11

标签: android cursor searchview

我的片段中有一个搜索功能,允许用户搜索我应用中的所有图标。

现在,当应用程序开始将所有图标加载到SQLiteDatabase FTS_VIRTUAL_TABLE时,我有一个加载活动。

即使在我的Nexus 5上,加载1200个图标名称大约需要30秒左右。

我让他们在异步任务中加载这样的

final String[] extras2 = mContext.getResources().getStringArray(R.array.icon_pack);
    for (String extra : extras2) {
        String uri = "drawable/" + extra;
        int res = mContext.getResources().getIdentifier(uri, null, mContext.getPackageName());
        if (res != 0) {
            ob.setNumber(res);
        }
    }
    final String[] extras = mContext.getResources().getStringArray(R.array.icon_pack_names);
    for (String extra : extras) {
        ob.setText(extra);
        publishProgress(extra);
    }

然后使用此异步任务将其加载到我的SQLiteDatabase中

mDbHelper = new SearchDbHelper(mContext);
    mDbHelper.open();

    for (int i = 0; i < myObject.getSize(); i++){
        mDbHelper.createList(myObject.getText().get(i),myObject.getNumber().get(i));
        Integer progress = (100 * i) / myObject.getSize();
        publishProgress(progress);
    }

    return mDbHelper;

然后在我的片段中创建选项菜单

 @Override
public void onPrepareOptionsMenu (Menu menu) {
    Log.i(TAG, "onPrepareOptionsMenu");
    MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.icon_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.search);
    searchView = (SearchView) searchItem.getActionView();
    searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            FrameLayout frame = (FrameLayout) getActivity().findViewById(R.id.frame);
            frame.setVisibility(View.INVISIBLE);
            return true;
        }
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            FrameLayout frame = (FrameLayout) getActivity().findViewById(R.id.frame);
            frame.setVisibility(View.VISIBLE);
            return true;
        }
    });
}

然后,当选择搜索选项时,它会运行此方法

public void doSearch(){
    Log.i(TAG, "doSearch");

    mCategoryList = ThemeApp.getSearchIcons();

    if (mCategoryList == null){
        Log.i(TAG, "mCategoryList == null");
        Intent intent = new Intent(getActivity(), InitializeActivity.class);
        startActivity(intent);
        getActivity().finish();
    }else{
        myList = (ListView) getActivity().findViewById(R.id.grid);
        defaultAdapter = new SearchListAdapter(getActivity(), mCategoryList);
        myList.setAdapter(defaultAdapter);

        searchView.setIconifiedByDefault(true);
        searchView.setOnQueryTextListener(IconsFrag.this);
        searchView.setOnCloseListener(IconsFrag.this);
        searchView.onActionViewCollapsed();

        mDbHelper = new SearchDbHelper(getActivity());
        mDbHelper.open();
    }
}

最后,这是显示结果的方式

private void displayResults(String query) {
    Boolean click = ClickBoolean.getValue();
    Log.i(TAG,query);
    Cursor cursor = mDbHelper.searchByInputText((query != null ? query : "@@@@"));
    Log.i(TAG,cursor.toString());
    if (cursor != null) {
        String[] from = new String[] {SearchDbHelper.COLUMN_NAME, String.valueOf(SearchDbHelper.COLUMN_URI)};

        // Specify the view where we want the results to go
        int[] to = new int[] {R.id.list_item_text_view, R.id.wp_thumb};

        // Create a simple cursor adapter to keep the search data
        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.icon_search_result, cursor, from, to);
        myList.setAdapter(cursorAdapter);

        if(click)
        // Click listener for the searched item that was selected
        myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Cursor cursor = (Cursor) myList.getItemAtPosition(position);
                Integer selectedName = cursor.getInt(cursor.getColumnIndexOrThrow("uri"));

                Log.i(TAG,String.valueOf(selectedName));
                IntentUtil.endWithIcon(getActivity(), selectedName);

            }
        });
    }
}

2个问题 1-如果应用程序保留在后台并且用户稍后打开并尝试搜索,则会发生崩溃。

    java.lang.NullPointerException
at com.**.**.icons.search.SearchListAdapter.getCount(SearchListAdapter.java:33)
at android.widget.ListView.setAdapter(ListView.java:460)
at com.**.**.icons.Icons_app.doSearch(Icons_app.java:148)
at com.**.**.icons.Icons_app.onOptionsItemSelected(Icons_app.java:128)
at android.app.Activity.onMenuItemSelected(Activity.java:2774)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:372)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:979)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:523)
at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:116)
at android.view.View.performClick(View.java:3528)
at android.view.View$PerformClick.run(View.java:14244)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4526)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)

2-必须有一种方法可以在不加载到数据库并需要加载屏幕的情况下执行此操作。我见过其他应用程序执行此操作,我的研究显示它是用MatrixCursor完成的吗?

请帮我解决这个问题..

0 个答案:

没有答案