我有一個工作的搜索功能,插入搜索結果到微調,但我希望它是沒有需要按搜索按鈕就像谷歌。 事情是這樣的:我怎樣才能在我的Android應用程序查詢我的MYSQL數據庫,而無需按搜索
回答
嗯,首先,你需要使用一個AutoCompleteTextView,即每次觸發一個過濾器,文本更改,並根據您附加的圖像精確顯示建議。
然後,你必須是你的自定義適配器,並把它添加到您的視圖在你的活動你的onCreate功能:從
protected void onCreate(Bundle savedInstanceState) {
...
AutomaticSearchAdapter adapter = new AutomaticSearchAdapter(this, new ArrayList<CustomObject>(), "");
listView = (AutoCompleteTextView) findViewById(R.id.search_complete);
listView.setThreshold(1);
listView.setAdapter(adapter);
...
}
下面是如何構建自定義適配器的模板,該載荷數據任何API和根據您自己的過濾標準建議
public class AutomaticSearchAdapter extends ArrayAdapter implements AsyncResponse, Filterable {
private ArrayList<ContactItem> realList; // The list that will be shown
private ArrayList<CustomObject> results = new ArrayList<CustomObject>(); // A list with all data from your API
private Filter mFilter = new Filter(){
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if(constraint != null) {
String constraintTrim = ContactSearchAdapter.toPlain(constraint.toString()).trim().toLowerCase();
// When te user introduces at least 3 characteres, get data from your API
if (constraintTrim.length() == 3) {
// Call Here to you API, and populate results
}
ArrayList<CustomObject> suggestions = new ArrayList<CustomObject>();
for (CustomObject object : results) {
if(/*some condition on your downloaded datum */) {
suggestions.add(object);
}
}
filterResults.values = suggestions;
filterResults.count = suggestions.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence contraint, FilterResults results) {
if(results == null){
return;
}
try {
realList.clear();
if (results.values != null){
realList.addAll((List<CustomObject>) results.values);
}
notifyDataSetChanged();
}catch (Exception e) {
}
}
};
public int getCount() {
return realList.size();
}
@Override
public Object getItem(int position) {
try {
if (realList.size() > 0) {
return realList.get(position);
}else {
return null;
}
}catch (Exception ex) {
return null;
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// HERE MUST BE YOUR CUSTOM getView FROM YOUR LIST
// YOU CAN LEARN MORE ABOUT http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67
return convertView;
}
@Override
public void postStart() {
//Nothing to do.
}
@Override
public Filter getFilter() {
return mFilter;
}
}
當然,您將需要重構上面的代碼以滿足自己的需求。
我會做一個函數,抓取我的搜索結果,並將它們放在一個隱藏的ArrayList中,然後我將使用上面的代碼使它從已經解析的數據中搜索來獲得我想要的結果。但是,如果數據庫規模很大,這不會是不好的做法。 – Elias
總是要向API發出獲取請求,您必須設置LIMIT,但它會干擾結果,因爲查詢不在隱藏的ArrayList上。因此,在這種情況下最好的做法是向API發出請求,直到結果大於LIMIT,並且結果小於LIMIT時,可以停止執行Web請求,並且只對您隱藏的ArrayList進行過濾 –
我bevieve你需要的是一個onTextChanged監聽到這裏看看https://developer.android.com/reference/android/text/TextWatcher.html
- 1. Android:我怎樣才能讓我的應用程序多語言?
- 2. 我怎樣才能在MySQL
- 3. Android-我怎樣才能在SQL應用程序中附加SQLite數據庫
- 4. 我怎樣才能得到我的數據庫的內容,而不是索引
- 5. 我怎樣才能從MySQL查詢與PHP的索引值?
- 6. 我怎樣才能在我的應用程序散列密碼
- 7. 我怎樣才能在android
- 8. 我怎樣才能改變搜索條
- 9. Symfony的1.4 - 我怎樣才能查詢
- 10. MySql,我怎樣才能從我的開發數據庫導出索引到我的生產數據庫?
- 11. 我怎樣才能讓我的數據庫連接UTF8啓用
- 12. 我怎樣才能無需提交
- 13. 我怎樣才能把我的Android應用程序的快照/佈局
- 14. 我怎樣才能從我的web應用程序進行android API調用?
- 15. 我需要MySQL子查詢嗎?我怎樣才能得到這些結果?
- 16. 我怎樣才能排序我用MYSQL過濾的類別?
- 17. 我們怎樣才能在Android 1.6的
- 18. 我怎樣才能在Android的
- 19. 我們怎樣才能在android中放置數據庫?
- 20. Android,我怎樣才能讓我的應用程序將要卸載
- 21. 我怎樣才能
- 22. 我怎樣才能使用這個MySQL查詢laravel 5.2?
- 23. 我怎樣才能查詢從表在MySQL
- 24. 我怎樣才能檢索mySql數據庫與PHP的散列密碼?
- 25. 我需要一個數據庫在我的Windows手機應用程序(芒果) - 我怎樣才能把一個數據庫在沙箱中,並使用它
- 26. 我怎樣才能在我的程序中獲得_GLOBAL_OFFSET_TABLE_地址?
- 27. 我怎樣才能從數據庫獲取數據在PHP中使用MySQL?
- 28. Redis:我怎樣才能按鍵排序我的散列?
- 29. 我怎樣才能訪問數據庫android appication到我的電腦
- 30. 我怎樣才能駝峯啓用Vim的搜索
沿着這些線? https://developer.android.com/guide/topics/search/adding-custom-suggestions.html –