我需要根據SearchDialog
中的用戶輸入更新我的ListView
。爲此,我需要爲SearchDialog
設置TextWatcher
。這個怎麼做?如何爲SearchDialog實現TextWatcher
0
A
回答
0
你可以如下做到這一點:
yourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// process new items for list view
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// push new items into adapter and call notifyDataSetChanged() on it
}
});
0
這裏是我確實是在幾個月前實現,你需要同樣的事情。當我使用sqlite語句從sqlite數據庫填充列表視圖時,我不得不根據用戶輸入對列表視圖進行排序。不知道這是不是最好的方式,但在我的情況下,它完美的作品。
所以我EditText
場我添加此(這名字是searchBar
):
searchString = "";
searchBar.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) {
// this is where actually I was changing the sqlite statement which sort my list view
names.clear();
categories.clear();
paths.clear();
count.clear();
cardId.clear();
myCardID.clear();
searchString = s.toString();
Log.e("","searchString : "+searchString);
sqlQuery = getSqlStatement(sort, collId, ascDesc,searchString);
Log.e(""," sqlquery : "+sqlQuery);
sqlQuery = getSqlStatement(sort, collId, ascDesc, searchString);
Cursor cursor = userDbHelper.executeSQLQuery(sqlQuery);
if (cursor.getCount() == 0) {
noCards.setVisibility(View.VISIBLE);
} else if (cursor.getCount() > 0) {
noCards.setVisibility(View.GONE);
for (cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
objectId = Integer.parseInt(cursor.getString(cursor.getColumnIndex("objectId")));
cardId.add(objectId);
title = cursor.getString(cursor.getColumnIndex("title"));
catTitle = cursor.getString(cursor.getColumnIndex("catTitle"));
if(extra!=0 && sort!=3){
tags.setText(catTitle);
}
int repeats = Integer.parseInt(cursor.getString(cursor.getColumnIndex("repeatsCount")));
count.add(repeats);
String cardsm = "SELECT objectId FROM cardmedias " +
"WHERE cardId="+
objectId +
" AND mediaType=" +
5012;
Cursor cardsM = userDbHelper.executeSQLQuery(cardsm);
if (cardsM.getCount() == 0) {
String defCard = "SELECT objectId FROM collectionmedias " +
"WHERE collectionId="+
collId +
" AND mediaType=" +
3003;
Cursor getDefCard = userDbHelper.executeSQLQuery(defCard);
getDefCard.moveToFirst();
objectID = Integer.parseInt(getDefCard.getString(getDefCard
.getColumnIndex("objectId")));
String filename = "mediacollection-"+objectID;
if(storageID==1){
path = RPCCommunicator.getImagePathFromInternalStorage(servername, userId, filename, getApplicationContext());
} else if(storageID==2){
path = RPCCommunicator.getImagePathFromExternalStorage(servername, userId, filename);
}
hm.put(objectID, path);
path = hm.get(objectID);
paths.add(path);
names.add(title);
categories.add(catTitle);
} else if (cardsM.getCount() > 0) {
for (cardsM.move(0); cardsM.moveToNext(); cardsM.isAfterLast()) {
objectID = Integer.parseInt(cardsM.getString(cardsM
.getColumnIndex("objectId")));
String filename = "mediacard-"+objectID;
if(storageID==1){
path = RPCCommunicator.getImagePathFromInternalStorage(servername, userId, filename, getApplicationContext());
} else if(storageID==2){
path = RPCCommunicator.getImagePathFromExternalStorage(servername, userId, filename);
}
hm.put(objectID, path);
path = hm.get(objectID);
paths.add(path);
names.add(title);
categories.add(catTitle);
}
}
cardsM.close();
}
}
cursor.close();
// don't forget to add this!!!
adapter.notifyDataSetChanged();
}
});
在我onCreate()
其實我在做同樣的事情,在TextWatcher
排序我的列表視圖,所以我覺得你可以像我一樣實施相同的邏輯。
如果您有任何問題需要做,請粘貼一些代碼,以便我們幫助您! :)
希望這有助於!
相關問題
- 1. 如何在活動上實現TextWatcher
- 2. ListView baseadapter類實現TextWatcher
- 3. 實現TextWatcher的Android RecycleView適配器(EditText)
- 4. 在多個edittext字段上實現TextWatcher
- 5. 我在哪裏使用TextWatcher實現?
- 6. 如何在我的Json準備好時實現TextWatcher?
- 7. 使用TextWatcher時出現NullPointerError
- 8. 如何爲ExUnit實現「assert_difference」
- 9. 如何爲ListView實現onItemClickListerner?
- 10. 如何爲Point3實現Slerp?
- 11. 如何爲此實現ActionListener?
- 12. 如何爲Xamarin.forms實現Lottie?
- 13. 如何爲twitter.getAccountSettings()實現AsyncTask:Android
- 14. 如何實現onCompleteListener爲DialogFragment
- 15. GetHashCode()如何爲Int32實現?
- 16. TextWatcher的行爲不如預期
- 17. 爲什麼需要TextWatcher
- 18. TextWatcher表現不同仿真器/電話
- 19. 如何將實現java.lang.Comparable的類轉換爲實現Scala.Ordered?
- 20. 如何實現
- 21. Android:激活操作欄中的SearchDialog
- 22. SearchDialog無法在ActivityGroup中顯示
- 23. EditText,如何控制TextWatcher中的光標?
- 24. 如何控制的EditText文本與textwatcher
- 25. 如何使用TextWatcher比較兩個值
- 26. 如何在android中使用TextWatcher?
- 27. 如何getText()內的TextWatcher從EditText
- 28. 如何避免使用textWatcher editText更改?
- 29. 如何在ActiveRecord中爲PostgreSQL實現'upsert`?
- 30. 如何將PostFilter實現爲PagingAndSortingRepository?
我知道這件事。我問的是我怎麼能實現這個爲SearchDialog(quicksearchbox) – 2012-03-22 09:38:30
沒有顯示您的代碼,這將是很難猜測答案 – waqaslam 2012-03-22 09:43:10
我想要的東西類似於QSB的SearchView.OnQueryTextListener。我無法使用SearchView.OnQueryTextListener,因爲它只支持API 11+。希望現在清楚。 – 2012-03-22 13:21:52