我已成功将我的AutoCompleteTextView链接到MySQL中的数据,此外我正在使用AsyncTask。我想知道的是如何在TextView中显示下拉列表。我知道我正确地获取数据,因为它显示在我的 System.err.println()方法中。这是我的代码..
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_httpentity);
AutoCompleteTextView locationupdate;
// This is an issue I do not know how to hook
// My autocompleteTextview here
AutoLocation mytask= new AutoLocation();
mytask.execute();
}
private class AutoLocation extends AsyncTask<String,String,String> {
String line,tryy="";
AutoCompleteTextView locationupdate;
@Override
protected void onPreExecute() {
super.onPreExecute();
locationupdate=(AutoCompleteTextView)findViewById(R.id.locationupdate);
}
@Override
protected String doInBackground(String...params) {
try {
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
// Other code here that I omitted because it is correct
while ((line = reader.readLine()) != null) {
tryy+= result.append(line);
}
// tryy contains the MySQL data and it works successfully
// because I can see output in System.err.println()
System.err.println(tryy);
}
catch (IOException e) {
e.printStackTrace();
}
return tryy;
}
protected void onPostExecute(List<String> result)
{
// How can I pass tryy into my adapter here?
ArrayAdapter<String> adapter= new ArrayAdapter<>
(Httpentity.this,android.R.layout.simple_dropdown_item_1line,result);
locationupdate.setAdapter(adapter);
}
//Async Ends
}
从上面可以看出, tryy 字符串包含来自MySQL的数据。我只是不知道如何将其传递给 onPostExecute 中的适配器。另一个问题仅仅是我现在如何将其传递给onCreate方法?例如,如果这是一个click方法,那么你将使用setOnclick然后在点击时执行Async,但是对于AutocompleteTextview呢?这是关于TextWatcher的,但我似乎无法实现任何建议都会很棒。 AutoCompleteTextView with MySQL Data
答案 0 :(得分:0)
ArrayAdapter<String> adapter= new ArrayAdapter<>(Httpentity.this,android.R.layout.simple_dropdown_item_1line,result);
locationupdate.setAdapter(adapter);
之后放locationupdate.showDropDown();