0

我有一个主要障碍试图在我的列表中搜索使用来自预填充 SQLite 数据库的游标生成的数据。

我正在使用以下库:

当我启动我的应用程序时,列表是空的,当我在 SearchView 上键入字母时,什么也没有显示。使用的查询有效,因为在尝试实现库之前显示了所有数据。

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.widget.SearchView;
import com.actionbarsherlock.widget.SearchView.OnQueryTextListener;
import com.commonsware.cwac.loaderex.acl.SQLiteCursorLoader;

import es.nirvash.android.mvptimer.adapters.MobListAdapter;
import es.nirvash.android.mvptimer.database.DataBaseManager;

public class FragMvp extends SherlockListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private DataBaseManager dataBase;
    private MobListAdapter mAdapter;
    private SQLiteCursorLoader mloader;
    private String currentQuery = null;
    private final static int MVP_ID = 1100;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true);

        dataBase = new DataBaseManager(getSherlockActivity());

        Cursor cursor = dataBase.mobList("2");

        if (cursor != null) {
            mAdapter = new MobListAdapter(getActivity(), cursor, false);
            setListAdapter(mAdapter);
        }
        getLoaderManager().initLoader(MVP_ID, null, FragMvp.this);
    }

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu, menu);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        searchView.setOnQueryTextListener(queryListener);
    }

    final private OnQueryTextListener queryListener = new OnQueryTextListener() {

        @Override
        public boolean onQueryTextChange(String newText) {
            if (TextUtils.isEmpty(newText)) {
                currentQuery = "";
                Toast.makeText(getActivity(), "Searching for: " + currentQuery + "...", Toast.LENGTH_SHORT).show();
            } else {
                currentQuery = newText;
                Toast.makeText(getActivity(), "Searching for: " + currentQuery + "...", Toast.LENGTH_SHORT).show();

            }
            return false;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            currentQuery = query;
            return false;
        }
    };

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle bargs) {
        String query = 
        "SELECT mo._id, mo.sprite, mo.iName, mo.LVL, mo.HP, ra.Race, pr.Property, mo.Counter " +
        "FROM Mobs AS mo " +
        "JOIN Race AS ra ON (ra._id = mo.Race) " +
        "JOIN Property AS pr ON (pr._id = mo.Property) " +
        "WHERE mo.MobType = ? " +
        "AND mo.iName LIKE ? " +
        "ORDER BY mo.iName " +
        "ASC;";
        String like = "%" + currentQuery + "%";
        String[] args = { "2", like };

        mloader = new SQLiteCursorLoader(getActivity(), dataBase, query, args);

        return (mloader);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        mAdapter.changeCursor(cursor);
        if (isResumed()) {
            setListShown(true);
        } else {
            setListShownNoAnimation(true);
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.changeCursor(null);
    }
}

我究竟做错了什么?先感谢您。

4

1 回答 1

1

感谢 CommonsWare,我发现将initLoader()方法放入其中onActivityCreated是错误的,因为它只被调用了一次。

相反,我应该将restartLoader()方法放在每次SearchView更新时调用的函数中,onQueryTextChange.

final private OnQueryTextListener queryListener = new OnQueryTextListener() {

    @Override
    public boolean onQueryTextChange(String newText) {
        if (TextUtils.isEmpty(newText)) {
            currentQuery = "";
            Toast.makeText(getActivity(), "Searching for: " + currentQuery + "...", Toast.LENGTH_SHORT).show();
        } else {
            currentQuery = newText;
            Toast.makeText(getActivity(), "Searching for: " + currentQuery + "...", Toast.LENGTH_SHORT).show();
        }
        **getLoaderManager().restartLoader(MVP_ID, null, FragMvp.this);**
        return false;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        currentQuery = query;
        return false;
    }
};
于 2013-05-10T13:14:21.880 回答