Skip to content

Commit

Permalink
add filter a recyclerview with searchview based on country name
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulazizahwan committed Apr 10, 2020
1 parent 2fdfdad commit 2d8d816
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 32 deletions.
Expand Up @@ -4,12 +4,16 @@
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.SearchView;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
Expand All @@ -28,38 +32,44 @@
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CountryFragment extends Fragment {

RecyclerView rvCovidCountry;
ProgressBar progressBar;
TextView tvTotalCountry;
CovidCountryAdapter covidCountryAdapter;

private static final String TAG = CountryFragment.class.getSimpleName();
ArrayList<CovidCountry> covidCountries;
List<CovidCountry> covidCountries;

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_country, container, false);

// set has option menu as true because we have menu
setHasOptionsMenu(true);

// call view
rvCovidCountry = root.findViewById(R.id.rvCovidCountry);
progressBar = root.findViewById(R.id.progress_circular_country);
tvTotalCountry = root.findViewById(R.id.tvTotalCountries);
rvCovidCountry.setLayoutManager(new LinearLayoutManager(getActivity()));

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(rvCovidCountry.getContext(), DividerItemDecoration.VERTICAL);
dividerItemDecoration.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.line_divider));
rvCovidCountry.addItemDecoration(dividerItemDecoration);

//call list
covidCountries = new ArrayList<>();

// call Volley method
getDataFromServer();

return root;
}

private void showRecyclerView() {
CovidCountryAdapter covidCountryAdapter = new CovidCountryAdapter(covidCountries, getActivity());
covidCountryAdapter = new CovidCountryAdapter(covidCountries, getActivity());
rvCovidCountry.setAdapter(covidCountryAdapter);

ItemClickSupport.addTo(rvCovidCountry).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
Expand All @@ -70,7 +80,7 @@ public void onItemClicked(RecyclerView recyclerView, int position, View v) {
});
}

private void showSelectedCovidCountry(CovidCountry covidCountry){
private void showSelectedCovidCountry(CovidCountry covidCountry) {
Intent covidCovidCountryDetail = new Intent(getActivity(), CovidCountryDetail.class);
covidCovidCountryDetail.putExtra("EXTRA_COVID", covidCountry);
startActivity(covidCovidCountryDetail);
Expand All @@ -79,8 +89,6 @@ private void showSelectedCovidCountry(CovidCountry covidCountry){
private void getDataFromServer() {
String url = "https://corona.lmao.ninja/countries";

covidCountries = new ArrayList<>();

StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Expand All @@ -101,9 +109,13 @@ public void onResponse(String response) {
data.getString("todayDeaths"), data.getString("recovered"),
data.getString("active"), data.getString("critical"),
countryInfo.getString("flag")
));
));
}
tvTotalCountry.setText(jsonArray.length()+" countries");
// tvTotalCountry.setText(jsonArray.length()+" countries");

// Action Bar Title
getActivity().setTitle(jsonArray.length()+" countries");

showRecyclerView();
} catch (JSONException e) {
e.printStackTrace();
Expand All @@ -120,4 +132,30 @@ public void onErrorResponse(VolleyError error) {
});
Volley.newRequestQueue(getActivity()).add(stringRequest);
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.search_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = new SearchView(getActivity());
searchView.setQueryHint("Search...");
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
if (covidCountryAdapter != null) {
covidCountryAdapter.getFilter().filter(newText);
}
return true;
}
});

searchItem.setActionView(searchView);
super.onCreateOptionsMenu(menu, inflater);
}
}
Expand Up @@ -4,6 +4,8 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

Expand All @@ -15,15 +17,19 @@
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

public class CovidCountryAdapter extends RecyclerView.Adapter<CovidCountryAdapter.ViewHolder> {
public class CovidCountryAdapter extends RecyclerView.Adapter<CovidCountryAdapter.ViewHolder> implements Filterable {

private List<CovidCountry> covidCountries;
private List<CovidCountry> covidCountriesFull;

ArrayList<CovidCountry> covidCountries;
private Context context;

public CovidCountryAdapter(ArrayList<CovidCountry> covidCountries, Context context){
public CovidCountryAdapter(List<CovidCountry> covidCountries, Context context) {
this.covidCountries = covidCountries;
this.context = context;
covidCountriesFull = new ArrayList<>(covidCountries);
}

@NonNull
Expand Down Expand Up @@ -63,4 +69,38 @@ public ViewHolder(@NonNull View itemView) {
imgCountryFlag = itemView.findViewById(R.id.imgCountryFlag);
}
}

@Override
public Filter getFilter() {
return covidCountriesFilter;
}

private Filter covidCountriesFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<CovidCountry> filteredCovidCountry = new ArrayList<>();

if (constraint == null || constraint.length() == 0) {
filteredCovidCountry.addAll(covidCountriesFull);
} else {
String filterPattern = constraint.toString().toLowerCase().trim();
for (CovidCountry itemCovidCountry : covidCountriesFull) {
if (itemCovidCountry.getmCovidCountry().toLowerCase().contains(filterPattern)) {
filteredCovidCountry.add(itemCovidCountry);
}
}
}

FilterResults results = new FilterResults();
results.values = filteredCovidCountry;
return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
covidCountries.clear();
covidCountries.addAll((List) results.values);
notifyDataSetChanged();
}
};
}
Expand Up @@ -42,6 +42,9 @@ public View onCreateView(@NonNull LayoutInflater inflater,
tvLastUpdated = root.findViewById(R.id.tvLastUpdated);
progressBar = root.findViewById(R.id.progress_circular_home);

// Action Bar title
getActivity().setTitle("Overview");

// call Volley
getData();

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_search.xml
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
</vector>
17 changes: 0 additions & 17 deletions app/src/main/res/layout/fragment_country.xml
Expand Up @@ -6,28 +6,11 @@
android:background="@color/colorPrimary"
tools:context=".ui.country.CountryFragment">

<LinearLayout
android:id="@+id/linearLayoutTotalCountries"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorPrimaryDark"
android:gravity="center">

<TextView
android:id="@+id/tvTotalCountries"
style="@style/TotalFontStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="24sp"
tools:text="189 countries" />
</LinearLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvCovidCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linearLayoutTotalCountries"
tools:listitem="@layout/item_list_covid_country" />

<ProgressBar
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/menu/search_menu.xml
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search"
android:title="Search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always|collapseActionView" />
</menu>
5 changes: 3 additions & 2 deletions app/src/main/res/values/styles.xml
@@ -1,11 +1,12 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimary">@color/colorTotalConfirmed</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">#fff</item>
</style>

<style name="BottomNavigationView">
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -8,7 +8,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
classpath 'com.android.tools.build:gradle:3.6.2'


// NOTE: Do not place your application dependencies here; they belong
Expand Down

0 comments on commit 2d8d816

Please sign in to comment.