我有一个用于填充列表视图的数据库。在此列表视图上方有一个编辑文本,用户在其中搜索列表视图中的项目。根据输入,我希望对列表视图进行细化和过滤,以仅包含与输入相似的项目。这在 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) {}
}