簡體   English   中英

從Android AutoCompleteTextView的數據庫中檢索JSON數據的最佳方法是什么?

[英]What is the best way to retrieve JSON data from database for Android AutoCompleteTextView?

我想為Android開發一個動態的AutoCompleteTextView ,其中填充了從MySQL數據庫檢索的JSON數據。 基本上這不是一項艱巨的任務,但我面臨的問題是什么? 我受到jQuery Autocomplete選項的啟發,該選項在輸入字母后動態獲取數據。 但是android AutoCompleteTextView預先填充了所有JSON數據。
我正在嘗試從數百萬個很難存儲的數據中查詢。 有什么方法可以動態搜索數據庫的輸入嗎?

例如:

例如,如果用戶輸入“ a”,則它將檢索“ a”的最佳結果。 然后,如果用戶鍵入“ ab” ,它將被刷新並用數據庫中的新結果填充。

謝謝

我只給您一個大致的概述,看起來像這樣,

public List<String> suggest; //List of suggestions

 autoComplete.addTextChangedListener(new TextWatcher(){

        public void afterTextChanged(Editable editable) {
            // TODO Auto-generated method stub

        }

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

        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String newText = s.toString();
            new getJson().execute(newText);
        }

    });

getJson AsyncTask- >用於從服務器檢索新值

 class getJson extends AsyncTask<String,String,String>{

    @Override
    protected String doInBackground(String... key) {
        String newText = key[0];
        newText = newText.trim();
        newText = newText.replace(" ", "+");
        try{
            //Codes to retrieve the data for suggestions
            suggest = new ArrayList<String>();
            JSONArray jArray = new JSONArray(data);
            for(loop the array){
            String SuggestKey = //retrieve values by iterating;
            suggest.add(SuggestKey);
            }

        }catch(Exception e){
            Log.w("Error", e.getMessage());
        }

       //Populate suggestions

        runOnUiThread(new Runnable(){
            public void run(){
                 aAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item,suggest);
                 autoComplete.setAdapter(aAdapter);
                 aAdapter.notifyDataSetChanged();
            }
        });

        return null;
    }

詳細信息請參見此處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM