单声道的AddTextChangedListener for android

时间:2012-12-19 08:41:22

标签: listview xamarin.android filtering 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);    
}

这是我在此活动中也包含的类,但我不知道要在方法中添加哪些代码来导致我对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) {}
}

3 个答案:

答案 0 :(得分:5)

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

因此,您可以将EventHandler附加到AfterTextChanged,并从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)
};

答案 1 :(得分:1)

在Mono for Android实施中,EditText已经有三个事件TextChangedAfterTextChangedBeforeTextChanged,因此您只需为每个事件添加代理。

答案 2 :(得分:0)

非常感谢你们两位的帮助。我正在做类似的事情而且效果很好。这是:

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.TextChanged += new EventHandler<TextChangedEventArgs>(OnTextChange);
}

public void OnTextChange(object sender, EventArgs e)
{           
    EditText myFilter = (EditText)FindViewById(Resource.Id.myFilter);
    string val = myFilter.Text.ToString();
    List<string> categ2 = new List<string>();
    foreach (string value in categories) 
    {
        if (value.IndexOf(val, StringComparison.CurrentCultureIgnoreCase) != -1)
        {
            categ2.Add(value);
        }
    }
    string[] categories2 = categ2.ToArray();
    ArrayAdapter<string> dataAdapter = new ArrayAdapter<String>(this, Resource.Layout.Main, categories2);
    ListView listView = (ListView) FindViewById(Resource.Id.listView1);
    listView.Adapter = dataAdapter; 
    listView.TextFilterEnabled = true;
}