在Android中搜索自定义列表视图?

时间:2013-08-17 09:20:34

标签: android listview sqlite

我有一个带有搜索功能的自定义列表视图,同时搜索它显示不同ID中的所有列表?我错过了什么。任何人都可以找到解决方案, 提前致谢。

1.searchfunction class

sqlCon = new SQLiteConnectornew(this);
     sqlCon.open();

        Cursor cursor = SQLiteConnectornew.fetchAllvalues1();
        //cursor.close();
    String[] from = new String[] { Contactsnew.TITLE};
    int[] to = new int[] { R.id.title };
    custAdapter = new CustomContactsAdapternew(this,
            R.layout.activity_contactlist, null, from, to);
    setListAdapter(custAdapter);
    search = (EditText) findViewById(R.id.alertsearch);
    search.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            custAdapter.getFilter().filter(s.toString());  
        }

    });
    custAdapter.setFilterQueryProvider(new FilterQueryProvider() {
         public Cursor runQuery(CharSequence constraint) {
             return SQLiteConnectornew.fetchCountriesByName1(constraint.toString());
         }
     });
     listView.setAdapter(custAdapter);
     listView.setTextFilterEnabled(true);

2.db class

public SQLiteConnectornew(Context context) {
    sqlHp = new SQLiteHelpernew(context, Contactsnew.DB_NAME, null, 1);
}

public void insertContact(String title, String category, int _id1) {
    ContentValues cv = new ContentValues();
    cv.put(Contactsnew.TITLE, title);
    cv.put(Contactsnew.CATEGORY, category);
    cv.put(Contactsnew.userId, _id1);

    db = sqlHp.getWritableDatabase();
    db.insert(Contactsnew.TABLE2, null, cv);
    //db.close();

}

public void updateContact(long _id, int _id1, String title,
        String category) {
    ContentValues cv = new ContentValues();
    cv.put(Contactsnew.TITLE, title);
    cv.put(Contactsnew.CATEGORY, category);
    cv.put(Contactsnew.userId, _id1);
    //cv.put(Contactsnew.userid, _id);
    //cv.put(Contactsnew.IMAGE, blob);

    db = sqlHp.getWritableDatabase();
    db.update(Contactsnew.TABLE2, cv, Contactsnew.userid + "=_id" + " AND " + Contactsnew.userId + "=_id1", null);
    //db.update(Contactsnew.TABLE2, cv, Contactsnew.userid + "=_id", null);
    db.update(Contactsnew.TABLE2, cv, Contactsnew.TITLE + "=title", null);
    db.close();
}

public Cursor getAllContacts() {
    db = sqlHp.getReadableDatabase();
    cur = db.query(Contactsnew.TABLE2, null, "_id1=" + GetSet.getUserId(),
            null, null, null, "title");
    return cur;
public static Cursor fetchAllvalues1() {

      Cursor mCursor = db.query(Contactsnew.TABLE2, new String[] {Contactsnew.userid,Contactsnew.userId,Contactsnew.CATEGORY,Contactsnew.TITLE},
        null, null, null, null, null);

      if (mCursor != null) {
       mCursor.moveToFirst();
      }
      return mCursor;
     }
public static Cursor fetchCountriesByName1(String inputText) throws SQLException {
      Log.w(TAG, inputText);
      Cursor mCursor = null;
      if (inputText == null  ||  inputText.length () == 0)  {
       mCursor = db.query(Contactsnew.TABLE2, new String[] {Contactsnew.userid,Contactsnew.userId,Contactsnew.CATEGORY,Contactsnew.TITLE},
         null, null, null, null, null);

      }
      else {
       mCursor = db.query(true, Contactsnew.TABLE2, new String[] {Contactsnew.userid,Contactsnew.userId,Contactsnew.CATEGORY,Contactsnew.TITLE},
         Contactsnew.userId + " AND " +Contactsnew.TITLE + " like '%" + inputText + "%'", null,
         null, null, null, null);
      }
      if (mCursor != null) {
       mCursor.moveToFirst();
      }
      return mCursor;

     }

1 个答案:

答案 0 :(得分:0)

在你的sqlitedatabase我编辑了一些代码试试!!

public static Cursor fetchAllvalues1() {

      Cursor mCursor = db.query(Contactsnew.TABLE02, new String[] {Contactsnew.userid,Contactsnew.userId,Contactsnew.CATEGORY,Contactsnew.TITLE},
        null, null, null, null, null);

      if (mCursor != null) {
       mCursor.moveToFirst();
      }
      return mCursor;
     }
public static Cursor fetchCountriesByName1(String inputText) throws SQLException {
      Log.w(TAG, inputText);
      Cursor mCursor = null;
      if (inputText == null  ||  inputText.length () == 0)  {
       mCursor = db.query(Contactsnew.TABLE02, new String[] {Contactsnew.userid,Contactsnew.userId,Contactsnew.CATEGORY,Contactsnew.TITLE},
         null, null, null, null, null);

      }
      else {
          mCursor=db.rawQuery(" SELECT * FROM "+ Contactsnew.TABLE02 + " WHERE " 
                    + Contactsnew.userId + " = " + GetSet.getUserId() + " AND " + Contactsnew.TITLE +
                             " LIKE ? " ,new String []{inputText +"%"});
      }
      if (mCursor != null) {
       mCursor.moveToFirst();
      }
      return mCursor;

     }