<小时/>
我需要实现一个SearchView。我的新闻源是通过 CardView 和 RecyclerView 设置的。我还使用了 volley 和 JSON 来创建效果。我对 Android 开发相当陌生,我希望能够搜索和过滤 JSON Activity,但我不知道如何做。有些教程很困难,因为它们使用了 RESTAPI。
是否可以获得示例代码和/或一些与之配套的说明?
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray =response.getJSONArray("articles");
for (int i = 0; i< jsonArray.length(); i++){
JSONObject article = jsonArray.getJSONObject(i);
String authorName = article.getString("author");
String imageUrl = article.getString("urlToImage");
String published = article.getString("publishedAt");
String description = article.getString("content");
String headline = article.getString("title");
mNewsList.add(new newsItem(imageUrl, authorName, published,description, headline ));
}
mNewsAdapter = new newsAdapter (news.this,mNewsList);
mRecyclerView.setAdapter(mNewsAdapter);
//calling onItemClick
mNewsAdapter.setOnItemClickListener(news.this);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
public void onErrorResponse(VolleyError error){
error.printStackTrace();
}
});
mRequestQueue.add(request);
}
最佳答案
Android提供了Filterable类来通过过滤器(条件)过滤数据。通常必须在提供过滤条件的适配器类中重写 getFilter() 方法以搜索列表。
注意:创建模型类来过滤对象
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
contactListFiltered = contactList;
} else {
List<Contact> filteredList = new ArrayList<>();
for (Contact row : contactList) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getName().toLowerCase().contains(charString.toLowerCase()) || row.getPhone().contains(charSequence)) {
filteredList.add(row);
}
}
contactListFiltered = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = contactListFiltered;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
contactListFiltered = (ArrayList<Contact>) filterResults.values;
// refresh the list with filtered data
notifyDataSetChanged();
}
};
}
打开 AndroidManifest.xml 并配置搜索,如下所示。
<activity
android:name="YourActivity">
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
res ⇒ xml 文件夹 - 打开 searchable.xml(如果 xml 文件夹不存在,则创建一个新文件夹)并配置搜索,如下所示。
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_hint"
android:label="@string/app_name" />
布局文件中的设置工具栏
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.androidhive.recyclerviewsearch.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/white"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
关于java - 如何实现搜索 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60032183/