单声道的AddTextChangedListener for android

use*_*003 1 listview filtering xamarin.android textchanged

我有一个数据库,我用来填充列表视图.在此列表视图上方有一个edittext,用户在列表视图中搜索项目.基于输入,我希望对列表视图进行细化和过滤,以仅包含与输入类似的项目.这在java中是直截了当的,但在xamarin中很难用于android.这是我的onCreate()方法,其中数据库填充listview.

string[] categories;
protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);
    SetContentView(Resource.Layout.b);
    var destPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "DDxDDB"); 
    System.IO.Stream source = Assets.Open("DDxDDB");
    var dest = System.IO.File.Create (destPath);
    source.CopyTo (dest); 
    var sql = "SELECT _id FROM Sx;";
    var conn = new SqliteConnection ("Data Source=" + destPath.ToString());
    conn.Open ();
    var cmd = conn.CreateCommand ();
    cmd.CommandText = sql;
    SqliteDataReader reader = cmd.ExecuteReader ();
    List<string> categ = new List<string>();
    while (reader.Read()) 
    {
        categ.Add(reader.GetString(0));
    }
    categories = categ.ToArray();
    ArrayAdapter<string> dataAdapter = new ArrayAdapter<String>(this, Resource.Layout.Main, categories);
    ListView listView = (ListView) FindViewById(Resource.Id.listView1);
    listView.Adapter = dataAdapter; 
    listView.TextFilterEnabled = true;
    EditText myFilter = (EditText)FindViewById(Resource.Id.myFilter);
    myFilter.AddTextChangedListener(New MyTextWatcher);    
}
Run Code Online (Sandbox Code Playgroud)

这是我在此活动中也包含的类,但我不知道要添加到方法中的代码以导致我对listview的过滤.

public class MyTextWatcher: Java.Lang.Object, ITextWatcher
{
    public void AfterTextChanged(IEditable s) {}
    public void BeforeTextChanged(Java.Lang.ICharSequence arg0, int start, int count, int after) {}
    public void OnTextChanged(Java.Lang.ICharSequence arg0, int start, int before, int count) {}
}
Run Code Online (Sandbox Code Playgroud)

Che*_*ron 5

正如aaronmix所说EditText,你可以使用三个事件而不必实现一个ITextWatcher类.

所以,你可以将一个EventHandlerAfterTextChanged,并得到了Editable来自的参数Event:

var editText = FindViewById<EditText>(Resource.Id.MyEditText);
editText.AfterTextChanged += (sender, args) =>
{
    //do your stuff in here
    //args.Editable is the equivalent of the `s` argument from Java
    //afterTextChanged(Editable s)
};
Run Code Online (Sandbox Code Playgroud)