SearchView Example
search icon is used for search all the contents in this page. Finally we implements SearchView.OnQueryTextListener to filter the list according to search query.
actvity_main.xml
This xml shows listview with searchview option.
MainActivity.java
This activity adds the code to initiate SearchView and ListView. We also implements SearchView.OnQueryTextListener to filter lists according to search query.
package com.example.codebridgeexample; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ListView; import android.widget.SearchView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener { ListView listView; ListViewAdapter listViewAdapter; SearchView searchView; String[] countryList; ArrayList arraylist = new ArrayList(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); countryNameList = new String[]{"India", "Australia", "Japan", "China", "Hongkong", "Russia", "Europe", "France", "Chicago","Germany","Malayasia"}; listView = (ListView) findViewById(R.id.listView); for (int i = 0; i < countryNameList.length; i++) { CountryNames countryNames = new CountryNames(countryNameList[i]); arraylist.add(countryNames ); } listViewAdapter= new ListViewAdapter(this, arraylist); list.setAdapter(listViewAdapter); editsearch = (SearchView) findViewById(R.id.searchView); editsearch.setOnQueryTextListener(this); } @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { String text = newText; adapter.filter(text); return false; } }
ListViewAdapter.java
we extends baseadapter in ListViewAdapter class and then set the data in the ListView by using Modal class.
package com.example.codebrdigeexample; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import java.util.Locale; public class ListViewAdapter extends BaseAdapter { Context mContext; LayoutInflater layoutInflater; private List countryNamesList = null; private ArrayList arraylist; public ListViewAdapter(Context context, List countryNamesList) { mContext = context; this.countryNamesList= countryNamesList; inflater = LayoutInflater.from(mContext); this.arraylist = new ArrayList(); this.arraylist.addAll(countryNamesList); } public class ViewHolder { TextView name; } @Override public int getCount() { return countryNamesList.size(); } @Override public CountryNames getItem(int position) { return countryNamesList.get(position); } @Override public long getItemId(int position) { return position; } public View getView(final int position, View view, ViewGroup parent) { final ViewHolder holder; if (view == null) { holder = new ViewHolder(); view = inflater.inflate(R.layout.listView_item, null); holder.name = (TextView) view.findViewById(R.id.name); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } holder.name.setText(countryNamesList.get(position).getCountryName()); return view; } // Filter Class public void filter(String charText) { charText = charText.toLowerCase(Locale.getDefault()); countryNamesList.clear(); if (charText.length() == 0) { countryNamesList.addAll(arraylist); } else { for (CountryNames cn : arraylist) { if (cn.getAnimalName().toLowerCase(Locale.getDefault()).contains(charText)) { countryNamesList.add(wp); } } } notifyDataSetChanged(); } }
listView_item.xml
This xml shows code for each row displayed based on listview adapter
CountryNames.java
We have a constructor for setting the country name and a function to get the country name.
package com.example.codebrdigeexample; public class CountryNames { private String countryName; public CountryNames(String countryName) { this.countryName = countryName; } public String getCountryName() { return this.countryName; } }
Leave A Comment