"Mbak, tau caranya bikin search yang bisa berubah-ubah list datanya tergantung teks yang dimasukkin gak?"
Berawal dari pertanyaan itu waktu di lab, akhirnya aku cari-cari tutorialnya, dan dapat akhirnya di link ini. Dalam posting disana, memanfaatkan method TextChange. Berikut ini sintaksnya:
list_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/labelList" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/labelList" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/str" /> <EditText android:id="@+id/searchEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="22dp" android:ems="10" > <requestFocus /> </EditText> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
activity_main.xml |
Activity Java
import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.text.Editable; import android.text.TextWatcher; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private EditText seacrh; ArrayAdapter<String> dataAdapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); seacrh = (EditText) findViewById(R.id.searchEditText); updateList(); seacrh.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { dataAdapter.getFilter().filter(s.toString()); } }); } public void updateList() { // Array list List<String> name = new ArrayList<String>(); name.add("Abimayu"); name.add("Adewira"); name.add("Afif S"); name.add("Andes"); name.add("Anik Richie"); name.add("Azis Catur Laksono"); name.add("Danang Nur"); name.add("Dimas Adila"); name.add("Dyah Fajar"); name.add("Femy Dwi"); name.add("Febrianti"); name.add("Ismi U.K"); name.add("Kartika Kusuma"); name.add("Kusuma Winanti"); name.add("Nasrokhah"); name.add("Nonie Yunicasari"); name.add("Nova Nurhuda"); name.add("Nurlaela"); name.add("Pipit Sulidar Fitri"); name.add("Pratama"); name.add("Puguh Hasta"); name.add("Puji Laksono"); name.add("Yode Arliando"); name.add("Wahyu Noer Hidayat"); // create an ArrayAdaptar from the String Array dataAdapter = new ArrayAdapter<String>(this, R.layout.list_layout, R.id.labelList, name); ListView listView = (ListView) findViewById(R.id.list); // Assign adapter to ListView listView.setAdapter(dataAdapter); // enables filtering for the contents of the given ListView listView.setTextFilterEnabled(true); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // When clicked, show a toast with the TextView text Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); } }); // enables filtering for the contents of the given ListView listView.setTextFilterEnabled(true); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
Mbak, kalo dari hasil pencarian dibuat biar bisa diklik untuk masuk activity selanjutnya bisa gak ?
BalasHapusbisa
HapusPunya contoh codingnya gak mba ?atau referensinya
Hapuscontoh source codenya belum ada mbak agitta.. mohon maaf, tapi pernah saya temukan di stackoverflow
Hapus