0
我有一個自定義列表視圖與搜索功能,同時搜索它顯示所有列表目前在不同的ID?我缺少的東西。任何人都可以找到解決方案, 感謝提前。在Android中搜索自定義列表視圖?
1.searchfunction類
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類
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;
}
究竟是什麼問題? – Pavlos
如果你使用更多的textview,那麼它會搜索所有。爲此,您必須在適配器類中編寫自己的filter()方法。 –
我的問題是它的過濾所有列表中存在的不同的ID,我想只搜索特定的ID列表!!例如:如果假設我登錄爲ID:86我會添加一些數據,它會顯示爲列表,同時搜索它顯示其他ID中出現的所有內容! – ajey