0
我正在創建一個具有搜索在線數據庫的搜索功能的Android應用程序。當我想搜索某些東西時,我必須雙擊搜索按鈕才能顯示正確的結果。我相信這個問題是因爲我在onClick按鈕中使用了Asynctask函數。這是我的onClick:如何通過一次點擊而不是兩次顯示搜索結果?
public void onSearchClick(View view) throws TVDBOutboundConnectionException {
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
// fetch data
new SeriesSearchTask().execute(text.getText().toString());
} else {
// display error
}
final ListView listview = (ListView) findViewById(R.id.listview);
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < ALTS.size(); i++) {
list.add(ALTS.get(i).getSeriesName());
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
view.animate().setDuration(2000).alpha(0)
.withEndAction(new Runnable() {
@Override
public void run() {
list.remove(item);
adapter.notifyDataSetChanged();
view.setAlpha(1);
}
});
}
});
}
這是我的AsyncTask方法:
private class SeriesSearchTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
ALTS = DB.searchSeriesInTVDB(urls[0], Language.ENGLISH);
return Integer.toString(ALTS.size());
} catch (TVDBOutboundConnectionException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
}
}
所以我的問題是如何獲取的方法來顯示一個搜索點擊正確的結果,因爲這將是正常的並期待。任何幫助表示感謝,並提前感謝您的努力。
非常感謝!我會閱讀關於事件公共汽車的信息,因爲這將是很好的瞭解以供將來參考。 – Skeens93