我是安卓的初学者。因为这个原因,我第一次用活动设计我的项目,一切都很好。然后,为了使用导航抽屉,我将我的活动更改为片段。
我有一个显示新闻列表的片段,在选项菜单中有一个搜索功能android - restartLoader()和initLoader() fragment 中的第三个参数-LMLPHP
我用装载机填了这张单子。当用户提交搜索时,我重新启动搜索结果加载程序。
我的片段:

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()

但它崩溃了,在我的日志中显示了以下内容:
mainActivity不能强制转换为android.support.v4.app.loadermanager$loadercallbacks
我理解oncreateview()中的加载程序“this”没有问题!
但是在oncreateoptionsmenu()>searchview.setonquerytextliner()>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;

最佳答案

onQueryTextSubmit上,这指的是您正在创建的匿名内部类,而new SearchView.OnQueryTextListener(没有实现LoaderManager.LoaderCallbacks<List<News>>。使用

getLoaderManager().restartLoader(NEWS_LOADER_ID, bundle, NewsListFragment.this);

应该解决你的问题

10-05 22:46