【问题标题】:What is the best way to retrieve JSON data from database for Android AutoCompleteTextView?从 Android AutoCompleteTextView 的数据库中检索 JSON 数据的最佳方法是什么?
【发布时间】:2016-04-03 19:18:52
【问题描述】:

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

例如:

比如如果用户输入"a",那么它将检索"a"的最佳结果。然后,如果用户输入 "ab",它将被刷新并填充来自数据库的新结果。

谢谢

【问题讨论】:

  • 如果您不想将数据保存在本地 SQLite 数据库中,您可以将基本密钥下载到 JSON 文件(文本文件)中并从中搜索。

标签: android json autocompletetextview


【解决方案1】:

我只会为您的任务提供一个大概的概述,看起来像这样,

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;
    }

详情请见here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多