【发布时间】:2015-02-27 23:57:40
【问题描述】:
我正在编写这个从 rottentomatoes API 获取电影信息的应用程序。我正在使用客户端进行解析和 httprequests。
首先我尝试设置一个搜索视图,但由于我使用的是 framents (FragmentNavigationDrawer) 这不是一个选项。
下一个最好的事情。带有 ListPopupWindow 的 EditText。
我设置了 textchangelistener,这样当我开始在给定的 edittext(search) 中输入内容时,它应该执行 asynctask(使用 edittext 输入作为 api 请求的搜索词)。然而,什么也没有发生。不是一回事。 logcat中没有一行红色文字,app不会崩溃,什么都没有。所以我在 stackoverflow 求助于你的神级编码员。
片段:
public class SecondFragment extends Fragment{
ListPopupWindow listPopupWindow;
EditText search;
List<Movie> result = new ArrayList<Movie>();
private MovieAdapter adpt;
MovieSearch searchTask = new MovieSearch();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.secondfragment, container, false);
search = (EditText) view.findViewById(R.id.searchBox2);
listPopupWindow = new ListPopupWindow(getActivity());
adpt = new MovieAdapter(new ArrayList<Movie>(), getActivity());
searchTask = new MovieSearch();
listPopupWindow.setAdapter(adpt);
listPopupWindow.setAnchorView(search);
listPopupWindow.setWidth(300);
listPopupWindow.setHeight(400);
listPopupWindow.setModal(true);
search.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3){
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3){
}
@Override
public void afterTextChanged(Editable editable){
searchTask.cancel(true);
String input = search.getText().toString();
searchTask.execute(input);
}
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.secondfragment, container, false);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View view) {
// detect the view that was "clicked"
switch (view.getId()) {
case R.id.addToCollection: //just a placeholder
break;
}
}
private class MovieSearch extends AsyncTask<String, Void, List<Movie>> {
@Override
protected void onPostExecute(List<Movie> result) {
super.onPostExecute(result);
adpt.setMovieList(result);
adpt.notifyDataSetChanged();
}
@Override
protected List<Movie> doInBackground(String... params){
Log.e("Status", "Entered thread");
String input = (params[0]);
JTomato jtomato = new JTomato("jf6cykts4sybqrex3dkjd7uq");
jtomato.setPage_limit(1);
jtomato.searchMovie(input, result, 0);
if (isCancelled())
{
//Notify your activity that you had canceled the task
return (null);
}
return result;
}
}
}
【问题讨论】:
-
请放置 "Log.e("Status","Entered thread");"在您的异步后台执行并检查它是否进入
-
你怎么知道的?看起来您总是从
doInBackground()返回null。而且你不能重新启动这样的任务。您每次都需要创建一个新的任务实例。MovieSearch searchTask.execute();除此之外,这不是您想要的方式。每次键入字母时都尝试取消并重新启动任务? -
我一定对取消任务有误解,我需要更改状态并在之后执行(我要么迟钝和/或疲倦)。你认为我应该如何进行搜索?有没有办法在键盘上按下“回车键”时开始搜索?此搜索仅用于查找电影。之后,我添加了一个不同的 asyncTask 来从该电影中获取详细信息。
-
首先,从
doInBackground()返回一个List<Moviess>,而不是返回null。其次,制作一个“搜索”按钮,并在单击按钮时执行 MovieSearch AsyncTask,而不是每次输入一个字母。 -
尝试将所有逻辑从 onTextChanged 移动到 afterTextChanged,并且每次都使用新的 MovieSearch 启动“searchTask”
标签: android android-asynctask fragment textchanged