我是Android的初学者。因此我首先用活动设计我的项目,一切正常。然后,为了使用导航抽屉,我将我的活动改为片段。
我用一个装载机来填写清单。当用户提交搜索时,我重新启动搜索结果的加载程序。
我的片段:
public class NewsListFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<News>> {
private static final int NEWS_LOADER_ID = 2;
private NewsAdapter adapter;
public NewsListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_news_list, container, false);
setHasOptionsMenu(true);
if(isNetworkConnected()) {
// Definition NewsAdapter with empty ArrayList (new ArrayList()) and appropriation it to adapter
adapter = new NewsAdapter(getActivity(), 0, new ArrayList());
// Set listView adapter
listView.setAdapter(adapter);
// Initialize loader
getActivity().getSupportLoaderManager().initLoader(NEWS_LOADER_ID, null, this);
}
else {
...
}
});
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.news_list_menu, menu);
MenuItem menuItem = menu.findItem(R.id.option_search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
if (query!=null && !query.isEmpty()) {
Bundle bundle = new Bundle();
bundle.putString("query_key", query);
getActivity().getSupportLoaderManager().restartLoader(NEWS_LOADER_ID, bundle, this);
}
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.option_settings) {
...
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public Loader<List<News>> onCreateLoader(int id, Bundle args) {
String query = "";
if (args!=null){
query = args.getString("query_key");
}
/* Definition Uri and append his relevant parameters */
Uri baseUri = Uri.parse("...");
Uri.Builder uriBuilder = baseUri.buildUpon();
uriBuilder.appendQueryParameter("q", query);
/* Define NewsLoader with uri */
return new NewsLoader(getActivity(), uriBuilder.toString());
}
@Override
public void onLoadFinished(Loader<List<News>> loader, List<News> newsList) {
adapter.clear();
if(newsList!=null && !newsList.isEmpty()) {
adapter.addAll(newsList);
}
}
@Override
public void onLoaderReset(Loader loader) {
adapter.clear();
}
/* Check connectivity and network status */
private boolean isNetworkConnected() {
...
}
}
排队:
getActivity().getSupportLoaderManager().restartLoader(NEWS_LOADER_ID, bundle, this);
表示“这个”是一个错误。谁说:
wrong 3rd argument type... required android.support.v4.app.LoaderManager.LoaderCallbacks<java.lang.object>
我甚至尝试过更改此行:
getActivity().getSupportLoaderManager().restartLoader(NEWS_LOADER_ID, bundle, (LoaderManager.LoaderCallbacks<List<News>>)getActivity()
但它崩溃了,并在我的logcat中显示以下内容:
MainActivity无法强制转换为android.support.v4.app.LoaderManager $ LoaderCallbacks
我明白onCreateView()中的加载器对“this”没问题! 但在onCreateOptionsMenu()&gt; searchView.setOnQueryTextListener()&gt; onQueryTextSubmit 有问题!
有谁能告诉我我要写什么而不是“这个”用于搜索?
我的进口商品:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v7.widget.SearchView;
答案 0 :(得分:1)
在onQueryTextSubmit
上,这是指您正在创建的匿名内部类,其中new SearchView.OnQueryTextListener(
未实现LoaderManager.LoaderCallbacks<List<News>>
。使用
getLoaderManager().restartLoader(NEWS_LOADER_ID, bundle, NewsListFragment.this);
应解决您的问题