从Android AutoCompleteTextView的数据库中检索JSON数据的最佳方法是什么?

时间:2016-04-03 10:46:39

标签: android json autocompletetextview

我想为Android开发一个动态AutoCompleteTextView,其中填充了从 MySQL 数据库中检索到的JSON数据。基本上这不是一项艰巨的任务,但我面临的问题是什么?我受到 jQuery Autocomplete选项的启发,在输入字母后动态获取数据。但是android AutoCompleteTextView预先填充了所有JSON数据。
我试图从数以百万计的数据中查询真正难以存储的数据。有没有办法动态搜索数据库的输入?

E.g:

  

如果用户输入“a”,则会检索“a”的最佳结果。然后,如果用户输入“ab”,它将被刷新并填充来自数据库的新结果。

由于

1 个答案:

答案 0 :(得分:2)

我将仅为您的任务提供一般概述,如下所示

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 - &gt;用于从服务器

中检索新值
 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;
    }

有关详细信息,请参阅here

相关问题