2

我有一个用于填充列表视图的数据库。在此列表视图上方有一个编辑文本,用户在其中搜索列表视图中的项目。根据输入,我希望对列表视图进行细化和过滤,以仅包含与输入相似的项目。这在 java 中很简单,但在 xamarin 的单声道中很难。这是我的 onCreate() 方法,数据库填充列表视图。

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);    
}

这是我也包含在此活动中的类,但我不知道将哪些代码添加到方法中以导致我过滤列表视图。

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) {}
}
4

3 回答 3

6

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

因此,您可以附加EventHandlerAfterTextChangedEditable从以下参数中获取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)
};
于 2012-12-19T23:29:25.947 回答
2

在 Mono for Android 的实现中,EditText已经有三个事件和TextChanged,因此您可以为每个事件添加委托。AfterTextChangedBeforeTextChanged

于 2012-12-19T18:07:56.797 回答
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;
} 
于 2012-12-20T00:37:22.573 回答