使用SearchView和SQLite数据库和碎片搜索ListView麻烦

时间:2018-02-15 16:43:15

标签: android sqlite listview searchview

我在项目中遇到SearchView问题。以下是片段的搜索组件:

    public class SupplementFragment extends Fragment implements . AddDialog.AddDialogListener
{
// declare java references
private TextView msgTextView;
private Button btnAdd;
private ListView lvSupplements;
private DatabaseHandler dbh;
private MyCursorAdapter adapter;


private SearchView svSearchSupp;
//SearchManager searchManager;

// reference to hold fragment
private SupplementFragment frag;

//DatabaseHandler dataBase;

// class method to create and return object of the fragment
public static SupplementFragment newInstance() {

    SupplementFragment fragment = new SupplementFragment();
    return fragment;
}

// default constructor
public SupplementFragment(){}

// Called to create the view hierarchy associated with the fragment.
// container from Activity holding the fragment.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    // inflate the xml layout file into the fragment, save the rootview
    View rootView = inflater.inflate(R.layout.supplement_frag, container, false);

    // test the supplement table
    DatabaseTest.populateSupplementTable(this.getContext());


    // use the rootView and findViewById to create Java object tied to the resource
    msgTextView = rootView.findViewById(R.id.msgTextView);
    msgTextView.setText("Supplement Frag Example");
    btnAdd = rootView.findViewById(R.id.btnAdd);
    lvSupplements = rootView.findViewById(R.id.lvSupplements);
    lvSupplements.setTextFilterEnabled(true);

    // search controls
    svSearchSupp = rootView.findViewById(R.id.svSearchSupp);
    setupSearchView();

    // create the database handler object
    dbh = new DatabaseHandler(this.getContext());
   // Cursor c = dbh.getReadableDatabase().rawQuery(DatabaseHandler.TABLE_SUPPLEMENT, null);

    // create and set the cursorAdapter to the business listView
    adapter = createMyCursorAdapter();
    lvSupplements.setAdapter(adapter);
    // use getActivity to get reference to the Activity if needed
    MainActivity ma = (MainActivity)getActivity();

    svSearchSupp.setOnQueryTextListener(new 
    SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            try
            {
                //adapter.getFilter().filter(newText);
                getSupplements(newText);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return false;
        }
    });

      Log.i("Fragment", "Supplement onCreateView");

    return rootView;
}

我也有为搜索功能创建的这两种方法(在同一片段中):

private void getSupplements(String searchTerm) throws Exception {

    //dbh.retrieve(searchTerm);
    SupplementInfo s = null;
    Cursor c = dbh.retrieve(searchTerm);
    adapter.getFilter().filter(searchTerm);
    adapter.swapCursor(dbh.getAllSupplementObjects());
    adapter.notifyDataSetChanged();

}

private void setupSearchView() {
    //svSearchSupp.setIconifiedByDefault(false);
    //svSearchSupp.setOnQueryTextListener(this);
    //svSearchSupp.setSubmitButtonEnabled(true);
    svSearchSupp.setQueryHint("Search...");
} 

这是'检索'我的DatabaseHandler类的方法:

    public Cursor retrieve(String searchTerm) {

    db = this.getWritableDatabase();

    String [] columns = {DatabaseHandler.COL_ID, DatabaseHandler.COL_NAME, DatabaseHandler.COL_INFO};
    Cursor c = null;

    if(searchTerm != null && searchTerm.length() > 0) {

        String sql = "SELECT * FROM " + DatabaseHandler.TABLE_SUPPLEMENT + " WHERE " + DatabaseHandler.COL_NAME +
                " LIKE '%" + searchTerm + "%'"; //
        c = db.rawQuery(sql, null);

    }
    c = db.query(DatabaseHandler.TABLE_SUPPLEMENT, columns, null, null, null, null, null);
   //  c = db.rawQuery("SELECT * FROM TABLE_SUPPLEMENTS", null);
    db.close();
    return c;
}

所以,现在我认为我已经有足够的代码供您使用了。问题是,当我单击片段中的SearchView并开始输入时,ListView不会更改或不移动。我也没有在LogCat中得到任何错误以了解问题所在。任何帮助将不胜感激,如果不清楚,我道歉,我可以提供任何其他细节。

0 个答案:

没有答案