我正在尝试搜索使用预先填充的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);
}
}
我做错了什么?提前谢谢。
答案 0 :(得分: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;
}
};