I need to implement a SearchView. My Newsfeed is set up via the CardView and RecyclerView. I also used volley and JSON to create the effect. I am fairly new to android development and I want to be able to search and filter the JSON Activity but I don't know how to. Some of the tutorials are difficult because they used RESTAPI.
Will it be possible to get a sample code and/or some instructions to go along with it please?
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 provides Filterable class to filter the data by a filter (condition). Usually the getFilter() method has to be overridden in the adapter class in which the filter condition is provided to search through a list.
Note: Create model class to filter objects
@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();
}
};
}
Open AndroidManifest.xml and configure the search as shown below.
<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 folder - Open searchable.xml (If xml folder doesn't exists, create a new one) and configure the search as shown below.
<?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" />
Setup Toolbar in Layout file
<?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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.