AutoCompleteTextView使用MySQL数据 [英] AutoCompleteTextView with MySQL Data
问题描述
我有一个 AutoCompleteTextView
,我需要建议从的MySQL
表名。我只用一次硬编码一个字符串数组使用此之前
。
我只看到一个例子,在此链接,它真的是没有太大的帮助。我需要用的AsyncTask
来做到这一点。我知道如何处理在 AsynTask
部分,但这里是我的问题:
我将需要发送类型为参数
为任务
这句话的每一个部分。我在哪里得到这个文本字符串
?
我能看到的唯一方法是在 TextWatcher
也许。那是不是呢,还是有其他方法?
那是不是呢,还是有其他方法?
块引用>您只需要某种机制,将手表在您的输入框的变化,最正确的方式如何实现它被提及
TextWatcher
因此,实现它,并在一些提供方法
TextWatcher
例如的 onTextChanged(),从输入框分配数据和发送它们作为参数的的AsyncTask 的和的 onPostExecute()的方法为创建新的适配器你的AutoCompleteTextView
与的MySQL
检索到的数据并分配适配器的小部件,你得到它。伪code:
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
如果(s.length()→1){
insertString = s.toString();
新YourTask()执行(insertString);
}
}和在你的AsyncTask,执行这样的事情:
保护名单,LT;弦乐> doInBackground(){
从MySQL // fetchning数据
返回列表;
}公共无效onPostExecute(列表<串GT;的结果){
如果(!result.isEmpty()){
SomeAdapter ADP =新SomeAdapter(背景下,布局,结果);
actv.setAdapter(ADP);
}
}注意:你的情况是easer让你的
的AsyncTask
内部类的活动的
类,你必须UI
组件直接访问,而无需通过构造函数传递它们。I have an
AutoCompleteTextView
that I need to suggest names from aMySQL
table. I have only once used this before with hard coding astring array
.I have only seen one example, at this link, and it really wasn't much of a help. I need to do this with an
AsyncTask
. I know how to handle theAsynTask
portion, but here are my questions:I'm going to have to send each part of the phrase typed as a
param
for thetask
. Where do I get this textstring
?The only method I can see is a
TextWatcher
maybe. Would that be it, or is there another method?解决方案Would that be it, or is there another method?
You simply need some mechanism that will "watch" changes in your inputbox and the most correct way how to achieve it is mentioned
TextWatcher
So implement it and in some method that provides
TextWatcher
for instance onTextChanged(), assign data from inputbox and send them as parameter to AsyncTask and in onPostExecute() method create new Adapter for yourAutoCompleteTextView
with data retrieved fromMySQL
and assign Adapter to your widget and you got it.Pseudo-code:
public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.length() > 1) { insertString = s.toString(); new YourTask().execute(insertString); } }
and in your AsyncTask, perform something like this:
protected List<String> doInBackground() { // fetchning data from MySQL return list; } public void onPostExecute(List<String> result) { if (!result.isEmpty()) { SomeAdapter adp = new SomeAdapter(context, layout, result); actv.setAdapter(adp); } }
Note: In your case is easer to make your
AsyncTask
inner class of yourActivity
class and you have direct access toUI
components without passing them via constructor.这篇关于AutoCompleteTextView使用MySQL数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!