Membuat Fitur Pencarian di Android dengan data statis
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.asus.searchbar.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listViewCountry"
android:layout_weight="1">
</ListView>
</LinearLayout>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.asus.searchbar.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listViewCountry"
android:layout_weight="1">
</ListView>
</LinearLayout>
2. Untuk datanya kita akan menggunakan
data statis yang disimpan di string. simpan file dibawah dengan nama string.xml
di folder values
<resources> <string name="app_name">SearchBar</string> <string-array name="array_country"> <item>Afganistan</item> <item>Australia</item> <item>Afrika Selatan</item> <item>Amerika</item> <item>Belanda</item> <item>Brazil</item> <item>Brunei Darussalam</item> <item>China</item> <item>Ceko</item> <item>Estonia</item> <item>Denmark</item> <item>Finlandia</item> <item>Francis</item> <item>Ghana</item> <item>Haiti</item> <item>Honduras</item> <item>Hongaria</item> <item>Italia</item> <item>India</item> <item>Iran</item> <item>Irak</item> <item>Israel</item> <item>Indonesia</item> <item>Inggris</item> <item>Jerman</item> <item>Jepang</item> <item>Kanada</item> <item>Korea Selatan</item> <item>Kabul</item> <item>Leshoto</item> <item>Latvia</item> <item>Luxseburg</item> <item>Madagaskar</item> <item>Malaysia</item> <item>Maldives</item> <item>Mesir</item> <item>Meksiko</item> <item>Nepal</item> <item>Norwegia</item> <item>Oman</item> <item>Paraguay</item> <item>Papua Nugini</item> <item>Peru</item> <item>Pilipina</item> <item>Portugal</item> <item>Qatar</item> <item>Rusia</item> <item>Swedia</item> <item>Swiss</item> <item>Saudi Arabia</item> <item>Selandia Baru</item> <item>Singapura</item> <item>Thailand</item> <item>Timor Leste</item> <item>Turkey</item> <item>Uruguay</item> <item>Uganda</item> <item>Vietnam</item> <item>Yaman</item> </string-array> </resources>
3. Buat Menu Pencariannya, caranya buat
dulu android resource directory di folder res dengan nama menu. kemudian dalam
folder menu buat file menu_search.xml dan isikan kode dibawah:
<?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/menuSearch" android:icon="@android:drawable/ic_menu_search" android:title="Search" app:actionViewClass="android.support.v7.widget.SearchView" app:showAsAction="always"></item> </menu>
4. Langkah terakhir untuk kode javanya
silakan masukan kode dibawah :
package com.example.asus.searchbar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.SearchView; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; import java.util.Arrays; public class MainActivity extends AppCompatActivity { ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv = (ListView) findViewById(R.id.listViewCountry); ArrayList<String> ArrayCountry = new ArrayList<>(); ArrayCountry.addAll(Arrays.asList(getResources().getStringArray(R.array.array_country))); adapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_list_item_1, ArrayCountry); lv.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_search, menu); MenuItem item = menu.findItem(R.id.menuSearch); SearchView searchView = (SearchView)item.getActionView(); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { adapter.getFilter().filter(newText); return false; } }); return super.onCreateOptionsMenu(menu); } }
Selanjutnya silakan jalankan aplikasi anda bila
ada yang error bias ditanyakan pada kolom komentar dibawah
0 komentar :