繁体   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