Filter Items

This sample shows how to filter the files and folders in sdcard directory. You can also filter any other items on a List View from this sample.

Filter.js
app.ShowProgress("Loading..." + app.GetInternalFolder())
//List Files and folders on the sdcard
 
var myArray = app.ListFolder(app.GetInternalFolder()).sort();
//Called when application is started.
function OnStart()
{
  app.ShowProgress("Loading..." + app.GetInternalFolder())
  //Create a layout with objects vertically centered.
  lay = app.CreateLayout("linear", "TopCenter,FillXY");
 
  //Create a text label and add it to layout.
  txt = app.CreateTextEdit("", 1, .1, "Center,Singleline");
  txt.SetTextSize(32);
  txt.SetHint("filter");
  txt.SetOnChange(change);
  lay.AddChild(txt);
  //Create Check Box
  chk = app.CreateCheckBox("Case insensitive");
  lay.AddChild(chk);
  //Create List
  lst = app.CreateList(myArray, 1, null);
  lst.SetOnTouch(tch);
  lay.AddChild(lst);
  //Add layout to app.
  app.AddLayout(lay);
  app.HideProgress();
}
 
function change()
{
  if (chk.GetChecked())
  {
    insens();
  }
  else
  {
    sens();
  }
}
 
function tch(it)
{
  if (app.IsFolder(app.GetInternalFolder() + "/" + it))
  {
    app.ShowPopup(it, "Short,Bottom");
  }
  else
  {
    app.OpenFile(app.GetInternalFolder() + "/" + it, null, "Select Application");
  }
}
 
function sens()
{
  peo = [];
  s = "";
  text = txt.GetText();
  for (i = 0; i < myArray.length; i++)
  {
    if (myArray[i].indexOf(text) != -1)
    {
      peo.push(myArray[i]);
    }
  }
  lst.SetList(peo);
}
 
function insens()
{
  peo = [];
  s = "";
  text = txt.GetText().toLowerCase();
  for (i = 0; i < myArray.length; i++)
  {
    if (myArray[i].toLowerCase().indexOf(text) != -1)
    {
      peo.push(myArray[i]);
    }
  }
  lst.SetList(peo);
}

HomeยปSample Code