如何创建将从远程服务器获取结果的AutoCompleteTextView?
据我所知,我需要实现ArrayAdapter,它必须向服务器发出异步请求.
AutoCompleteTextView 我不是为了这种行为,我曾经像你一样思考并且用那个做了非常糟糕的代码......
如果你的自动完成列表来自他根据你输入的每个字符的网络,你最好创建一个包含EditText和listView的垂直线性布局,并创建一个连接池.
对于每个密钥类型,检查是否在100-130毫秒内输入以确保用户可能等待响应并发出请求,在任何新请求中,删除或使您发送的最后一个请求无效.
一个响应安全到达,更新列表适配器.
对我来说很顺利.
AutoCompleteTextview使用预先制作的列表.它使用AsyncTask本身,所以我自己用它来更新它使用的列表是非常糟糕的...
1)您必须首先为 asyncTask 创建一个类,并且必须在其 doInBackground() 方法中建立到远程服务器的连接
2)你必须从remoteServer的响应中制作arrayAdapter,这也必须在doInBackground()方法中制作
3)成功后,您必须将适配器设置为AutoCompleteTextView
new AsyncTask<Integer, Void, arrayList> () {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(context, "downloading...", "Please wait...");
super.onPreExecute();
}
@Override
protected void onPostExecute(arrayList result) {
//make arrayAdapter from result
//set adapter to AutoCompleteTextView
progressDialog.dismiss();
super.onPostExecute(result);
}
@Override
protected arrayList doInBackground(Integer... params) {
// make connection to remote server
//retrive response from remote server
// make arrayList from response
return arrayList
}
}.execute(1);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2463 次 |
| 最近记录: |