我用ListView和JSON做了一个新闻应用程序。我想向我的应用添加搜索功能。所以 IhAave 已经在操作栏中显示我的搜索栏。问题是每当我单击"搜索"时,我的ListView结果都会消失,并且我搜索的单词不会产生任何结果。我正在发布我的一个片段的代码。(我已经通过设置日志消息检查了搜索栏的工作原理,我在日志消息中看到了这一点。但是当涉及到我的列表视图时,它没有显示任何结果。也许我的适配器是空的?或者我在某处缺少列表视图。请帮助我。
这是代码。
public class Uspolitics extends Fragment implements LoaderManager.LoaderCallbacks<List<News>> {
private static final String LOG_TAG = Uspolitics.class.getSimpleName();
private static final String NEWS_API = "https://newsapi.org/v2/top-headlines?country=us&apiKey=6a6f7c77766442acb20c86157a152131";
private NewsAdapter mAdapter;
private static final int EARTHQUAKE_LOADER_ID = 1;
private TextView mEmptyStateTextView;
private ProgressBar progressSpinner;
private String mUrl;
public Uspolitics() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.activity_all, container, false);
setHasOptionsMenu(true);
mEmptyStateTextView = rootView.findViewById(R.id.empty_view);
final ListView listView = (ListView) rootView.findViewById(R.id.list);
listView.setEmptyView(mEmptyStateTextView);
mAdapter = new NewsAdapter(getActivity(), new ArrayList<News>());
listView.setAdapter(mAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
News currentNews = mAdapter.getItem(position);
Uri uriweb = Uri.parse(currentNews.getURL());
Intent webIntent = new Intent(Intent.ACTION_VIEW, uriweb);
startActivity(webIntent);
}
});
progressSpinner = rootView.findViewById(R.id.loading_indicator);
// Indeterminate progress bar type
progressSpinner.setIndeterminate(true);
ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
if (getActivity() != null) {
if (connMgr != null) {
// Get details on the currently active default data network
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
LoaderManager loaderManager = getActivity().getSupportLoaderManager();
loaderManager.initLoader(EARTHQUAKE_LOADER_ID, null, this);
} else {
progressSpinner.setVisibility(ProgressBar.GONE);
mEmptyStateTextView.setText("NO INTERNET CONNECTION");
}
}
}
return rootView;
}
@Override
public Loader<List<News>> onCreateLoader(int i, Bundle bundle) {
return new PoliticsLoader(getContext(), NEWS_API);
}
@Override
public void onLoadFinished(Loader<List<News>> loader, List<News> news) {
progressSpinner.setVisibility(ProgressBar.GONE);
mAdapter.clear();
if (news != null && !news.isEmpty()) {
mAdapter.addAll(news);
}
}
@Override
public void onLoaderReset(Loader<List<News>> loader) {
mAdapter.clear();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the options menu from XML
MenuInflater inflaters = getActivity().getMenuInflater();
inflater.inflate(R.menu.dashboard, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
mAdapter.getFilter().filter(s); // **I think this is issue**
return true;
}
@Override
public boolean onQueryTextChange(String s) {
return true;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
}
单击提交按钮后,您需要再次请求 API。
我认为,您应该使用RecyclerView而不是ListView,并且还使用Volley或Retrofit for Rest API。