2

我在过滤我的 listView 时遇到问题。每当我尝试在我的 editText 中输入文本时,什么都没有改变。

也许问题是关于这个..我的 mCursor 声明

Cursor mCursor = myDatabaseHelper.getDirectoryList(null);

我试图将参数从 null 更改为“Android”,输出在我的 listView 中显示术语 Android .. 但如果参数设置为 null,那么我的字典中的每个术语都会显示在 listView 中。

我在同一个类中有另一个调用 getDirectoryList 的代码片段,但似乎该片段中的参数被忽略了。请帮我..

这是代码

 public class SearchActivity extends Activity{

private DatabaseHelper myDatabaseHelper;

  /* Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.search_layout);

      myDatabaseHelper = new DatabaseHelper(this);
      try {
        myDatabaseHelper.createDataBase();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
      myDatabaseHelper.openDataBase();


      Cursor mCursor = myDatabaseHelper.getDirectoryList(null);


      String[] from = new String[]{DatabaseHelper.KEY_CONTENT};
      int[] to = new int[]{R.id.text1};
      SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, mCursor, from, to);
        startManagingCursor(mCursor);

        ListView listContent = (ListView)findViewById(R.id.contentlist);
        listContent.setAdapter(cursorAdapter);

      listContent.setFastScrollEnabled(true);
      listContent.setTextFilterEnabled(true);

      EditText etext=(EditText)findViewById(R.id.editText1);
      etext.addTextChangedListener(new TextWatcher() {
          public void onTextChanged(CharSequence s, int start, int before, int count) {
          }

          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
          }

          public void afterTextChanged(Editable s) {
              ListView listContent = (ListView)findViewById(R.id.contentlist);
              SimpleCursorAdapter filterAdapter = (SimpleCursorAdapter)listContent.getAdapter();
              filterAdapter.getFilter().filter(s.toString());
              //Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
          }
      });

      myDatabaseHelper = new DatabaseHelper(this);
     cursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
          public Cursor runQuery(CharSequence constraint) {
              //myDatabaseHelper = new DatabaseHelper(this);
              //int x = constraint.toString().length();
              myDatabaseHelper.getDirectoryList(constraint);
              Toast.makeText(getApplicationContext(), constraint, Toast.LENGTH_SHORT).show();

              return myDatabaseHelper.getDirectoryList(constraint);

          }
        });


      myDatabaseHelper.close();

和我的 getDirectory 类

 public Cursor getDirectoryList (CharSequence constraint)  {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(DB_NAME);

    String[] columns = new String[]{KEY_ID, KEY_CONTENT, KEY_DESCRIPTION, KEY_NETWORKING, KEY_HARDWARE, KEY_SOFTWARE, KEY_INTERNET, KEY_TECHNOLOGY};


    if (constraint == null  ||  constraint.length () == 0)  {
        //  Return the full list
        //String[] columns = new String[]{KEY_ID, KEY_CONTENT, KEY_DESCRIPTION, KEY_NETWORKING, KEY_HARDWARE, KEY_SOFTWARE, KEY_INTERNET, KEY_TECHNOLOGY};Toast.makeText(getContext(), constraint, Toast.LENGTH_SHORT).show();

        Cursor cursor = myDataBase.query("MY_TABLE", 
                 columns, KEY_TECHNOLOGY + "='Y'", null, null, null, null);
        return cursor;
    }  else  {
            String value = constraint.toString()+"%";
            Cursor cursor2 = myDataBase.query("MY_TABLE", columns, KEY_CONTENT + " like ?", new String[]{value}, null, null, null);
            return cursor2;
        }
    }

}
4

0 回答 0