对于Android AutoCompleteTextView,从数据库检索JSON数据的最佳方法是什么?
我想为Android开发一个动态的对于Android AutoCompleteTextView,从数据库检索JSON数据的最佳方法是什么?,android,json,autocompletetextview,Android,Json,Autocompletetextview,我想为Android开发一个动态的AutoCompleteTextView,其中包含从MySQL数据库检索到的JSON数据。基本上这不是一项艰巨的任务,但我面临的问题是什么?我的灵感来自于jQuery的自动完成选项,该选项在输入字母后动态获取数据。但是安卓AutoCompleteTextView预先填充了所有JSON数据 我试图从数百万的数据中进行查询,这些数据很难存储。有没有办法在数据库中动态搜索输入 例如: 例如,如果用户输入“a”,则它将检索“a”的最佳结果。然后,如果用户键入“ab”,它
AutoCompleteTextView
,其中包含从MySQL数据库检索到的JSON
数据。基本上这不是一项艰巨的任务,但我面临的问题是什么?我的灵感来自于jQuery的自动完成选项,该选项在输入字母后动态获取数据。但是安卓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);
}
});
公开列表建议//建议清单
autoComplete.addTextChangedListener(新的TextWatcher(){
public void PostTextChanged(可编辑){
//TODO自动生成的方法存根
}
更改前文本之前的公共void(字符序列s、int start、int count、int after){
//TODO自动生成的方法存根
}
public void onTextChanged(字符序列、int start、int before、int count){
字符串newText=s.toString();
新建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;
}
类getJson扩展异步任务{
@凌驾
受保护的字符串背景(字符串…键){
字符串newText=键[0];
newText=newText.trim();
newText=newText.replace(“,“+”);
试一试{
//用于检索数据以获取建议的代码
建议=新建ArrayList();
JSONArray jArray=新JSONArray(数据);
for(循环数组){
String SuggestKey=//通过迭代检索值;
suggest.add(SuggestKey);
}
}捕获(例外e){
w(“Error”,e.getMessage());
}
//填充建议
runOnUiThread(新的Runnable(){
公开募捐{
aAdapter=newarrayadapter(getApplicationContext(),R.layout.item,suggest);
自动完成设置适配器(AAAdapter);
aAdapter.notifyDataSetChanged();
}
});
返回null;
}
有关详细信息,请参阅。我将仅为您的任务提供一个大致概述,如下所示,
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);
}
});
公共列表建议;//建议列表
autoComplete.addTextChangedListener(新的TextWatcher(){
public void PostTextChanged(可编辑){
//TODO自动生成的方法存根
}
更改前文本之前的公共void(字符序列s、int start、int count、int after){
//TODO自动生成的方法存根
}
public void onTextChanged(字符序列、int start、int before、int count){
字符串newText=s.toString();
新建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;
}
类getJson扩展异步任务{
@凌驾
受保护的字符串背景(字符串…键){
字符串newText=键[0];
newText=newText.trim();
newText=newText.replace(“,“+”);
试一试{
//用于检索数据以获取建议的代码
建议=新建ArrayList();
JSONArray jArray=新JSONArray(数据);
for(循环数组){
String SuggestKey=//通过迭代检索值;
suggest.add(SuggestKey);
}
}捕获(例外e){
w(“Error”,e.getMessage());
}
//填充建议
runOnUiThread(新的Runnable(){
公开募捐{
aAdapter=newarrayadapter(getApplicationContext(),R.layout.item,suggest);
自动完成设置适配器(AAAdapter);
aAdapter.notifyDataSetChanged();
}
});
返回null;
}
有关详细信息,请参阅。如果不想将数据保存在本地SQLite数据库中,可以将基本密钥下载到JSON文件(文本文件)中并从中进行搜索。如果不想将数据保存在本地SQLite数据库中,可以将基本密钥下载到JSON文件(文本文件)中并从中进行搜索。