title: Android SQLite Listview Database Search using EditText
body: <p>I have a <code>listview</code> populated from <code>database</code> which consists of <strong>phrases</strong>(in english) and it's translation(in malay). The interface is like the <code>image below.</code></p>
  
  <p><a href="http://i.stack.imgur.com/LJIqY.jpg" rel="nofollow">http://i.stack.imgur.com/LJIqY.jpg</a></p>
  
  <p>For starter, my goal is to search the <code>listview</code> by phrases. When a <code>keyword</code> is entered in the search <code>EditText</code>, I want the <code>listview</code> to be <strong>filtered</strong> accordingly. I already have the <code>EditText</code> ready there, </p>
  
  <p>but I have no idea how to start. Reading a lot of tutorials made me confused. I'm not even sure that my approach is correct.</p>
  
  <p>Here is activity ListGreetings.java`</p>
  
  <pre><code>ListView userList;
  GreetingAdapter greetAdapter;
  EditText inputSearch;
  ArrayList&lt;Frasa&gt; frasaArray;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_list_greetings);
  
      //database activities
      DB db = new DB(this);
  
      try {
          db.create();
      } catch (IOException ioe) {
          throw new Error("Unable to create database");
      }
  
      if(db.open()) {
          //get Frasa data
          frasaArray = db.getFrasa("Greetings");
  
  
          /**
           * set item into adapter
           */
          greetAdapter = new GreetingAdapter(ListGreetings.this, R.layout.list_item_greet, frasaArray);
          userList = (ListView) findViewById(R.id.listGreetings);
          userList.setItemsCanFocus(false);
          userList.setAdapter(greetAdapter);
      }
  
      //inputSearch.setOnQueryTextListener(this);
  
          //Searching start here
          inputSearch = (EditText) findViewById(R.id.inputSearch);
          inputSearch.addTextChangedListener(new TextWatcher() {
  
              @Override
              public void beforeTextChanged(CharSequence s, int start, int before, int count) {
  
              }
  
              @Override
              public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
                  // TODO Auto-generated method stub
  
              }
  
              @Override
              public void afterTextChanged(Editable s) {
                  // When user changed the Text
              }
          });
  }
  </code></pre>
  
  <p>GreetingAdapter.java</p>
  
  <pre><code>public class GreetingAdapter extends ArrayAdapter&lt;Frasa&gt; {
  int layoutResourceId;
  Context context;
  Resources resources;
  ArrayList&lt;Frasa&gt; data = new ArrayList&lt;Frasa&gt;();
  MediaPlayer speak;
  
  //recorder variables
  MediaPlayer playRecorder = null;
  MediaRecorder recorder = null;
  String mFileName = null;
  final String LOG_TAG = "malay4U_Record";
  boolean mStartRecording = true;
  boolean mStartPlaying = true;
  
  public GreetingAdapter(Context context, int layoutResourceId, ArrayList&lt;Frasa&gt; data)
  {
      super(context, layoutResourceId, data);
      this.layoutResourceId = layoutResourceId;
      this.context = context;
      this.resources = this.context.getResources();
      this.data = data;
      this.mFileName =  Environment.getExternalStorageDirectory().getAbsolutePath();
      mFileName += "/audiorecordtest.3gp";
  
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      View row = convertView;
      UserHolder holder = null;
  
      if (row == null) {
          LayoutInflater inflater = ((Activity) context).getLayoutInflater();
          row = inflater.inflate(layoutResourceId, parent, false);
          holder = new UserHolder();
          holder.textPhrase = (TextView) row.findViewById(R.id.textViewPhrase);
          holder.textTranslate = (TextView) row.findViewById(R.id.textViewTranslate);
          holder.btnSpeak = (Button) row.findViewById(R.id.buttonSpeak);
          holder.btnRecord = (Button) row.findViewById(R.id.buttonRecord);
          holder.btnPlay = (Button) row.findViewById(R.id.buttonPlay);
          row.setTag(holder);
      } else {
          holder = (UserHolder) row.getTag();
      }
      final Frasa frasa = data.get(position);
      holder.textPhrase.setText(frasa.getPhrase());
      holder.textTranslate.setText(frasa.getTranslate());
  
      //button speak action
      holder.btnSpeak.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
  
              String direct = frasa.getPronounce(); //audio file directory where in database stored as raw/filename
  
              int resID = resources.getIdentifier(direct, "String", "hairulhazri.malayforyou"); //convert string to resouce ID
              speak = MediaPlayer.create(context, resID);
              speak.start();
          }
      });
  
      //button Record action
  
      holder.btnRecord.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
  
              onRecord(mStartRecording);
              if (mStartRecording) {
                  Log.i("Record Button Clicked", "**********");
                  Toast.makeText(context, "Recording....",
                          Toast.LENGTH_LONG).show();
              } else {
                  Toast.makeText(context, "Recording finished.",
                          Toast.LENGTH_LONG).show();
              }
              mStartRecording = !mStartRecording;
  
  
          }
      });
  
      //button play record action
      holder.btnPlay.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              Log.i("Play Button Clicked", "*******");
              onPlay(mStartPlaying);
  
              mStartPlaying = !mStartPlaying;
          }
      });
      return row;
  
  }
  
  static class UserHolder {
      SearchView inputSearch;
      TextView textPhrase;
      TextView textTranslate;
      Button btnSpeak;
      Button btnRecord;
      Button btnPlay;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>function getFrasa in database</p>
  
  <pre><code>public ArrayList&lt;Frasa&gt; getFrasa(String situation ) {
  
      //ArrayList of Frasa class objects
      ArrayList&lt;Frasa&gt; arrFrasa = null;
      String selectQuery = "SELECT * FROM Frasa WHERE situation = " +"'"+situation+"'";
  
      db = SQLiteDatabase.openDatabase( DB_PATH + DB_NAME , null, SQLiteDatabase.OPEN_READWRITE);
      Cursor curFrasa = db.rawQuery(selectQuery, null);
  
      if (curFrasa != null &amp;&amp; curFrasa.moveToFirst()) {
          arrFrasa = new ArrayList&lt;Frasa&gt;();
          while (curFrasa.isAfterLast() == false) {
              //Frasa is a class with list of fields
              Frasa fra = new Frasa();
              fra.setId(curFrasa.getInt(curFrasa.getColumnIndex(Frasa.KEY_ID)));
              fra.setPhrase(curFrasa.getString(curFrasa.getColumnIndex(Frasa.KEY_PHRASE)));
              fra.setTranslate(curFrasa.getString(curFrasa.getColumnIndex(Frasa.KEY_TRANSLATE)));
              fra.setPronounce(curFrasa.getString(curFrasa.getColumnIndex(Frasa.KEY_PRONOUNCE)));
              arrFrasa.add(fra);
              curFrasa.moveToNext();
          }
      }
  
  
      curFrasa.close();
      db.close();
      return arrFrasa;
  }
  </code></pre>
  
  <p>Where do I apply the <code>filtering</code>? Inside <code>ListGreetings</code> or <code>GreetingAdapter</code>? And how?</p>
  
  <p>Thank you in advance.</p>