我有一个主要障碍试图在我的列表中搜索使用来自预填充 SQLite 数据库的游标生成的数据。
我正在使用以下库:
- 动作酒吧夏洛克。
- SQLite 资产助手(以简化从我的数据库加载数据)。
- 加载器Ex。
当我启动我的应用程序时,列表是空的,当我在 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);
}
}
我究竟做错了什么?先感谢您。